forked from GitHub/gf-core
Some refactoring of the GWT translator.
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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<String> getSelectedValues() {
|
||||||
|
int c = getItemCount();
|
||||||
|
List<String> l = new ArrayList<String>();
|
||||||
|
for (int i = 0; i < c; i++) {
|
||||||
|
if (isItemSelected(i)) {
|
||||||
|
l.add(getValue(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -38,8 +38,8 @@ public class TranslateApp implements EntryPoint {
|
|||||||
private CompletionOracle oracle;
|
private CompletionOracle oracle;
|
||||||
private SuggestBox suggest;
|
private SuggestBox suggest;
|
||||||
private PGF.Grammar grammar;
|
private PGF.Grammar grammar;
|
||||||
private ListBox fromLangBox;
|
private InputLanguageBox fromLangBox;
|
||||||
private ListBox toLangBox;
|
private OutputLanguageBox toLangBox;
|
||||||
private Button translateButton;
|
private Button translateButton;
|
||||||
private VerticalPanel outputPanel;
|
private VerticalPanel outputPanel;
|
||||||
private PopupPanel statusPopup;
|
private PopupPanel statusPopup;
|
||||||
@@ -58,8 +58,8 @@ public class TranslateApp implements EntryPoint {
|
|||||||
private void translate() {
|
private void translate() {
|
||||||
outputPanel.clear();
|
outputPanel.clear();
|
||||||
setStatus("Translating...");
|
setStatus("Translating...");
|
||||||
pgf.translate(suggest.getText(), listBoxSelection(fromLangBox), null,
|
pgf.translate(suggest.getText(), fromLangBox.getSelectedValues(), null,
|
||||||
listBoxSelection(toLangBox), new PGF.TranslateCallback() {
|
fromLangBox.getSelectedValues(), new PGF.TranslateCallback() {
|
||||||
public void onResult (PGF.Translations translations) {
|
public void onResult (PGF.Translations translations) {
|
||||||
for (PGF.Translation t : translations.iterable()) {
|
for (PGF.Translation t : translations.iterable()) {
|
||||||
addTranslation(t.getText(), t.getTo());
|
addTranslation(t.getText(), t.getTo());
|
||||||
@@ -73,18 +73,7 @@ public class TranslateApp implements EntryPoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateLangs() {
|
private void updateLangs() {
|
||||||
oracle.setInputLangs(listBoxSelection(fromLangBox));
|
oracle.setInputLangs(fromLangBox.getSelectedValues());
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> listBoxSelection(ListBox box) {
|
|
||||||
int c = box.getItemCount();
|
|
||||||
List<String> l = new ArrayList<String>();
|
|
||||||
for (int i = 0; i < c; i++) {
|
|
||||||
if (box.isItemSelected(i)) {
|
|
||||||
l.add(box.getValue(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return l;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setStatus(String msg) {
|
private void setStatus(String msg) {
|
||||||
@@ -104,21 +93,11 @@ public class TranslateApp implements EntryPoint {
|
|||||||
private void setGrammar(PGF.Grammar grammar) {
|
private void setGrammar(PGF.Grammar grammar) {
|
||||||
this.grammar = grammar;
|
this.grammar = grammar;
|
||||||
|
|
||||||
for (PGF.Language l : grammar.getLanguages().iterable()) {
|
fromLangBox.setGrammar(grammar);
|
||||||
String name = l.getName();
|
toLangBox.setGrammar(grammar);
|
||||||
if (l.canParse()) {
|
|
||||||
fromLangBox.addItem(name);
|
|
||||||
if (name.equals(grammar.getUserLanguage())) {
|
|
||||||
fromLangBox.setSelectedIndex(fromLangBox.getItemCount()-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
toLangBox.addItem(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
updateLangs();
|
updateLangs();
|
||||||
clearStatus();
|
clearStatus();
|
||||||
fromLangBox.setEnabled(true);
|
|
||||||
toLangBox.setEnabled(true);
|
|
||||||
translateButton.setEnabled(true);
|
translateButton.setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,8 +118,7 @@ public class TranslateApp implements EntryPoint {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
fromLangBox = new ListBox();
|
fromLangBox = new InputLanguageBox();
|
||||||
fromLangBox.setEnabled(false);
|
|
||||||
fromLangBox.addItem("Any language", "");
|
fromLangBox.addItem("Any language", "");
|
||||||
fromLangBox.addChangeListener(new ChangeListener() {
|
fromLangBox.addChangeListener(new ChangeListener() {
|
||||||
public void onChange(Widget sender) {
|
public void onChange(Widget sender) {
|
||||||
@@ -149,8 +127,7 @@ public class TranslateApp implements EntryPoint {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
toLangBox = new ListBox();
|
toLangBox = new OutputLanguageBox();
|
||||||
toLangBox.setEnabled(false);
|
|
||||||
toLangBox.addItem("All languages", "");
|
toLangBox.addItem("All languages", "");
|
||||||
toLangBox.addChangeListener(new ChangeListener() {
|
toLangBox.addChangeListener(new ChangeListener() {
|
||||||
public void onChange(Widget sender) {
|
public void onChange(Widget sender) {
|
||||||
|
|||||||
Reference in New Issue
Block a user