Better error handling in GWT UI.

This commit is contained in:
bjorn
2008-09-16 13:53:07 +00:00
parent c09783e604
commit 5c2eae8abd
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.core.client.GWT;
import java.util.*;
public class CompletionOracle extends SuggestOracle {
private GF gf;
private List<String> inputLangs;
private ErrorHandler errorHandler;
private List<String> inputLangs = null;
private GFRequest gfRequest = null;
public CompletionOracle (GF gf) {
this(gf, null);
}
public CompletionOracle (GF gf, ErrorHandler errorHandler) {
this.gf = gf;
inputLangs = new ArrayList<String>();
this.errorHandler = errorHandler;
}
public void setInputLangs(List<String> inputLangs) {
@@ -26,6 +34,14 @@ public class CompletionOracle extends SuggestOracle {
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 {
private String string;
public CompletionSuggestion(String string) {
@@ -49,14 +65,19 @@ public class CompletionOracle extends SuggestOracle {
gfRequest = gf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(),
new GF.CompleteCallback() {
public void onCompleteDone(GF.Completions completions) {
Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
for (int i = 0; i < completions.length(); i++) {
String text = completions.get(i).getText();
suggestions.add(new CompletionSuggestion(text));
}
callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions));
}
public void onResult(GF.Completions completions) {
Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
for (int i = 0; i < completions.length(); i++) {
String text = completions.get(i).getText();
suggestions.add(new CompletionSuggestion(text));
}
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;
}
public static interface GFCallback<T extends JavaScriptObject> {
public void onResult (T result) ;
public void onError (Throwable e) ;
}
/* Languages */
public GFRequest languages (final LanguagesCallback callback) {
return sendRequest("languages", null, new JSONRequestCallback() {
public void onJSONReceived(JSONValue json) {
callback.onLanguagesDone((Languages)json.isArray().getJavaScriptObject().cast());
}
});
return sendRequest("languages", null, callback);
}
public interface LanguagesCallback extends GFCallback<Languages> { }
public static class Languages extends JsArray<Language> {
protected Languages() { }
}
@@ -43,11 +46,6 @@ public class GF {
public final native boolean canParse() /*-{ return this.canParse; }-*/;
}
public interface LanguagesCallback {
public void onLanguagesDone (Languages languages);
}
/* Translation */
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));
}
}
return sendRequest("translate", args, new JSONRequestCallback() {
public void onJSONReceived(JSONValue json) {
callback.onTranslateDone((Translations)json.isArray().getJavaScriptObject().cast());
}
});
return sendRequest("translate", args, callback);
}
public interface TranslateCallback extends GFCallback<Translations> { }
public static class Translations extends JsArray<Translation> {
protected Translations() { }
}
@@ -84,10 +80,6 @@ public class GF {
public final native String getText() /*-{ return this.text; }-*/;
}
public interface TranslateCallback {
public void onTranslateDone (Translations translations);
}
/* Completion */
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("limit", limit));
return sendRequest("complete", args, new JSONRequestCallback() {
public void onJSONReceived(JSONValue json) {
callback.onCompleteDone((Completions)json.isArray().getJavaScriptObject().cast());
}
});
return sendRequest("complete", args, callback);
}
public interface CompleteCallback extends GFCallback<Completions> { }
public static class Completions extends JsArray<Translation> {
protected Completions() { }
}
@@ -118,44 +108,41 @@ public class GF {
public final native String getText() /*-{ return this.text; }-*/;
}
public interface CompleteCallback {
public void onCompleteDone (Completions completions);
}
/* Utilities */
public interface JSONRequestCallback {
public void onJSONReceived(JSONValue json);
}
private GFRequest sendRequest (String resource, List<Arg> vars, final JSONRequestCallback callback) {
private <T extends JavaScriptObject> GFRequest sendRequest (String resource, List<Arg> vars, final GFCallback<T> callback) {
String url = baseURL + "/" + resource + "?" + buildQueryString(vars);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
builder.setTimeoutMillis(30000);
builder.setHeader("Accept","text/plain, text/html;q=0.5, */*;q=0.1");
Request request = null;
try {
request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable e) {
GWT.log("onError called", e);
callback.onError(e);
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
callback.onJSONReceived(JSONParser.parse(response.getText()));
callback.onResult((T)eval(response.getText()).cast());
} 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) {
GWT.log("Failed to send request", e);
callback.onError(e);
}
return new GFRequest(request);
}
private static native JavaScriptObject eval(String json) /*-{
return eval('(' + json + ')');
}-*/;
private static class Arg {
public final String name;
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.ChangeListener;
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.Grid;
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.KeyboardListenerAdapter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import java.util.ArrayList;
@@ -34,6 +37,7 @@ public class Translate implements EntryPoint {
private List<String> fromLangs;
private List<String> toLangs;
private VerticalPanel outputPanel;
private Label statusLabel;
private void addTranslation(String text) {
Label l = new Label(text);
@@ -43,13 +47,16 @@ public class Translate implements EntryPoint {
private void translate() {
gf.translate(suggest.getText(), fromLangs, null, toLangs, new GF.TranslateCallback() {
public void onTranslateDone (GF.Translations translations) {
public void onResult (GF.Translations translations) {
outputPanel.clear();
for (int i = 0; i < translations.length(); i++) {
GF.Translation t = translations.get(i);
addTranslation(t.getText());
}
}
public void onError (Throwable e) {
showError("Translation failed", e);
}
});
}
@@ -64,15 +71,26 @@ public class Translate implements EntryPoint {
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() {
final PopupPanel loading = new PopupPanel();
loading.add(new Label("Loading..."));
loading.center();
statusLabel = new Label("Loading...");
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.addKeyboardListener(new KeyboardListenerAdapter() {
@@ -133,9 +151,10 @@ public class Translate implements EntryPoint {
vPanel.add(outputPanel);
RootPanel.get().add(vPanel);
RootPanel.get().add(statusLabel, (Window.getClientWidth() - statusLabel.getOffsetWidth())/2, (Window.getClientHeight() - statusLabel.getOffsetHeight()));
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++) {
GF.Language l = languages.get(i);
if (l.canParse()) {
@@ -144,7 +163,11 @@ public class Translate implements EntryPoint {
toLangBox.addItem(l.getName());
}
loading.hide();
setStatus("Loaded languages.");
}
public void onError (Throwable e) {
showError("Error getting language information", e);
}
});