1
0
forked from GitHub/gf-core

improved Web UI for queries

This commit is contained in:
krasimir
2010-10-13 12:31:38 +00:00
parent 8970184578
commit 63ac5f5db6
10 changed files with 146 additions and 153 deletions

View File

@@ -13,13 +13,15 @@ import com.google.gwt.event.logical.shared.*;
public class BrowsePanel extends Composite {
private PGFWrapper pgf;
private StatusPopup statusPopup;
private HTML sourceView;
private SuggestBox searchBox;
private CompletionOracle oracle;
private List<String> identifiers = null;
public BrowsePanel(PGFWrapper pgf) {
public BrowsePanel(PGFWrapper pgf, StatusPopup statusPopup) {
this.pgf = pgf;
this.statusPopup = statusPopup;
oracle = new CompletionOracle();
@@ -79,8 +81,8 @@ public class BrowsePanel extends Composite {
sourceView.setHTML(response.getText());
}
public void onError(Request request, java.lang.Throwable exception) {
// errorHandler.onError(e);
public void onError(Request request, java.lang.Throwable e) {
statusPopup.showError("Cannot load the page", e);
}
});
}

View File

@@ -118,7 +118,7 @@ public class CompletionOracle extends SuggestOracle {
jsonRequest = pgf.complete(request.getQuery(), LIMIT_SCALE_FACTOR * request.getLimit(),
new PGF.CompleteCallback() {
public void onResult(PGF.Completions completions) {
public void onResult(IterableJsArray<PGF.Completion> completions) {
jsonRequest = null;
List<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
for (PGF.Completion completion : completions.iterable()) {

View File

@@ -57,7 +57,7 @@ public class EditorApp implements EntryPoint {
outputPanel.addStyleDependentName("working");
translateRequest = pgf.translate(textPanel.getText(),
new PGF.TranslateCallback() {
public void onResult (PGF.Translations translations) {
public void onResult (IterableJsArray<PGF.TranslationResult> translations) {
translateRequest = null;
outputPanel.clear();
@@ -66,8 +66,8 @@ public class EditorApp implements EntryPoint {
textPanel.renderBracketedString(tr.getBracketedString());
if (tr.getTranslations() != null)
for (PGF.Translation t : tr.getTranslations().iterable()) {
LinearizationsPanel lin = new LinearizationsPanel(pgf, t.getTree(), t.getLinearizations());
for (PGF.Linearizations lins : tr.getTranslations().iterable()) {
LinearizationsPanel lin = new LinearizationsPanel(pgf, lins);
lin.setWidth("100%");
outputPanel.add(lin);
}
@@ -89,7 +89,7 @@ public class EditorApp implements EntryPoint {
public void onError (Throwable e) {
translateRequest = null;
showError("Translation failed", e);
statusPopup.showError("Translation failed", e);
}
});
}
@@ -155,7 +155,7 @@ public class EditorApp implements EntryPoint {
bagPanel.clear();
completeRequest = pgf.complete(query, LIMIT_SCALE_FACTOR * maxMagnets,
new PGF.CompleteCallback() {
public void onResult(PGF.Completions completions) {
public void onResult(IterableJsArray<PGF.Completion> completions) {
completeRequest = null;
cachedPrefix = query;
@@ -188,28 +188,11 @@ public class EditorApp implements EntryPoint {
public void onError(Throwable e) {
completeRequest = null;
showError("Getting completions failed", e);
statusPopup.showError("Getting completions failed", e);
}
});
}
//
// Status stuff
//
protected void setStatus(String msg) {
statusPopup.setStatus(msg);
}
protected void showError(String msg, Throwable e) {
statusPopup.showError(msg, e);
}
protected void clearStatus() {
statusPopup.clearStatus();
}
//
// GUI
//
@@ -313,11 +296,11 @@ public class EditorApp implements EntryPoint {
}
protected BrowsePanel createBrowsePanel() {
return new BrowsePanel(pgf);
return new BrowsePanel(pgf, statusPopup);
}
protected QueryPanel createQueryPanel() {
return new QueryPanel(pgf);
return new QueryPanel(pgf, statusPopup);
}
protected DocumentsPanel createDocumentsPanel() {
@@ -435,7 +418,7 @@ public class EditorApp implements EntryPoint {
update();
}
public void onSettingsError(String msg, Throwable e) {
showError(msg,e);
statusPopup.showError(msg,e);
}
}

View File

@@ -52,7 +52,7 @@ public class FridgeApp implements EntryPoint {
int limit = updatePrefixes ? 0 : maxMagnets;
completeRequest = pgf.complete(text + " " + prefix,
limit, new PGF.CompleteCallback() {
public void onResult(PGF.Completions completions) {
public void onResult(IterableJsArray<PGF.Completion> completions) {
List<Magnet> magnets = new ArrayList<Magnet>();
for (PGF.Completion completion : completions.iterable()) {
for (String word : completion.getCompletions()) {
@@ -105,11 +105,11 @@ public class FridgeApp implements EntryPoint {
}
translateRequest = pgf.translate(getText(),
new PGF.TranslateCallback() {
public void onResult (PGF.Translations translations) {
public void onResult (IterableJsArray<PGF.TranslationResult> translations) {
outputPanel.removeStyleDependentName("working");
for (PGF.TranslationResult tr : translations.iterable()) {
if (tr.getTranslations() != null)
for (PGF.Translation t : tr.getTranslations().iterable()) {
for (PGF.Linearizations t : tr.getTranslations().iterable()) {
for (PGF.Linearization l : t.getLinearizations().iterable()) {
outputPanel.add(createTranslation(l.getTo(), l.getText()));
}

View File

@@ -13,7 +13,7 @@ public class LinearizationsPanel extends Composite {
protected PGFWrapper pgf;
public LinearizationsPanel(PGFWrapper pgf, String tree, PGF.Linearizations lins) {
public LinearizationsPanel(PGFWrapper pgf, PGF.Linearizations lins) {
this.pgf = pgf;
HorizontalPanel hPanel = new HorizontalPanel();
@@ -23,13 +23,13 @@ public class LinearizationsPanel extends Composite {
HorizontalPanel btnPanel = new HorizontalPanel();
btnPanel.addStyleName("my-translation-btns");
btnPanel.setSpacing(4);
btnPanel.add(createAbsTreeButton(tree));
btnPanel.add(createAlignButton(tree));
btnPanel.add(createAbsTreeButton(lins.getTree()));
btnPanel.add(createAlignButton(lins.getTree()));
hPanel.add(btnPanel);
hPanel.setCellHorizontalAlignment(btnPanel,HasHorizontalAlignment.ALIGN_RIGHT);
for (PGF.Linearization l : lins.iterable()) {
linsPanel.add(createTranslation(l.getTo(), tree, l.getText()));
for (PGF.Linearization l : lins.getLinearizations().iterable()) {
linsPanel.add(createTranslation(l.getTo(), lins.getTree(), l.getText()));
}
initWidget(hPanel);

View File

@@ -52,26 +52,22 @@ public class PGF {
return sendGrammarRequest(pgfURL, "translate", args, callback);
}
public interface TranslateCallback extends JSONCallback<Translations> { }
public static class Translations extends IterableJsArray<TranslationResult> {
protected Translations() { }
}
public interface TranslateCallback extends JSONCallback<IterableJsArray<TranslationResult>> { }
public static class TranslationResult extends JavaScriptObject {
protected TranslationResult() { }
public final native String getFrom() /*-{ return this.from; }-*/;
public final native BracketedString getBracketedString() /*-{ return this.brackets; }-*/;
public final native IterableJsArray<Translation> getTranslations() /*-{ return this.translations; }-*/;
public final native IterableJsArray<Linearizations> getTranslations() /*-{ return this.translations; }-*/;
public final native TcError[] getTypeErrors() /*-{ return this.typeErrors; }-*/;
}
public static class Translation extends JavaScriptObject {
protected Translation() { }
public static class Linearizations extends JavaScriptObject {
protected Linearizations() { }
public final native String getTree() /*-{ return this.tree; }-*/;
public final native Linearizations getLinearizations() /*-{ return this.linearizations; }-*/;
public final native IterableJsArray<Linearization> getLinearizations() /*-{ return this.linearizations; }-*/;
}
/* Completion */
@@ -92,11 +88,7 @@ public class PGF {
return sendGrammarRequest(pgfURL, "complete", args, callback);
}
public interface CompleteCallback extends JSONCallback<Completions> { }
public static class Completions extends IterableJsArray<Completion> {
protected Completions() { }
}
public interface CompleteCallback extends JSONCallback<IterableJsArray<Completion>> { }
public static class Completion extends JavaScriptObject {
protected Completion() { }
@@ -117,11 +109,7 @@ public class PGF {
return sendGrammarRequest(pgfURL, "parse", args, callback);
}
public interface ParseCallback extends JSONCallback<ParseResults> { }
public static class ParseResults extends IterableJsArray<ParseResult> {
protected ParseResults() { }
}
public interface ParseCallback extends JSONCallback<IterableJsArray<ParseResult>> { }
public static class ParseResult extends JavaScriptObject {
protected ParseResult() { }
@@ -174,11 +162,7 @@ public class PGF {
return sendGrammarRequest(pgfURL, "linearize", args, callback);
}
public interface LinearizeCallback extends JSONCallback<Linearizations> { }
public static class Linearizations extends IterableJsArray<Linearization> {
protected Linearizations() { }
}
public interface LinearizeCallback extends JSONCallback<IterableJsArray<Linearization>> { }
public static class Linearization extends JavaScriptObject {
protected Linearization() { }
@@ -229,20 +213,26 @@ public class PGF {
return request;
}
public JSONRequest query(String pgfURL, String query, QueryCallback callback) {
public JSONRequest generateAll(String pgfURL, String cat, int depth, int limit, String toLang, GenerationCallback callback) {
List<Arg> args = new ArrayList<Arg>();
args.add(new Arg("cat", query));
return sendGrammarRequest(pgfURL, "query", args, callback);
args.add(new Arg("cat", cat));
args.add(new Arg("depth", depth));
args.add(new Arg("limit", limit));
args.add(new Arg("to", toLang));
return sendGrammarRequest(pgfURL, "generate", args, callback);
}
public interface QueryCallback extends JSONCallback<QueryResult> {}
public static class QueryResult extends JavaScriptObject {
protected QueryResult() { }
public final native String[] getRows() /*-{ return this.rows; }-*/;
public JSONRequest generateRandom(String pgfURL, String cat, int depth, int limit, String toLang, GenerationCallback callback) {
List<Arg> args = new ArrayList<Arg>();
args.add(new Arg("cat", cat));
args.add(new Arg("depth", depth));
args.add(new Arg("limit", limit));
args.add(new Arg("to", toLang));
return sendGrammarRequest(pgfURL, "random", args, callback);
}
public interface GenerationCallback extends JSONCallback<IterableJsArray<Linearizations>> {}
public <T extends JavaScriptObject> JSONRequest sendGrammarRequest(String pgfURL, String resource, List<Arg> args, final JSONCallback<T> callback) {
args.add(new Arg("command", resource));
return JSONRequestBuilder.sendRequest(pgfURL, args, callback);

View File

@@ -158,8 +158,12 @@ public class PGFWrapper {
return pgf.browse(grammarURL, id, href, cssClass, callback);
}
public JSONRequest query(String query, PGF.QueryCallback callback) {
return pgf.query(grammarURL, query, callback);
public JSONRequest generateAll(String cat, int depth, int limit, PGF.GenerationCallback callback) {
return pgf.generateAll(grammarURL, cat, depth, limit, outputLanguage, callback);
}
public JSONRequest generateRandom(String cat, int depth, int limit, PGF.GenerationCallback callback) {
return pgf.generateRandom(grammarURL, cat, depth, limit, outputLanguage, callback);
}
//

View File

@@ -8,9 +8,14 @@ import com.google.gwt.user.client.ui.*;
public class QueryPanel extends Composite {
private PGFWrapper pgf;
private StatusPopup statusPopup;
private TextArea queryBox;
private VerticalPanel outputPanel;
private JSONRequest executeRequest = null;
public QueryPanel(PGFWrapper pgf) {
public QueryPanel(PGFWrapper pgf, StatusPopup statusPopup) {
this.pgf = pgf;
this.statusPopup = statusPopup;
VerticalPanel vPanel = new VerticalPanel();
vPanel.add(createQueryPanel());
@@ -18,61 +23,46 @@ public class QueryPanel extends Composite {
initWidget(vPanel);
setStylePrimaryName("my-QueryPanel");
pgf.addSettingsListener(new MySettingsListener(pgf));
pgf.addSettingsListener(new MySettingsListener());
}
protected Widget createQueryPanel() {
final TextArea queryBox = new TextArea();
queryBox = new TextArea();
queryBox.setStylePrimaryName("my-QueryBox");
queryBox.setTitle("Goal category");
HorizontalPanel boxPanel = new HorizontalPanel();
boxPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
boxPanel.setSpacing(5);
final Grid resultGrid = new Grid(0, 1);
resultGrid.setStylePrimaryName("my-ResultGrid");
resultGrid.setBorderWidth(3);
final TextBox limitBox = new TextBox();
limitBox.setTitle("Upper limit of the number of examples generated");
limitBox.setWidth("5em");
limitBox.setText("10");
boxPanel.add(new Label("limit:"));
boxPanel.add(limitBox);
boxPanel.add(new HTML(""));
final TextBox depthBox = new TextBox();
depthBox.setTitle("Maximal depth for every example");
depthBox.setWidth("5em");
depthBox.setText("4");
boxPanel.add(new Label("depth:"));
boxPanel.add(depthBox);
boxPanel.add(new HTML(""));
final CheckBox randomBox = new CheckBox();
randomBox.setTitle("random/exhaustive generation");
randomBox.setText("random");
boxPanel.add(randomBox);
outputPanel = new VerticalPanel();
outputPanel.addStyleName("my-translations");
outputPanel.addStyleDependentName("working");
Button execButton = new Button("Execute");
execButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
pgf.query(queryBox.getText(), new PGF.QueryCallback() {
public void onResult(PGF.QueryResult result) {
while (resultGrid.getRowCount() > 0) {
resultGrid.removeRow(resultGrid.getRowCount() - 1);
}
ClickListener labelClickListener = new ClickListener() {
public void onClick(Widget sender) {
final Label label = (Label) sender;
pgf.linearize(label.getText(), new PGF.LinearizeCallback() {
public void onResult(PGF.Linearizations result) {
final PopupPanel popup = new PopupPanel(true);
popup.setWidget(new LinearizationsPanel(pgf, label.getText(), result));
popup.setPopupPosition(label.getAbsoluteLeft(),
label.getAbsoluteTop()+label.getOffsetHeight());
popup.show();
}
public void onError(Throwable e) {
}
});
}
};
int row = 0;
for (String tree : result.getRows()) {
Label label = new Label(tree);
label.addClickListener(labelClickListener);
resultGrid.insertRow(row);
resultGrid.setWidget(row, 0, label);
row++;
}
}
public void onError(Throwable e) {
}
});
}
});
DecoratorPanel queryDecorator = new DecoratorPanel();
VerticalPanel vPanel = new VerticalPanel();
@@ -81,26 +71,70 @@ public class QueryPanel extends Composite {
hPanel.add(queryBox);
hPanel.add(execButton);
vPanel.add(hPanel);
vPanel.add(boxPanel);
queryDecorator.add(vPanel);
VerticalPanel queryPanel = new VerticalPanel();
queryPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
queryPanel.add(queryDecorator);
queryPanel.add(resultGrid);
queryPanel.add(outputPanel);
execButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if (executeRequest != null) {
executeRequest.cancel();
}
PGF.GenerationCallback callback = new PGF.GenerationCallback() {
public void onResult(IterableJsArray<PGF.Linearizations> result) {
executeRequest = null;
outputPanel.clear();
outputPanel.removeStyleDependentName("working");
for (PGF.Linearizations lins : result.iterable()) {
LinearizationsPanel lin = new LinearizationsPanel(pgf, lins);
lin.setWidth("100%");
outputPanel.add(lin);
}
}
public void onError(Throwable e) {
executeRequest = null;
statusPopup.showError("The execution failed", e);
}
};
int depth, limit;
try {
depth = Integer.parseInt(depthBox.getText());
limit = Integer.parseInt(limitBox.getText());
} catch (NumberFormatException e) {
return;
}
if (randomBox.getValue())
executeRequest = pgf.generateRandom(queryBox.getText(), depth, limit, callback);
else
executeRequest = pgf.generateAll(queryBox.getText(), depth, limit, callback);
}
});
return queryPanel;
}
protected class MySettingsListener implements PGFWrapper.SettingsListener {
private PGFWrapper pgf;
public MySettingsListener(PGFWrapper pgf) {
this.pgf = pgf;
public MySettingsListener() {
}
public void onAvailableGrammarsChanged() { }
public void onSelectedGrammarChanged() { }
public void onSelectedGrammarChanged() {
queryBox.setText("");
outputPanel.clear();
}
public void onInputLanguageChanged() { }
public void onOutputLanguageChanged() { }
public void onStartCategoryChanged() { }

View File

@@ -38,31 +38,15 @@ public class TranslateApp implements EntryPoint {
outputPanel.addStyleDependentName("working");
pgf.translate(getText(),
new PGF.TranslateCallback() {
public void onResult (PGF.Translations translations) {
public void onResult (IterableJsArray<PGF.TranslationResult> translations) {
outputPanel.clear();
outputPanel.removeStyleDependentName("working");
for (PGF.TranslationResult tr : translations.iterable()) {
if (tr.getTranslations() != null)
for (PGF.Translation t : tr.getTranslations().iterable()) {
HorizontalPanel hPanel = new HorizontalPanel();
hPanel.addStyleName("my-translation-frame");
VerticalPanel linsPanel = new VerticalPanel();
linsPanel.addStyleName("my-translation-bar");
hPanel.add(linsPanel);
HorizontalPanel btnPanel = new HorizontalPanel();
btnPanel.addStyleName("my-translation-btns");
btnPanel.setSpacing(4);
btnPanel.add(createAbsTreeButton(t.getTree()));
btnPanel.add(createAlignButton(t.getTree()));
hPanel.add(btnPanel);
hPanel.setCellHorizontalAlignment(btnPanel,
HasHorizontalAlignment.ALIGN_RIGHT);
outputPanel.add(hPanel);
for (PGF.Linearization l : t.getLinearizations().iterable()) {
linsPanel.add(createTranslation(l.getTo(), t.getTree(), l.getText()));
}
if (tr.getTranslations() != null) {
for (PGF.Linearizations t : tr.getTranslations().iterable()) {
outputPanel.add(new LinearizationsPanel(pgf, t));
}
}
if (tr.getTypeErrors() != null && tr.getTypeErrors().length > 0) {
for (PGF.TcError error : tr.getTypeErrors()) {

View File

@@ -200,10 +200,6 @@
min-height: 94px;
}
.my-ResultGrid {
margin: 1em;
}
.my-translations {
margin-top: 1em;
}