Better error handling in GWT UI.

This commit is contained in:
bjorn
2008-09-16 13:53:07 +00:00
parent 0e9c7629d2
commit 64728d1205
3 changed files with 86 additions and 55 deletions

View File

@@ -2,20 +2,28 @@ package se.chalmers.cs.gf.gwt_translate.client;
import com.google.gwt.user.client.ui.SuggestOracle; import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.core.client.GWT;
import java.util.*; import java.util.*;
public class CompletionOracle extends SuggestOracle { public class CompletionOracle extends SuggestOracle {
private GF gf; private GF gf;
private List<String> inputLangs; private ErrorHandler errorHandler;
private List<String> inputLangs = null;
private GFRequest gfRequest = null; private GFRequest gfRequest = null;
public CompletionOracle (GF gf) { public CompletionOracle (GF gf) {
this(gf, null);
}
public CompletionOracle (GF gf, ErrorHandler errorHandler) {
this.gf = gf; this.gf = gf;
inputLangs = new ArrayList<String>(); this.errorHandler = errorHandler;
} }
public void setInputLangs(List<String> inputLangs) { public void setInputLangs(List<String> inputLangs) {
@@ -26,6 +34,14 @@ public class CompletionOracle extends SuggestOracle {
return inputLangs; return inputLangs;
} }
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public static interface ErrorHandler {
public void onError(Throwable e);
}
public static class CompletionSuggestion implements SuggestOracle.Suggestion { public static class CompletionSuggestion implements SuggestOracle.Suggestion {
private String string; private String string;
public CompletionSuggestion(String string) { public CompletionSuggestion(String string) {
@@ -49,14 +65,19 @@ public class CompletionOracle extends SuggestOracle {
gfRequest = gf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(), gfRequest = gf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(),
new GF.CompleteCallback() { new GF.CompleteCallback() {
public void onCompleteDone(GF.Completions completions) { public void onResult(GF.Completions completions) {
Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>(); Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
for (int i = 0; i < completions.length(); i++) { for (int i = 0; i < completions.length(); i++) {
String text = completions.get(i).getText(); String text = completions.get(i).getText();
suggestions.add(new CompletionSuggestion(text)); suggestions.add(new CompletionSuggestion(text));
} }
callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions)); callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions));
} }
public void onError(Throwable e) {
errorHandler.onError(e);
}
}); });
} }

View File

