mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-30 23:02:50 -06:00
TranslateApp now have browser for abstract syntax
This commit is contained in:
156
src/server/gwt/src/se/chalmers/cs/gf/gwt/client/BrowsePanel.java
Normal file
156
src/server/gwt/src/se/chalmers/cs/gf/gwt/client/BrowsePanel.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import java.util.*;
|
||||
import java.lang.*;
|
||||
import com.google.gwt.core.client.*;
|
||||
import com.google.gwt.user.client.ui.*;
|
||||
import com.google.gwt.http.client.*;
|
||||
import com.google.gwt.dom.client.*;
|
||||
|
||||
public class BrowsePanel extends Composite {
|
||||
|
||||
private PGFWrapper pgf;
|
||||
private HTML sourceView;
|
||||
private SuggestBox searchBox;
|
||||
private CompletionOracle oracle;
|
||||
private List<String> identifiers = null;
|
||||
|
||||
public BrowsePanel(PGFWrapper pgf) {
|
||||
this.pgf = pgf;
|
||||
|
||||
oracle = new CompletionOracle();
|
||||
|
||||
HorizontalPanel browsePanel = new HorizontalPanel();
|
||||
browsePanel.add(createSearchPanel(oracle));
|
||||
browsePanel.add(createSourcePanel());
|
||||
browsePanel.setCellWidth(sourceView,"100%");
|
||||
|
||||
initWidget(browsePanel);
|
||||
setStylePrimaryName("my-BrowsePanel");
|
||||
|
||||
pgf.addSettingsListener(new MySettingsListener(pgf));
|
||||
}
|
||||
|
||||
public native void onActivate() /*-{
|
||||
$doc.browsePanel = this;
|
||||
$doc.callBrowse = @se.chalmers.cs.gf.gwt.client.BrowsePanel::callBrowse(Lse/chalmers/cs/gf/gwt/client/BrowsePanel;Ljava/lang/String;);
|
||||
}-*/;
|
||||
|
||||
protected Widget createSearchPanel(CompletionOracle oracle) {
|
||||
searchBox = new SuggestBox(oracle);
|
||||
searchBox.setLimit(10);
|
||||
searchBox.addKeyboardListener(new KeyboardListenerAdapter() {
|
||||
public void onKeyUp (Widget sender, char keyCode, int modifiers) {
|
||||
if (keyCode == KEY_ENTER) {
|
||||
browse(searchBox.getText());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
DecoratorPanel decorator = new DecoratorPanel();
|
||||
VerticalPanel vPanel = new VerticalPanel();
|
||||
vPanel.add(new Label("Search"));
|
||||
vPanel.add(searchBox);
|
||||
decorator.add(vPanel);
|
||||
return decorator;
|
||||
}
|
||||
|
||||
private static void callBrowse(BrowsePanel panel, String id) {
|
||||
panel.browse(id);
|
||||
}
|
||||
|
||||
protected void browse(String id) {
|
||||
pgf.browse(id, "javascript:document.callBrowse(document.browsePanel,'$ID')",
|
||||
"my-identifierLink",
|
||||
new RequestCallback() {
|
||||
public void onResponseReceived(Request request, Response response) {
|
||||
sourceView.setHTML(response.getText());
|
||||
}
|
||||
|
||||
public void onError(Request request, java.lang.Throwable exception) {
|
||||
// errorHandler.onError(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected Widget createSourcePanel() {
|
||||
sourceView = new HTML();
|
||||
return sourceView;
|
||||
}
|
||||
|
||||
protected class CompletionOracle extends SuggestOracle {
|
||||
|
||||
public CompletionOracle() {
|
||||
}
|
||||
|
||||
public void requestSuggestions(SuggestOracle.Request request, SuggestOracle.Callback callback) {
|
||||
List<CompletionSuggestion> list = new ArrayList();
|
||||
|
||||
int index = Collections.binarySearch(identifiers, request.getQuery());
|
||||
index = (index >= 0) ? index : -(index+1);
|
||||
|
||||
for (; index < identifiers.size(); index++) {
|
||||
String id = identifiers.get(index);
|
||||
|
||||
if (id.startsWith(request.getQuery())) {
|
||||
list.add(new CompletionSuggestion(id));
|
||||
}
|
||||
else
|
||||
break;
|
||||
|
||||
if (list.size() > request.getLimit())
|
||||
break;
|
||||
}
|
||||
|
||||
callback.onSuggestionsReady(request, new SuggestOracle.Response(list));
|
||||
}
|
||||
}
|
||||
|
||||
protected static class CompletionSuggestion implements SuggestOracle.Suggestion {
|
||||
private String string;
|
||||
|
||||
public CompletionSuggestion(String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
public String getDisplayString() {
|
||||
return string;
|
||||
}
|
||||
|
||||
public String getReplacementString() {
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
||||
protected class MySettingsListener implements PGFWrapper.SettingsListener {
|
||||
|
||||
private PGFWrapper pgf;
|
||||
|
||||
public MySettingsListener(PGFWrapper pgf) {
|
||||
this.pgf = pgf;
|
||||
}
|
||||
|
||||
public void onAvailableGrammarsChanged() { }
|
||||
public void onSelectedGrammarChanged()
|
||||
{
|
||||
List<String> ids = new ArrayList();
|
||||
|
||||
for (int i = 0; i < pgf.getCategories().length(); i++) {
|
||||
ids.add(pgf.getCategories().get(i));
|
||||
}
|
||||
for (int i = 0; i < pgf.getFunctions().length(); i++) {
|
||||
ids.add(pgf.getFunctions().get(i));
|
||||
}
|
||||
|
||||
Collections.sort(ids);
|
||||
|
||||
identifiers = ids;
|
||||
sourceView.setText("");
|
||||
searchBox.setText("");
|
||||
}
|
||||
public void onInputLanguageChanged() { }
|
||||
public void onOutputLanguageChanged() { }
|
||||
public void onStartCategoryChanged() { }
|
||||
public void onSettingsError(String msg, Throwable e) { }
|
||||
}
|
||||
}
|
||||
@@ -30,12 +30,10 @@ public class CompletionOracle extends SuggestOracle {
|
||||
this.errorHandler = errorHandler;
|
||||
pgf.addSettingsListener(new PGFWrapper.SettingsListener() {
|
||||
public void onAvailableGrammarsChanged() { clearState(); }
|
||||
public void onAvailableLanguagesChanged() { clearState(); }
|
||||
public void onSelectedGrammarChanged() { clearState(); }
|
||||
public void onInputLanguageChanged() { clearState(); }
|
||||
public void onOutputLanguageChanged() { clearState(); }
|
||||
public void onAvailableCategoriesChanged() { clearState(); }
|
||||
public void onStartCategoryChanged() { clearState(); }
|
||||
public void onAvailableFunctionsChanged() { clearState(); }
|
||||
public void onSettingsError(String msg, Throwable e) { clearState(); }
|
||||
});
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ public class FridgeApp implements EntryPoint {
|
||||
}
|
||||
}
|
||||
}
|
||||
public void onAvailableLanguagesChanged() {
|
||||
public void onSelectedGrammarChanged() {
|
||||
if (pgf.getInputLanguage() == null) {
|
||||
pgf.setInputLanguage(pgf.getUserLanguage());
|
||||
}
|
||||
@@ -265,13 +265,9 @@ public class FridgeApp implements EntryPoint {
|
||||
public void onOutputLanguageChanged() {
|
||||
update();
|
||||
}
|
||||
public void onAvailableCategoriesChanged() {
|
||||
}
|
||||
public void onStartCategoryChanged() {
|
||||
update();
|
||||
}
|
||||
public void onAvailableFunctionsChanged() {
|
||||
}
|
||||
public void onSettingsError(String msg, Throwable e) {
|
||||
showError(msg,e);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,9 @@ package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import se.chalmers.cs.gf.gwt.client.JSONRequestBuilder.Arg;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.*;
|
||||
import com.google.gwt.core.client.*;
|
||||
import com.google.gwt.http.client.*;
|
||||
|
||||
public class PGF {
|
||||
|
||||
@@ -29,9 +28,9 @@ public class PGF {
|
||||
|
||||
public final native IterableJsArray<Language> getLanguages() /*-{ return this.languages; }-*/;
|
||||
|
||||
public final native IterableJsArray<Category> getCategories() /*-{ return this.categories; }-*/;
|
||||
public final native JsArrayString getCategories() /*-{ return this.categories; }-*/;
|
||||
|
||||
public final native IterableJsArray<Function> getFunctions() /*-{ return this.functions; }-*/;
|
||||
public final native JsArrayString getFunctions() /*-{ return this.functions; }-*/;
|
||||
}
|
||||
|
||||
public static class Language extends JavaScriptObject {
|
||||
@@ -42,18 +41,6 @@ public class PGF {
|
||||
public final native boolean canParse() /*-{ return this.canParse; }-*/;
|
||||
}
|
||||
|
||||
public static class Category extends JavaScriptObject {
|
||||
protected Category() { }
|
||||
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
}
|
||||
|
||||
public static class Function extends JavaScriptObject {
|
||||
protected Function() { }
|
||||
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
}
|
||||
|
||||
/* Translation */
|
||||
|
||||
public JSONRequest translate (String pgfURL, String input, String fromLang, String cat, String toLang,
|
||||
@@ -167,6 +154,26 @@ public class PGF {
|
||||
return JSONRequestBuilder.getQueryURL(pgfURL,args);
|
||||
}
|
||||
|
||||
public Request browse(String pgfURL, String id, String href, String cssClass, RequestCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("command", "browse"));
|
||||
args.add(new Arg("id", id));
|
||||
args.add(new Arg("href", href));
|
||||
args.add(new Arg("css-class", cssClass));
|
||||
|
||||
Request request = null;
|
||||
try {
|
||||
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
|
||||
JSONRequestBuilder.getQueryURL(pgfURL,args));
|
||||
builder.setCallback(callback);
|
||||
request = builder.send();
|
||||
} catch (RequestException ex) {
|
||||
callback.onError(request, ex);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
/* Common */
|
||||
|
||||
public <T extends JavaScriptObject> JSONRequest sendGrammarRequest(String pgfURL, String resource, List<Arg> args, final JSONCallback<T> callback) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import com.google.gwt.http.client.*;
|
||||
import com.google.gwt.xml.client.*;
|
||||
import com.google.gwt.core.client.*;
|
||||
|
||||
public class PGFWrapper {
|
||||
|
||||
@@ -36,8 +37,8 @@ public class PGFWrapper {
|
||||
|
||||
private List<String> parseableLanguages;
|
||||
|
||||
private List<String> categories;
|
||||
private List<String> functions;
|
||||
private JsArrayString categories;
|
||||
private JsArrayString functions;
|
||||
|
||||
// Event listeners
|
||||
|
||||
@@ -114,19 +115,11 @@ public class PGFWrapper {
|
||||
parseableLanguages.add(name);
|
||||
}
|
||||
}
|
||||
fireAvailableLanguagesChanged();
|
||||
|
||||
categories = new ArrayList<String>();
|
||||
for (PGF.Category category : grammar.getCategories().iterable()) {
|
||||
categories.add(category.getName());
|
||||
}
|
||||
fireAvailableCategoriesChanged();
|
||||
categories = grammar.getCategories();
|
||||
functions = grammar.getFunctions();
|
||||
|
||||
functions = new ArrayList<String>();
|
||||
for (PGF.Function function : grammar.getFunctions().iterable()) {
|
||||
functions.add(function.getName());
|
||||
}
|
||||
fireAvailableFunctionsChanged();
|
||||
fireSelectedGrammarChanged();
|
||||
}
|
||||
|
||||
public void onError (Throwable e) {
|
||||
@@ -163,6 +156,10 @@ public class PGFWrapper {
|
||||
return pgf.graphvizAlignment(grammarURL,abstractTree);
|
||||
}
|
||||
|
||||
public Request browse(String id, String href, String cssClass, RequestCallback callback) {
|
||||
return pgf.browse(grammarURL, id, href, cssClass, callback);
|
||||
}
|
||||
|
||||
//
|
||||
// Settings
|
||||
//
|
||||
@@ -220,11 +217,11 @@ public class PGFWrapper {
|
||||
fireStartCategoryChanged();
|
||||
}
|
||||
|
||||
public List<String> getCategories() {
|
||||
public JsArrayString getCategories() {
|
||||
return categories;
|
||||
}
|
||||
|
||||
public List<String> getFunctions() {
|
||||
public JsArrayString getFunctions() {
|
||||
return functions;
|
||||
}
|
||||
|
||||
@@ -267,23 +264,19 @@ public class PGFWrapper {
|
||||
|
||||
public interface SettingsListener {
|
||||
public void onAvailableGrammarsChanged();
|
||||
public void onAvailableLanguagesChanged();
|
||||
public void onSelectedGrammarChanged();
|
||||
public void onInputLanguageChanged();
|
||||
public void onOutputLanguageChanged();
|
||||
public void onAvailableCategoriesChanged();
|
||||
public void onStartCategoryChanged();
|
||||
public void onAvailableFunctionsChanged();
|
||||
public void onSettingsError(String msg, Throwable e);
|
||||
}
|
||||
|
||||
public static class SettingsAdapter implements SettingsListener {
|
||||
public void onAvailableGrammarsChanged() {}
|
||||
public void onAvailableLanguagesChanged() {}
|
||||
public void onSelectedGrammarChanged() {}
|
||||
public void onInputLanguageChanged() {}
|
||||
public void onOutputLanguageChanged() {}
|
||||
public void onAvailableCategoriesChanged() {}
|
||||
public void onStartCategoryChanged() {}
|
||||
public void onAvailableFunctionsChanged() {}
|
||||
public void onSettingsError(String msg, Throwable e) {}
|
||||
}
|
||||
|
||||
@@ -297,9 +290,9 @@ public class PGFWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireAvailableLanguagesChanged() {
|
||||
protected void fireSelectedGrammarChanged() {
|
||||
for (SettingsListener listener : listeners) {
|
||||
listener.onAvailableLanguagesChanged();
|
||||
listener.onSelectedGrammarChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,23 +308,11 @@ public class PGFWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireAvailableCategoriesChanged() {
|
||||
for (SettingsListener listener : listeners) {
|
||||
listener.onAvailableCategoriesChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireStartCategoryChanged() {
|
||||
for (SettingsListener listener : listeners) {
|
||||
listener.onStartCategoryChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireAvailableFunctionsChanged() {
|
||||
for (SettingsListener listener : listeners) {
|
||||
listener.onAvailableFunctionsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireSettingsError(String msg, Throwable e) {
|
||||
for (SettingsListener listener : listeners) {
|
||||
|
||||
@@ -75,7 +75,7 @@ public class SettingsPanel extends Composite {
|
||||
grammarBox.addItems(pgf.getGrammars());
|
||||
}
|
||||
}
|
||||
public void onAvailableLanguagesChanged() {
|
||||
public void onSelectedGrammarChanged() {
|
||||
if (grammarBox != null) {
|
||||
grammarBox.setSelectedValue(pgf.getPGFName());
|
||||
}
|
||||
@@ -108,9 +108,7 @@ public class SettingsPanel extends Composite {
|
||||
toLangBox.setSelectedValue(pgf.getOutputLanguage());
|
||||
}
|
||||
}
|
||||
public void onAvailableCategoriesChanged() { }
|
||||
public void onStartCategoryChanged() { }
|
||||
public void onAvailableFunctionsChanged() { }
|
||||
public void onSettingsError(String msg, Throwable e) { }
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ public class TranslateApp implements EntryPoint {
|
||||
|
||||
protected SuggestPanel suggestPanel;
|
||||
protected VerticalPanel outputPanel;
|
||||
protected Widget translatePanel;
|
||||
protected BrowsePanel browsePanel;
|
||||
protected StatusPopup statusPopup;
|
||||
|
||||
//
|
||||
@@ -201,12 +203,28 @@ public class TranslateApp implements EntryPoint {
|
||||
//
|
||||
|
||||
protected Widget createUI() {
|
||||
translatePanel = createTranslatePanel();
|
||||
browsePanel = createBrowsePanel();
|
||||
|
||||
VerticalPanel vPanel = new VerticalPanel();
|
||||
|
||||
HorizontalPanel hPanel = new HorizontalPanel();
|
||||
hPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
|
||||
hPanel.setStylePrimaryName("my-HeaderPanel");
|
||||
|
||||
Widget linksPanel = createLinksPanel(vPanel);
|
||||
hPanel.add(linksPanel);
|
||||
hPanel.setCellHorizontalAlignment(linksPanel,HorizontalPanel.ALIGN_LEFT);
|
||||
|
||||
Widget settingsPanel = createSettingsPanel();
|
||||
hPanel.add(settingsPanel);
|
||||
hPanel.setCellHorizontalAlignment(settingsPanel,HorizontalPanel.ALIGN_RIGHT);
|
||||
|
||||
vPanel.setWidth("100%");
|
||||
vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
|
||||
vPanel.add(createSuggestPanel());
|
||||
vPanel.add(createSettingsPanel());
|
||||
vPanel.add(createTranslationsPanel());
|
||||
vPanel.add(hPanel);
|
||||
vPanel.add(translatePanel);
|
||||
|
||||
return vPanel;
|
||||
}
|
||||
|
||||
@@ -231,6 +249,43 @@ public class TranslateApp implements EntryPoint {
|
||||
return outputPanel;
|
||||
}
|
||||
|
||||
protected Widget createTranslatePanel() {
|
||||
VerticalPanel translatePanel = new VerticalPanel();
|
||||
translatePanel.add(createSuggestPanel());
|
||||
translatePanel.add(createTranslationsPanel());
|
||||
return translatePanel;
|
||||
}
|
||||
|
||||
protected BrowsePanel createBrowsePanel() {
|
||||
return new BrowsePanel(pgf);
|
||||
}
|
||||
|
||||
protected Widget createLinksPanel(final Panel parent) {
|
||||
HorizontalPanel linksPanel = new HorizontalPanel();
|
||||
linksPanel.setStylePrimaryName("my-LinksPanel");
|
||||
|
||||
Hyperlink translateLink = new Hyperlink("Translate", null);
|
||||
translateLink.addClickListener(new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
parent.remove(browsePanel);
|
||||
parent.add(translatePanel);
|
||||
}
|
||||
});
|
||||
linksPanel.add(translateLink);
|
||||
|
||||
Hyperlink browseLink = new Hyperlink("Browse", null);
|
||||
browseLink.addClickListener(new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
parent.remove(translatePanel);
|
||||
parent.add(browsePanel);
|
||||
browsePanel.onActivate();
|
||||
}
|
||||
});
|
||||
linksPanel.add(browseLink);
|
||||
|
||||
return linksPanel;
|
||||
}
|
||||
|
||||
protected Widget createLoadingWidget () {
|
||||
VerticalPanel loadingPanel = new VerticalPanel();
|
||||
loadingPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
|
||||
@@ -287,7 +342,7 @@ public class TranslateApp implements EntryPoint {
|
||||
}
|
||||
}
|
||||
}
|
||||
public void onAvailableLanguagesChanged() {
|
||||
public void onSelectedGrammarChanged() {
|
||||
if (pgf.getInputLanguage() == null) {
|
||||
GWT.log("Setting input language to user language: " + pgf.getUserLanguage(), null);
|
||||
pgf.setInputLanguage(pgf.getUserLanguage());
|
||||
@@ -300,13 +355,9 @@ public class TranslateApp implements EntryPoint {
|
||||
public void onOutputLanguageChanged() {
|
||||
update();
|
||||
}
|
||||
public void onAvailableCategoriesChanged() {
|
||||
}
|
||||
public void onStartCategoryChanged() {
|
||||
update();
|
||||
}
|
||||
public void onAvailableFunctionsChanged() {
|
||||
}
|
||||
public void onSettingsError(String msg, Throwable e) {
|
||||
showError(msg,e);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,34 @@
|
||||
margin: 0 0.4em;
|
||||
}
|
||||
|
||||
.my-LinksPanel * {
|
||||
margin: 0 0.1em;
|
||||
}
|
||||
|
||||
.my-HeaderPanel {
|
||||
width: 100%;
|
||||
margin: 0 0.1em;
|
||||
padding-top: 2px;
|
||||
padding-bottom: 2px;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
border-bottom-color: rgb(122,165,214);
|
||||
}
|
||||
|
||||
.my-BrowsePanel {
|
||||
width: 100%;
|
||||
margin: 1em;
|
||||
border-width: 5px;
|
||||
border-color: rgb(122,165,214);
|
||||
}
|
||||
|
||||
.my-BrowseFrame {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
margin: 1em;
|
||||
border-style:none;
|
||||
}
|
||||
|
||||
.my-translations {
|
||||
margin-top: 1em;
|
||||
}
|
||||
@@ -59,6 +87,17 @@
|
||||
height: 300px;
|
||||
}
|
||||
|
||||
.my-identifierLink:link {
|
||||
text-decoration: none;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.my-identifierLink:hover {
|
||||
text-decoration: none;
|
||||
color: black;
|
||||
background-color: silver;
|
||||
}
|
||||
|
||||
/*
|
||||
* [LANG=bg] { background-image: url("flags/bg.png"); }
|
||||
* [LANG=ca] { background-image: url("flags/catalonia.png"); }
|
||||
|
||||
Reference in New Issue
Block a user