GWT: somewhat nicer iterable() hack.

This commit is contained in:
bjorn
2008-09-22 10:11:15 +00:00
parent 491a829105
commit 3d11bf48a3
2 changed files with 27 additions and 24 deletions

View File

@@ -28,27 +28,30 @@ 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) { public static class IterableJsArray<T extends JavaScriptObject> extends JsArray<T> {
return new Iterable<T>() { protected IterableJsArray() {}
public Iterator<T> iterator() {
return new Iterator<T>() {
int i = 0;
public boolean hasNext() { public final Iterable<T> iterable() {
return i < array.length(); return new Iterable<T>() {
} public Iterator<T> iterator() {
public T next() { return new Iterator<T>() {
if (!hasNext()) { private int i = 0;
throw new NoSuchElementException(); public boolean hasNext() {
return i < length();
} }
return array.get(i++); public T next() {
} if (!hasNext()) {
public void remove() { throw new NoSuchElementException();
throw new UnsupportedOperationException(); }
} return get(i++);
}; }
} public void remove() {
}; throw new UnsupportedOperationException();
}
};
}
};
}
} }
/* Grammar */ /* Grammar */
@@ -66,7 +69,7 @@ public class GF {
public final native String getUserLanguage() /*-{ return this.userLanguage; }-*/; public final native String getUserLanguage() /*-{ return this.userLanguage; }-*/;
public final native JsArray<Language> getLanguages() /*-{ return this.languages; }-*/; public final native IterableJsArray<Language> getLanguages() /*-{ return this.languages; }-*/;
public final Language getLanguage(String name) { public final Language getLanguage(String name) {
int c = getLanguages().length(); int c = getLanguages().length();
@@ -109,7 +112,7 @@ public class GF {
public interface TranslateCallback extends GFCallback<Translations> { } public interface TranslateCallback extends GFCallback<Translations> { }
public static class Translations extends JsArray<Translation> { public static class Translations extends IterableJsArray<Translation> {
protected Translations() { } protected Translations() { }
} }
@@ -138,7 +141,7 @@ public class GF {
public interface CompleteCallback extends GFCallback<Completions> { } public interface CompleteCallback extends GFCallback<Completions> { }
public static class Completions extends JsArray<Translation> { public static class Completions extends IterableJsArray<Translation> {
protected Completions() { } protected Completions() { }
} }

View File

@@ -60,7 +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 (GF.Translation t : GF.iterable(translations)) { for (GF.Translation t : translations.iterable()) {
addTranslation(t.getText(), t.getTo()); addTranslation(t.getText(), t.getTo());
} }
clearStatus(); clearStatus();
@@ -103,7 +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 (GF.Language l :GF.iterable(grammar.getLanguages())) { for (GF.Language l : grammar.getLanguages().iterable()) {
String name = l.getName(); String name = l.getName();
if (l.canParse()) { if (l.canParse()) {
fromLangBox.addItem(name); fromLangBox.addItem(name);