From 7c914d39cb836c07d5927202f230db958d904da2 Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 22 Oct 2008 19:37:57 +0000 Subject: [PATCH] Some refactoring of the GWT translator. --- .../cs/gf/gwt/client/InputLanguageBox.java | 22 ++++++++++ .../cs/gf/gwt/client/MultiListBox.java | 24 +++++++++++ .../cs/gf/gwt/client/OutputLanguageBox.java | 16 ++++++++ .../cs/gf/gwt/client/TranslateApp.java | 41 ++++--------------- 4 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 src/server/gwt/src/se/chalmers/cs/gf/gwt/client/InputLanguageBox.java create mode 100644 src/server/gwt/src/se/chalmers/cs/gf/gwt/client/MultiListBox.java create mode 100644 src/server/gwt/src/se/chalmers/cs/gf/gwt/client/OutputLanguageBox.java diff --git a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/InputLanguageBox.java b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/InputLanguageBox.java new file mode 100644 index 000000000..26ca7e8e1 --- /dev/null +++ b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/InputLanguageBox.java @@ -0,0 +1,22 @@ +package se.chalmers.cs.gf.gwt.client; + +public class InputLanguageBox extends MultiListBox { + + public InputLanguageBox() { + setEnabled(false); + } + + public void setGrammar(PGF.Grammar grammar) { + for (PGF.Language l : grammar.getLanguages().iterable()) { + String name = l.getName(); + if (l.canParse()) { + addItem(name); + if (name.equals(grammar.getUserLanguage())) { + setSelectedIndex(getItemCount()-1); + } + } + } + setEnabled(true); + } + +} \ No newline at end of file diff --git a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/MultiListBox.java b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/MultiListBox.java new file mode 100644 index 000000000..ac09c751f --- /dev/null +++ b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/MultiListBox.java @@ -0,0 +1,24 @@ +package se.chalmers.cs.gf.gwt.client; + +import com.google.gwt.user.client.ui.ListBox; + +import java.util.ArrayList; +import java.util.List; + +public class MultiListBox extends ListBox { + + public MultiListBox() { + } + + public List getSelectedValues() { + int c = getItemCount(); + List l = new ArrayList(); + for (int i = 0; i < c; i++) { + if (isItemSelected(i)) { + l.add(getValue(i)); + } + } + return l; + } + +} \ No newline at end of file diff --git a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/OutputLanguageBox.java b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/OutputLanguageBox.java new file mode 100644 index 000000000..2f943eb3a --- /dev/null +++ b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/OutputLanguageBox.java @@ -0,0 +1,16 @@ +package se.chalmers.cs.gf.gwt.client; + +public class OutputLanguageBox extends MultiListBox { + + public OutputLanguageBox() { + setEnabled(false); + } + + public void setGrammar(PGF.Grammar grammar) { + for (PGF.Language l : grammar.getLanguages().iterable()) { + addItem(l.getName()); + } + setEnabled(true); + } + +} \ No newline at end of file diff --git a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/TranslateApp.java b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/TranslateApp.java index fd92713c3..54a65fa20 100644 --- a/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/TranslateApp.java +++ b/src/server/gwt/src/se/chalmers/cs/gf/gwt/client/TranslateApp.java @@ -38,8 +38,8 @@ public class TranslateApp implements EntryPoint { private CompletionOracle oracle; private SuggestBox suggest; private PGF.Grammar grammar; - private ListBox fromLangBox; - private ListBox toLangBox; + private InputLanguageBox fromLangBox; + private OutputLanguageBox toLangBox; private Button translateButton; private VerticalPanel outputPanel; private PopupPanel statusPopup; @@ -58,8 +58,8 @@ public class TranslateApp implements EntryPoint { private void translate() { outputPanel.clear(); setStatus("Translating..."); - pgf.translate(suggest.getText(), listBoxSelection(fromLangBox), null, - listBoxSelection(toLangBox), new PGF.TranslateCallback() { + pgf.translate(suggest.getText(), fromLangBox.getSelectedValues(), null, + fromLangBox.getSelectedValues(), new PGF.TranslateCallback() { public void onResult (PGF.Translations translations) { for (PGF.Translation t : translations.iterable()) { addTranslation(t.getText(), t.getTo()); @@ -73,18 +73,7 @@ public class TranslateApp implements EntryPoint { } private void updateLangs() { - oracle.setInputLangs(listBoxSelection(fromLangBox)); - } - - private List listBoxSelection(ListBox box) { - int c = box.getItemCount(); - List l = new ArrayList(); - for (int i = 0; i < c; i++) { - if (box.isItemSelected(i)) { - l.add(box.getValue(i)); - } - } - return l; + oracle.setInputLangs(fromLangBox.getSelectedValues()); } private void setStatus(String msg) { @@ -104,21 +93,11 @@ public class TranslateApp implements EntryPoint { private void setGrammar(PGF.Grammar grammar) { this.grammar = grammar; - for (PGF.Language l : grammar.getLanguages().iterable()) { - String name = l.getName(); - if (l.canParse()) { - fromLangBox.addItem(name); - if (name.equals(grammar.getUserLanguage())) { - fromLangBox.setSelectedIndex(fromLangBox.getItemCount()-1); - } - } - toLangBox.addItem(name); - } + fromLangBox.setGrammar(grammar); + toLangBox.setGrammar(grammar); updateLangs(); clearStatus(); - fromLangBox.setEnabled(true); - toLangBox.setEnabled(true); translateButton.setEnabled(true); } @@ -139,8 +118,7 @@ public class TranslateApp implements EntryPoint { } }); - fromLangBox = new ListBox(); - fromLangBox.setEnabled(false); + fromLangBox = new InputLanguageBox(); fromLangBox.addItem("Any language", ""); fromLangBox.addChangeListener(new ChangeListener() { public void onChange(Widget sender) { @@ -149,8 +127,7 @@ public class TranslateApp implements EntryPoint { } }); - toLangBox = new ListBox(); - toLangBox.setEnabled(false); + toLangBox = new OutputLanguageBox(); toLangBox.addItem("All languages", ""); toLangBox.addChangeListener(new ChangeListener() { public void onChange(Widget sender) {