@@ -21,16 +21,19 @@ public class GF {
this.baseURL = baseURL; this.baseURL = baseURL;
} }
public static interface GFCallback<T extends JavaScriptObject> {
public void onResult (T result) ;
public void onError (Throwable e) ;
}
/* Languages */ /* Languages */
public GFRequest languages (final LanguagesCallback callback) { public GFRequest languages (final LanguagesCallback callback) {
return sendRequest("languages", null, new JSONRequestCallback() { return sendRequest("languages", null, callback);
public void onJSONReceived(JSONValue json) {
callback.onLanguagesDone((Languages)json.isArray().getJavaScriptObject().cast());
}
});
} }
public interface LanguagesCallback extends GFCallback<Languages> { }
public static class Languages extends JsArray<Language> { public static class Languages extends JsArray<Language> {
protected Languages() { } protected Languages() { }
} }
@@ -43,11 +46,6 @@ public class GF {
public final native boolean canParse() /*-{ return this.canParse; }-*/; public final native boolean canParse() /*-{ return this.canParse; }-*/;
} }
public interface LanguagesCallback {
public void onLanguagesDone (Languages languages);
}
/* Translation */ /* Translation */
public GFRequest translate (String input, List<String> fromLangs, String cat, List<String> toLangs, public GFRequest translate (String input, List<String> fromLangs, String cat, List<String> toLangs,
@@ -65,13 +63,11 @@ public class GF {
args.add(new Arg("to", to)); args.add(new Arg("to", to));
} }
} }
return sendRequest("translate", args, new JSONRequestCallback() { return sendRequest("translate", args, callback);
public void onJSONReceived(JSONValue json) {
callback.onTranslateDone((Translations)json.isArray().getJavaScriptObject().cast());
}
});
} }
public interface TranslateCallback extends GFCallback<Translations> { }
public static class Translations extends JsArray<Translation> { public static class Translations extends JsArray<Translation> {
protected Translations() { } protected Translations() { }
} }
@@ -84,10 +80,6 @@ public class GF {
public final native String getText() /*-{ return this.text; }-*/; public final native String getText() /*-{ return this.text; }-*/;
} }
public interface TranslateCallback {
public void onTranslateDone (Translations translations);
}
/* Completion */ /* Completion */
public GFRequest complete (String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) { public GFRequest complete (String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) {
@@ -100,13 +92,11 @@ public class GF {
} }
args.add(new Arg("cat", cat)); args.add(new Arg("cat", cat));
args.add(new Arg("limit", limit)); args.add(new Arg("limit", limit));
return sendRequest("complete", args, new JSONRequestCallback() { return sendRequest("complete", args, callback);
public void onJSONReceived(JSONValue json) {
callback.onCompleteDone((Completions)json.isArray().getJavaScriptObject().cast());
}
});
} }
public interface CompleteCallback extends GFCallback<Completions> { }
public static class Completions extends JsArray<Translation> { public static class Completions extends JsArray<Translation> {
protected Completions() { } protected Completions() { }
} }
@@ -118,44 +108,41 @@ public class GF {
public final native String getText() /*-{ return this.text; }-*/; public final native String getText() /*-{ return this.text; }-*/;
} }
public interface CompleteCallback {
public void onCompleteDone (Completions completions);
}
/* Utilities */ /* Utilities */
public interface JSONRequestCallback { private <T extends JavaScriptObject> GFRequest sendRequest (String resource, List<Arg> vars, final GFCallback<T> callback) {
public void onJSONReceived(JSONValue json);
}
private GFRequest sendRequest (String resource, List<Arg> vars, final JSONRequestCallback callback) {
String url = baseURL + "/" + resource + "?" + buildQueryString(vars); String url = baseURL + "/" + resource + "?" + buildQueryString(vars);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
builder.setTimeoutMillis(30000);
builder.setHeader("Accept","text/plain, text/html;q=0.5, */*;q=0.1"); builder.setHeader("Accept","text/plain, text/html;q=0.5, */*;q=0.1");
Request request = null; Request request = null;
try { try {
request = builder.sendRequest(null, new RequestCallback() { request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable e) { public void onError(Request request, Throwable e) {
GWT.log("onError called", e); callback.onError(e);
} }
public void onResponseReceived(Request request, Response response) { public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) { if (200 == response.getStatusCode()) {
callback.onJSONReceived(JSONParser.parse(response.getText())); callback.onResult((T)eval(response.getText()).cast());
} else { } else {
GWT.log("Response not OK: " + response.getStatusCode() + ". " + response.getText(), null); RequestException e = new RequestException("Response not OK: " + response.getStatusCode() + ". " + response.getText());
callback.onError(e);
} }
} }
}); });
} catch (RequestException e) { } catch (RequestException e) {
GWT.log("Failed to send request", e); callback.onError(e);
} }
return new GFRequest(request); return new GFRequest(request);
} }
private static native JavaScriptObject eval(String json) /*-{
return eval('(' + json + ')');
}-*/;
private static class Arg { private static class Arg {
public final String name; public final String name;
public final String value; public final String value;

View File

@@ -4,6 +4,7 @@ import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener; import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid; import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.Image;
@@ -17,6 +18,8 @@ import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget; import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter; import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window; import com.google.gwt.user.client.Window;
import java.util.ArrayList; import java.util.ArrayList;
@@ -34,6 +37,7 @@ public class Translate implements EntryPoint {
private List<String> fromLangs; private List<String> fromLangs;
private List<String> toLangs; private List<String> toLangs;
private VerticalPanel outputPanel; private VerticalPanel outputPanel;
private Label statusLabel;
private void addTranslation(String text) { private void addTranslation(String text) {
Label l = new Label(text); Label l = new Label(text);
@@ -43,13 +47,16 @@ public class Translate implements EntryPoint {
private void translate() { private void translate() {
gf.translate(suggest.getText(), fromLangs, null, toLangs, new GF.TranslateCallback() { gf.translate(suggest.getText(), fromLangs, null, toLangs, new GF.TranslateCallback() {
public void onTranslateDone (GF.Translations translations) { public void onResult (GF.Translations translations) {
outputPanel.clear(); outputPanel.clear();
for (int i = 0; i < translations.length(); i++) { for (int i = 0; i < translations.length(); i++) {
GF.Translation t = translations.get(i); GF.Translation t = translations.get(i);
addTranslation(t.getText()); addTranslation(t.getText());
} }
} }
public void onError (Throwable e) {
showError("Translation failed", e);
}
}); });
} }
@@ -64,15 +71,26 @@ public class Translate implements EntryPoint {
return l; return l;
} }
private void setStatus(String msg) {
statusLabel.setText(msg);
}
private void showError(String msg, Throwable e) {
GWT.log(msg, e);
setStatus(msg);
}
public void onModuleLoad() { public void onModuleLoad() {
final PopupPanel loading = new PopupPanel(); statusLabel = new Label("Loading...");
loading.add(new Label("Loading..."));
loading.center();
gf = new GF(gfBaseURL); gf = new GF(gfBaseURL);
oracle = new CompletionOracle(gf); oracle = new CompletionOracle(gf, new CompletionOracle.ErrorHandler() {
public void onError(Throwable e) {
showError("Completion failed", e);
}
});
suggest = new SuggestBox(oracle); suggest = new SuggestBox(oracle);
suggest.addKeyboardListener(new KeyboardListenerAdapter() { suggest.addKeyboardListener(new KeyboardListenerAdapter() {
@@ -133,9 +151,10 @@ public class Translate implements EntryPoint {
vPanel.add(outputPanel); vPanel.add(outputPanel);
RootPanel.get().add(vPanel); RootPanel.get().add(vPanel);
RootPanel.get().add(statusLabel, (Window.getClientWidth() - statusLabel.getOffsetWidth())/2, (Window.getClientHeight() - statusLabel.getOffsetHeight()));
gf.languages(new GF.LanguagesCallback() { gf.languages(new GF.LanguagesCallback() {
public void onLanguagesDone(GF.Languages languages) { public void onResult(GF.Languages languages) {
for (int i = 0; i < languages.length(); i++) { for (int i = 0; i < languages.length(); i++) {
GF.Language l = languages.get(i); GF.Language l = languages.get(i);
if (l.canParse()) { if (l.canParse()) {
@@ -144,7 +163,11 @@ public class Translate implements EntryPoint {
toLangBox.addItem(l.getName()); toLangBox.addItem(l.getName());
} }
loading.hide(); setStatus("Loaded languages.");
}
public void onError (Throwable e) {
showError("Error getting language information", e);
} }
}); });