Started working on Google Web Toolkit version of translation server ui.

This commit is contained in:
bjorn
2008-09-12 10:03:12 +00:00
parent 92ffc5072a
commit 45331ffa9c
9 changed files with 290 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
#!/bin/sh
APPDIR=`dirname $0`;
java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:/Users/bringert/src/gwt-mac-1.5.2/gwt-user.jar:/Users/bringert/src/gwt-mac-1.5.2/gwt-dev-mac.jar" com.google.gwt.dev.GWTCompiler -out "$APPDIR/www" "$@" se.chalmers.cs.gf.gwt_translate.Translate;

View File

@@ -0,0 +1,3 @@
#!/bin/sh
APPDIR=`dirname $0`;
java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:/Users/bringert/src/gwt-mac-1.5.2/gwt-user.jar:/Users/bringert/src/gwt-mac-1.5.2/gwt-dev-mac.jar" com.google.gwt.dev.GWTShell -out "$APPDIR/www" "$@" se.chalmers.cs.gf.gwt_translate.Translate/Translate.html;

View File

@@ -0,0 +1,3 @@
#!/bin/sh
APPDIR=`dirname $0`;
java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:/Users/bringert/src/gwt-mac-1.5.2/gwt-user.jar:/Users/bringert/src/gwt-mac-1.5.2/gwt-dev-mac.jar" com.google.gwt.dev.GWTShell -out "$APPDIR/www" -noserver "$@" http://localhost/~bringert/gwt-client/se.chalmers.cs.gf.gwt_translate.Translate/Translate.html;

View File

@@ -0,0 +1,24 @@
<module>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<inherits name="com.google.gwt.http.HTTP" />
<inherits name="com.google.gwt.json.JSON" />
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.standard.Standard'/>
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<!-- Specify the app entry point class. -->
<entry-point class='se.chalmers.cs.gf.gwt_translate.client.Translate'/>
<!-- Specify the application specific style sheet. -->
<stylesheet src='Translate.css' />
</module>

View File

@@ -0,0 +1,46 @@
package se.chalmers.cs.gf.gwt_translate.client;
import com.google.gwt.user.client.ui.SuggestOracle;
import java.util.*;
public class CompletionOracle extends SuggestOracle {
private GF gf;
public CompletionOracle (String gfBaseURL) {
gf = new GF(gfBaseURL);
}
public 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 + " ";
}
}
public void requestSuggestions(final SuggestOracle.Request request, final SuggestOracle.Callback callback) {
gf.complete(request.getQuery(), request.getLimit(), new GF.CompleteCallback() {
public void onCompleteDone(GF.Completions completions) {
Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
for (String lang : completions.getCompletionLanguages()) {
int c = completions.countCompletions(lang);
for (int i = 0; i < c; i++) {
String text = request.getQuery() + " " + completions.getCompletion(lang,i);
suggestions.add(new CompletionSuggestion(text));
}
}
callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions));
}
});
}
}

View File

@@ -0,0 +1,103 @@
package se.chalmers.cs.gf.gwt_translate.client;
import com.google.gwt.http.client.*;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
public class GF {
private String baseURL;
public GF (String baseURL) {
this.baseURL = baseURL;
}
/* Translation */
/* Completion */
public void complete (String text, int count, final CompleteCallback callback) {
Map<String,String> args = new HashMap<String,String>();
args.put("input",text);
// args.put("from",);
// args.put("cat",);
// args.put("count",);
sendRequest("complete", args, new JSONRequestCallback() {
public void onJSONReceived(JSONValue json) {
callback.onCompleteDone(new Completions(json));
}
});
}
public static final class Completions {
private final JSONObject completions;
public Completions (JSONValue json) {
this.completions = json.isObject();
}
public final Set<String> getCompletionLanguages() { return completions.keySet(); }
public final int countCompletions(String lang) {
JSONValue cs = completions.get(lang);
return cs == null ? 0 : cs.isArray().size();
}
public final String getCompletion(String lang, int i) {
JSONValue cs = completions.get(lang);
return cs == null ? null : cs.isArray().get(i).isString().stringValue();
}
}
public interface CompleteCallback {
public void onCompleteDone (Completions completions);
}
/* Utilities */
public interface JSONRequestCallback {
public void onJSONReceived(JSONValue json);
}
private void sendRequest (String resource, Map<String,String> vars, final JSONRequestCallback callback) {
String url = baseURL + "/" + resource + "?" + buildQueryString(vars);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable e) {
GWT.log("onError called", e);
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
callback.onJSONReceived(JSONParser.parse(response.getText()));
} else {
GWT.log("Response not OK: " + response.getStatusCode(), null);
}
}
});
} catch (RequestException e) {
GWT.log("Failed to send request", e);
}
}
private static String buildQueryString(Map<String,String> vars) {
StringBuffer sb = new StringBuffer();
for (Map.Entry<String,String> e : vars.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(URL.encodeComponent(e.getKey()));
sb.append("=");
sb.append(URL.encodeComponent(e.getValue()));
}
return sb.toString();
}
}

View File

@@ -0,0 +1,60 @@
package se.chalmers.cs.gf.gwt_translate.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SuggestBox;
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.user.client.Window;
public class Translate implements EntryPoint {
private SuggestBox suggest;
private Grid outputTable;
private void translate() {
outputTable.resizeRows(3);
outputTable.setText(0, 1, suggest.getText());
outputTable.setText(1, 1, suggest.getText());
outputTable.setText(2, 1, suggest.getText());
}
public void onModuleLoad() {
suggest = new SuggestBox(new CompletionOracle("http://localhost/~bringert/gf-server/gf.fcgi"));
suggest.setWidth("50em");
suggest.addKeyboardListener(new KeyboardListenerAdapter() {
public void onKeyUp (Widget sender, char keyCode, int modifiers) {
if (keyCode == KEY_ENTER) {
translate();
}
}
});
Button translateButton = new Button("Translate");
translateButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
translate();
}
});
outputTable = new Grid(0, 2);
VerticalPanel vPanel = new VerticalPanel();
vPanel.setWidth("100%");
vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
vPanel.add(suggest);
vPanel.add(translateButton);
vPanel.add(outputTable);
RootPanel.get().add(vPanel);
}
}

View File

@@ -0,0 +1,12 @@
/** Add css rules here for your application. */
/** Example rules used by the template application (remove for your app) */
.pc-template-btn {
display: block;
font-size: 16pt
}
#pc-template-img {
margin-top: 20px;
}

View File

@@ -0,0 +1,36 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set -->
<!-- the browser's rendering engine into -->
<!-- "Quirks Mode". Replacing this declaration -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout. -->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Translate</title>
<!-- -->
<!-- This script loads your compiled module. -->
<!-- If you add any GWT meta tags, they must -->
<!-- be added before this line. -->
<!-- -->
<script type="text/javascript" language="javascript" src="se.chalmers.cs.gf.gwt_translate.Translate.nocache.js"></script>
</head>
<!-- -->
<!-- The body can have arbitrary html, or -->
<!-- you can leave the body empty if you want -->
<!-- to create a completely dynamic UI. -->
<!-- -->
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
</body>
</html>