mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 19:42:50 -06:00
Added iterable() method for nicer iteration through GF method results. (GWT)
This commit is contained in:
@@ -10,7 +10,9 @@ import com.google.gwt.json.client.JSONObject;
|
|||||||
import com.google.gwt.json.client.JSONParser;
|
import com.google.gwt.json.client.JSONParser;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class GF {
|
public class GF {
|
||||||
@@ -26,6 +28,29 @@ public class GF {
|
|||||||
public void onError (Throwable e) ;
|
public void onError (Throwable e) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T extends JavaScriptObject> Iterable<T> iterable(final JsArray<T> array) {
|
||||||
|
return new Iterable<T>() {
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
return new Iterator<T>() {
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return i < array.length();
|
||||||
|
}
|
||||||
|
public T next() {
|
||||||
|
if (!hasNext()) {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
return array.get(i++);
|
||||||
|
}
|
||||||
|
public void remove() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/* Grammar */
|
/* Grammar */
|
||||||
|
|
||||||
public GFRequest grammar (final GrammarCallback callback) {
|
public GFRequest grammar (final GrammarCallback callback) {
|
||||||
|
|||||||
@@ -60,8 +60,7 @@ public class Translate implements EntryPoint {
|
|||||||
gf.translate(suggest.getText(), listBoxSelection(fromLangBox), null,
|
gf.translate(suggest.getText(), listBoxSelection(fromLangBox), null,
|
||||||
listBoxSelection(toLangBox), new GF.TranslateCallback() {
|
listBoxSelection(toLangBox), new GF.TranslateCallback() {
|
||||||
public void onResult (GF.Translations translations) {
|
public void onResult (GF.Translations translations) {
|
||||||
for (int i = 0; i < translations.length(); i++) {
|
for (GF.Translation t : GF.iterable(translations)) {
|
||||||
GF.Translation t = translations.get(i);
|
|
||||||
addTranslation(t.getText(), t.getTo());
|
addTranslation(t.getText(), t.getTo());
|
||||||
}
|
}
|
||||||
clearStatus();
|
clearStatus();
|
||||||
@@ -104,8 +103,7 @@ public class Translate implements EntryPoint {
|
|||||||
private void setGrammar(GF.Grammar grammar) {
|
private void setGrammar(GF.Grammar grammar) {
|
||||||
this.grammar = grammar;
|
this.grammar = grammar;
|
||||||
|
|
||||||
for (int i = 0; i < grammar.getLanguages().length(); i++) {
|
for (GF.Language l :GF.iterable(grammar.getLanguages())) {
|
||||||
GF.Language l = grammar.getLanguages().get(i);
|
|
||||||
String name = l.getName();
|
String name = l.getName();
|
||||||
if (l.canParse()) {
|
if (l.canParse()) {
|
||||||
fromLangBox.addItem(name);
|
fromLangBox.addItem(name);
|
||||||
|
|||||||
Reference in New Issue
Block a user