forked from GitHub/gf-core
Added Eclipse project stuff for the GWT library. Some refactoring of the GWT code.
This commit is contained in:
@@ -10,7 +10,10 @@ gwt:
|
||||
gwt/Translate-compile
|
||||
|
||||
gf-gwt.jar:
|
||||
mkdir -p gwt/bin/se/chalmers/cs/gf/gwt/client
|
||||
javac -classpath $(GWT_CLASSPATH) -sourcepath gwt/src -d gwt/bin gwt/src/se/chalmers/cs/gf/gwt/client/*.java
|
||||
jar -cf $@ -C gwt/src se
|
||||
jar -uf $@ -C gwt/bin se
|
||||
cp $@ ../../lib/java
|
||||
|
||||
food.pgf:
|
||||
|
||||
8
src/server/gwt/.classpath
Normal file
8
src/server/gwt/.classpath
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="test"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="/Users/bringert/src/gwt-mac-1.5.3/gwt-user.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
15
src/server/gwt/.project
Normal file
15
src/server/gwt/.project
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<projectDescription>
|
||||
<name>GF-GWT</name>
|
||||
<comment>GF-GWT project</comment>
|
||||
<projects/>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments/>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
16
src/server/gwt/TranslateApp.launch
Normal file
16
src/server/gwt/TranslateApp.launch
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.gwt.dev.GWTShell"/>
|
||||
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
|
||||
<listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry containerPath="org.eclipse.jdt.launching.JRE_CONTAINER" javaProject="GF-GWT" path="1" type="4"/> "/>
|
||||
<listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry internalArchive="/GF-GWT/src" path="3" type="2"/> "/>
|
||||
<listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry id="org.eclipse.jdt.launching.classpathentry.defaultClasspath"> <memento project="GF-GWT"/> </runtimeClasspathEntry> "/>
|
||||
<listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry externalArchive="/Users/bringert/src/gwt-mac-1.5.3/gwt-dev-mac.jar" path="3" type="2"/> "/>
|
||||
|
||||
</listAttribute>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-XstartOnFirstThread -Xmx256M"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-out www -noserver http://localhost:41296/gwt/www/se.chalmers.cs.gf.gwt.TranslateApp/Translate.html"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="GF-GWT"/>
|
||||
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
|
||||
</launchConfiguration>
|
||||
13
src/server/gwt/src/se/chalmers/cs/gf/gwt/client/Arg.java
Normal file
13
src/server/gwt/src/se/chalmers/cs/gf/gwt/client/Arg.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
public class Arg {
|
||||
public final String name;
|
||||
public final String value;
|
||||
public Arg (String name, String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
public Arg (String name, int value) {
|
||||
this(name, Integer.toString(value));
|
||||
}
|
||||
}
|
||||
@@ -2,83 +2,81 @@ package se.chalmers.cs.gf.gwt.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 PGF pgf;
|
||||
private PGF pgf;
|
||||
|
||||
private ErrorHandler errorHandler;
|
||||
private ErrorHandler errorHandler;
|
||||
|
||||
private List<String> inputLangs = null;
|
||||
private List<String> inputLangs = null;
|
||||
|
||||
private PGFRequest pgfRequest = null;
|
||||
private JSONRequest JSONRequest = null;
|
||||
|
||||
|
||||
public CompletionOracle (PGF pgf) {
|
||||
this(pgf, null);
|
||||
}
|
||||
|
||||
public CompletionOracle (PGF pgf, ErrorHandler errorHandler) {
|
||||
this.pgf = pgf;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
public void setInputLangs(List<String> inputLangs) {
|
||||
this.inputLangs = inputLangs;
|
||||
}
|
||||
|
||||
public List<String> getInputLangs() {
|
||||
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) {
|
||||
this.string = string;
|
||||
public CompletionOracle (PGF pgf) {
|
||||
this(pgf, null);
|
||||
}
|
||||
|
||||
public String getDisplayString() {
|
||||
return string;
|
||||
public CompletionOracle (PGF pgf, ErrorHandler errorHandler) {
|
||||
this.pgf = pgf;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
public String getReplacementString() {
|
||||
return string;
|
||||
public void setInputLangs(List<String> inputLangs) {
|
||||
this.inputLangs = inputLangs;
|
||||
}
|
||||
}
|
||||
|
||||
public void requestSuggestions(final SuggestOracle.Request request, final SuggestOracle.Callback callback) {
|
||||
public List<String> getInputLangs() {
|
||||
return inputLangs;
|
||||
}
|
||||
|
||||
// only allow a single completion request at a time
|
||||
if (pgfRequest != null)
|
||||
pgfRequest.cancel();
|
||||
public void setErrorHandler(ErrorHandler errorHandler) {
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
pgfRequest = pgf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(),
|
||||
new PGF.CompleteCallback() {
|
||||
public static interface ErrorHandler {
|
||||
public void onError(Throwable e);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
// only allow a single completion request at a time
|
||||
if (JSONRequest != null)
|
||||
JSONRequest.cancel();
|
||||
|
||||
JSONRequest = pgf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(),
|
||||
new PGF.CompleteCallback() {
|
||||
public void onResult(PGF.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));
|
||||
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);
|
||||
errorHandler.onError(e);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
import com.google.gwt.core.client.JsArray;
|
||||
|
||||
public class IterableJsArray<T extends JavaScriptObject> extends JsArray<T> {
|
||||
|
||||
protected IterableJsArray() {}
|
||||
|
||||
public final Iterable<T> iterable() {
|
||||
return new Iterable<T>() {
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
private int i = 0;
|
||||
public boolean hasNext() {
|
||||
return i < length();
|
||||
}
|
||||
public T next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return get(i++);
|
||||
}
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
|
||||
public interface JSONCallback<T extends JavaScriptObject> {
|
||||
public void onResult (T result) ;
|
||||
public void onError (Throwable e) ;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import com.google.gwt.http.client.*;
|
||||
|
||||
public class JSONRequest {
|
||||
|
||||
private Request httpRequest;
|
||||
|
||||
JSONRequest (Request httpRequest) {
|
||||
this.httpRequest = httpRequest;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
if (httpRequest != null) {
|
||||
httpRequest.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
import com.google.gwt.http.client.Request;
|
||||
import com.google.gwt.http.client.RequestBuilder;
|
||||
import com.google.gwt.http.client.RequestCallback;
|
||||
import com.google.gwt.http.client.RequestException;
|
||||
import com.google.gwt.http.client.Response;
|
||||
import com.google.gwt.http.client.URL;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JSONRequestBuilder {
|
||||
|
||||
public static <T extends JavaScriptObject> JSONRequest sendRequest (String base, List<Arg> vars, final JSONCallback<T> callback) {
|
||||
String url = base + "?" + 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) {
|
||||
callback.onError(e);
|
||||
}
|
||||
|
||||
public void onResponseReceived(Request request, Response response) {
|
||||
if (200 == response.getStatusCode()) {
|
||||
callback.onResult((T)eval(response.getText()).cast());
|
||||
} else {
|
||||
RequestException e = new RequestException("Response not OK: " + response.getStatusCode() + ". " + response.getText());
|
||||
callback.onError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (RequestException e) {
|
||||
callback.onError(e);
|
||||
}
|
||||
|
||||
return new JSONRequest(request);
|
||||
}
|
||||
|
||||
private static native JavaScriptObject eval(String json) /*-{
|
||||
return eval('(' + json + ')');
|
||||
}-*/;
|
||||
|
||||
private static String buildQueryString(List<Arg> args) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (args != null) {
|
||||
for (Arg arg : args) {
|
||||
if (arg.value != null) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append("&");
|
||||
}
|
||||
sb.append(URL.encodeComponent(arg.name));
|
||||
sb.append("=");
|
||||
sb.append(URL.encodeComponent(arg.value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,248 +1,149 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import com.google.gwt.http.client.*;
|
||||
import com.google.gwt.core.client.GWT;
|
||||
import com.google.gwt.core.client.JsArray;
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
|
||||
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.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class PGF {
|
||||
|
||||
private String baseURL;
|
||||
private String pgfName;
|
||||
private String baseURL;
|
||||
private String pgfName;
|
||||
|
||||
public PGF (String baseURL, String pgfName) {
|
||||
this.baseURL = baseURL;
|
||||
this.pgfName = pgfName;
|
||||
}
|
||||
public PGF (String baseURL, String pgfName) {
|
||||
this.baseURL = baseURL;
|
||||
this.pgfName = pgfName;
|
||||
}
|
||||
|
||||
public static interface GFCallback<T extends JavaScriptObject> {
|
||||
public void onResult (T result) ;
|
||||
public void onError (Throwable e) ;
|
||||
}
|
||||
/* Grammar */
|
||||
|
||||
public static class IterableJsArray<T extends JavaScriptObject> extends JsArray<T> {
|
||||
protected IterableJsArray() {}
|
||||
public JSONRequest grammar (final GrammarCallback callback) {
|
||||
return sendRequest("grammar", null, callback);
|
||||
}
|
||||
|
||||
public final Iterable<T> iterable() {
|
||||
return new Iterable<T>() {
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
private int i = 0;
|
||||
public boolean hasNext() {
|
||||
return i < length();
|
||||
public interface GrammarCallback extends JSONCallback<Grammar> { }
|
||||
|
||||
public static class Grammar extends JavaScriptObject {
|
||||
protected Grammar() { }
|
||||
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
|
||||
public final native String getUserLanguage() /*-{ return this.userLanguage; }-*/;
|
||||
|
||||
public final native IterableJsArray<Language> getLanguages() /*-{ return this.languages; }-*/;
|
||||
|
||||
public final Language getLanguage(String name) {
|
||||
int c = getLanguages().length();
|
||||
for (int i = 0; i < c; i++) {
|
||||
Language l = getLanguages().get(i);
|
||||
if (l.getName().equals(name))
|
||||
return l;
|
||||
}
|
||||
public T next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return get(i++);
|
||||
}
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/* Grammar */
|
||||
public static class Language extends JavaScriptObject {
|
||||
protected Language() { }
|
||||
|
||||
public PGFRequest grammar (final GrammarCallback callback) {
|
||||
return sendRequest("grammar", null, callback);
|
||||
}
|
||||
|
||||
public interface GrammarCallback extends GFCallback<Grammar> { }
|
||||
|
||||
public static class Grammar extends JavaScriptObject {
|
||||
protected Grammar() { }
|
||||
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
|
||||
public final native String getUserLanguage() /*-{ return this.userLanguage; }-*/;
|
||||
|
||||
public final native IterableJsArray<Language> getLanguages() /*-{ return this.languages; }-*/;
|
||||
|
||||
public final Language getLanguage(String name) {
|
||||
int c = getLanguages().length();
|
||||
for (int i = 0; i < c; i++) {
|
||||
Language l = getLanguages().get(i);
|
||||
if (l.getName().equals(name))
|
||||
return l;
|
||||
}
|
||||
return null;
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
public final native String getLanguageCode() /*-{ return this.languageCode; }-*/;
|
||||
public final native boolean canParse() /*-{ return this.canParse; }-*/;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Language extends JavaScriptObject {
|
||||
protected Language() { }
|
||||
/* Translation */
|
||||
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
public final native String getLanguageCode() /*-{ return this.languageCode; }-*/;
|
||||
public final native boolean canParse() /*-{ return this.canParse; }-*/;
|
||||
}
|
||||
|
||||
/* Translation */
|
||||
|
||||
public PGFRequest translate (String input, List<String> fromLangs, String cat, List<String> toLangs,
|
||||
final TranslateCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
args.add(new Arg("cat", cat));
|
||||
if (toLangs != null) {
|
||||
for (String to : toLangs) {
|
||||
args.add(new Arg("to", to));
|
||||
}
|
||||
}
|
||||
return sendRequest("translate", args, callback);
|
||||
}
|
||||
|
||||
public interface TranslateCallback extends GFCallback<Translations> { }
|
||||
|
||||
public static class Translations extends IterableJsArray<Translation> {
|
||||
protected Translations() { }
|
||||
}
|
||||
|
||||
public static class Translation extends JavaScriptObject {
|
||||
protected Translation() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getTo() /*-{ return this.to; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
}
|
||||
|
||||
/* Completion */
|
||||
|
||||
public PGFRequest complete (String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
args.add(new Arg("cat", cat));
|
||||
args.add(new Arg("limit", limit));
|
||||
return sendRequest("complete", args, callback);
|
||||
}
|
||||
|
||||
public interface CompleteCallback extends GFCallback<Completions> { }
|
||||
|
||||
public static class Completions extends IterableJsArray<Translation> {
|
||||
protected Completions() { }
|
||||
}
|
||||
|
||||
public static class Completion extends JavaScriptObject {
|
||||
protected Completion() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
}
|
||||
|
||||
/* Parsing */
|
||||
|
||||
public PGFRequest parse (String input, List<String> fromLangs, String cat, final ParseCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
args.add(new Arg("cat", cat));
|
||||
return sendRequest("parse", args, callback);
|
||||
}
|
||||
|
||||
public interface ParseCallback extends GFCallback<ParseResults> { }
|
||||
|
||||
public static class ParseResults extends IterableJsArray<ParseResult> {
|
||||
protected ParseResults() { }
|
||||
}
|
||||
|
||||
public static class ParseResult extends JavaScriptObject {
|
||||
protected ParseResult() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getTree() /*-{ return this.tree; }-*/;
|
||||
}
|
||||
|
||||
/* Utilities */
|
||||
|
||||
private <T extends JavaScriptObject> PGFRequest sendRequest (String resource, List<Arg> vars, final GFCallback<T> callback) {
|
||||
String url = baseURL + "/" + pgfName + "/" + 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) {
|
||||
callback.onError(e);
|
||||
}
|
||||
|
||||
public void onResponseReceived(Request request, Response response) {
|
||||
if (200 == response.getStatusCode()) {
|
||||
callback.onResult((T)eval(response.getText()).cast());
|
||||
} else {
|
||||
RequestException e = new RequestException("Response not OK: " + response.getStatusCode() + ". " + response.getText());
|
||||
callback.onError(e);
|
||||
public JSONRequest translate (String input, List<String> fromLangs, String cat, List<String> toLangs,
|
||||
final TranslateCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (RequestException e) {
|
||||
callback.onError(e);
|
||||
}
|
||||
|
||||
return new PGFRequest(request);
|
||||
}
|
||||
|
||||
private static native JavaScriptObject eval(String json) /*-{
|
||||
return eval('(' + json + ')');
|
||||
}-*/;
|
||||
|
||||
private static class Arg {
|
||||
public final String name;
|
||||
public final String value;
|
||||
public Arg (String name, String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
public Arg (String name, int value) {
|
||||
this(name, Integer.toString(value));
|
||||
}
|
||||
}
|
||||
|
||||
private static String buildQueryString(List<Arg> args) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (args != null) {
|
||||
for (Arg arg : args) {
|
||||
if (arg.value != null) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append("&");
|
||||
}
|
||||
sb.append(URL.encodeComponent(arg.name));
|
||||
sb.append("=");
|
||||
sb.append(URL.encodeComponent(arg.value));
|
||||
}
|
||||
}
|
||||
args.add(new Arg("cat", cat));
|
||||
if (toLangs != null) {
|
||||
for (String to : toLangs) {
|
||||
args.add(new Arg("to", to));
|
||||
}
|
||||
}
|
||||
return sendRequest("translate", args, callback);
|
||||
}
|
||||
|
||||
public interface TranslateCallback extends JSONCallback<Translations> { }
|
||||
|
||||
public static class Translations extends IterableJsArray<Translation> {
|
||||
protected Translations() { }
|
||||
}
|
||||
|
||||
public static class Translation extends JavaScriptObject {
|
||||
protected Translation() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getTo() /*-{ return this.to; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
}
|
||||
|
||||
/* Completion */
|
||||
|
||||
public JSONRequest complete (String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
args.add(new Arg("cat", cat));
|
||||
args.add(new Arg("limit", limit));
|
||||
return sendRequest("complete", args, callback);
|
||||
}
|
||||
|
||||
public interface CompleteCallback extends JSONCallback<Completions> { }
|
||||
|
||||
public static class Completions extends IterableJsArray<Translation> {
|
||||
protected Completions() { }
|
||||
}
|
||||
|
||||
public static class Completion extends JavaScriptObject {
|
||||
protected Completion() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
}
|
||||
|
||||
/* Parsing */
|
||||
|
||||
public JSONRequest parse (String input, List<String> fromLangs, String cat, final ParseCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
args.add(new Arg("cat", cat));
|
||||
return sendRequest("parse", args, callback);
|
||||
}
|
||||
|
||||
public interface ParseCallback extends JSONCallback<ParseResults> { }
|
||||
|
||||
public static class ParseResults extends IterableJsArray<ParseResult> {
|
||||
protected ParseResults() { }
|
||||
}
|
||||
|
||||
public static class ParseResult extends JavaScriptObject {
|
||||
protected ParseResult() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getTree() /*-{ return this.tree; }-*/;
|
||||
}
|
||||
|
||||
/* Common */
|
||||
|
||||
public <T extends JavaScriptObject> JSONRequest sendRequest(String resource, List<Arg> args, final JSONCallback<T> callback) {
|
||||
return JSONRequestBuilder.sendRequest(baseURL + "/" + pgfName + "/" + resource, args, callback);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import com.google.gwt.http.client.*;
|
||||
|
||||
public class PGFRequest {
|
||||
|
||||
private Request httpRequest;
|
||||
|
||||
PGFRequest (Request httpRequest) {
|
||||
this.httpRequest = httpRequest;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
if (httpRequest != null) {
|
||||
httpRequest.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,13 +4,8 @@ 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;
|
||||
import com.google.gwt.user.client.ui.HorizontalPanel;
|
||||
import com.google.gwt.user.client.ui.Label;
|
||||
import com.google.gwt.user.client.ui.ListBox;
|
||||
import com.google.gwt.user.client.ui.PopupPanel;
|
||||
import com.google.gwt.user.client.ui.RootPanel;
|
||||
import com.google.gwt.user.client.ui.SuggestBox;
|
||||
@@ -20,13 +15,6 @@ 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.i18n.client.LocaleInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class TranslateApp implements EntryPoint {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user