Added first version of the fridge poetry gui

This commit is contained in:
bjorn
2008-11-03 16:43:47 +00:00
parent 12ecbdc89f
commit df5e7b3363
12 changed files with 497 additions and 23 deletions

View File

@@ -0,0 +1,17 @@
#!/bin/sh
APPDIR=`dirname $0`;
if [ -z "$GWT_CLASSPATH" ]; then
echo 'ERROR: $GWT_CLASSPATH is not set'
echo 'Set $GWT_CLASSPATH to point to the GWT JAR files. For example:'
echo 'export GWT_DIR="/Users/bringert/src/gwt-mac-1.5.2"'
echo 'export GWT_CLASSPATH="$GWT_DIR/gwt-user.jar:$GWT_DIR/gwt-dev-mac.jar"'
exit 1
fi
if [ `uname` = "Darwin" ]; then
GWT_JAVA_OPTS=-XstartOnFirstThread
fi
java $GWT_JAVA_OPTS -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:$GWT_CLASSPATH" com.google.gwt.dev.GWTCompiler -out "$APPDIR/www" "$@" se.chalmers.cs.gf.gwt.FridgeApp;

View File

@@ -0,0 +1,17 @@
#!/bin/sh
APPDIR=`dirname $0`;
if [ -z "$GWT_CLASSPATH" ]; then
echo 'ERROR: $GWT_CLASSPATH is not set'
echo 'Set $GWT_CLASSPATH to point to the GWT JAR files. For example:'
echo 'export GWT_DIR="/Users/bringert/src/gwt-mac-1.5.2"'
echo 'export GWT_CLASSPATH="$GWT_DIR/gwt-user.jar:$GWT_DIR/gwt-dev-mac.jar"'
exit 1
fi
if [ `uname` = "Darwin" ]; then
GWT_JAVA_OPTS=-XstartOnFirstThread
fi
java $GWT_JAVA_OPTS -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:$GWT_CLASSPATH" com.google.gwt.dev.GWTShell -out "$APPDIR/www" -noserver "$@" http://localhost:41296/gwt/www/se.chalmers.cs.gf.gwt.FridgeApp/Fridge.html;

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/GF-GWT"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER&quot; javaProject=&quot;GF-GWT&quot; path=&quot;1&quot; type=&quot;4&quot;/&gt;&#13;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry internalArchive=&quot;/GF-GWT/src&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#13;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry id=&quot;org.eclipse.jdt.launching.classpathentry.defaultClasspath&quot;&gt;&#13;&#10;&lt;memento project=&quot;GF-GWT&quot;/&gt;&#13;&#10;&lt;/runtimeClasspathEntry&gt;&#13;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry externalArchive=&quot;/Users/bringert/src/gwt-mac-1.5.3/gwt-dev-mac.jar&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#13;&#10;"/>
</listAttribute>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.gwt.dev.GWTShell"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-out www -noserver http://localhost:41296/gwt/www/se.chalmers.cs.gf.gwt.FridgeApp/Fridge.html"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="GF-GWT"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-XstartOnFirstThread -Xmx256M"/>
</launchConfiguration>

View File

@@ -1,16 +1,21 @@
<?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.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/GF-GWT"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER&quot; javaProject=&quot;GF-GWT&quot; path=&quot;1&quot; type=&quot;4&quot;/&gt;&#13;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry internalArchive=&quot;/GF-GWT/src&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#13;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry id=&quot;org.eclipse.jdt.launching.classpathentry.defaultClasspath&quot;&gt;&#13;&#10;&lt;memento project=&quot;GF-GWT&quot;/&gt;&#13;&#10;&lt;/runtimeClasspathEntry&gt;&#13;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry externalArchive=&quot;/Users/bringert/src/gwt-mac-1.5.3/gwt-dev-mac.jar&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#13;&#10;"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-XstartOnFirstThread -Xmx256M"/>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.gwt.dev.GWTShell"/>
<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"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-XstartOnFirstThread -Xmx256M"/>
</launchConfiguration>

View File

@@ -0,0 +1,24 @@
<module>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name="com.google.gwt.user.User" />
<inherits name="se.chalmers.cs.gf.gwt.PGF" />
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name="com.google.gwt.user.theme.standard.Standard"/>
<!-- <inherits name="com.google.gwt.user.theme.chrome.Chrome"/> -->
<!-- <inherits name="com.google.gwt.user.theme.dark.Dark"/> -->
<!-- Other module inherits -->
<!-- Specify the app entry point class. -->
<entry-point class="se.chalmers.cs.gf.gwt.client.FridgeApp" />
<!-- Specify the application specific style sheet. -->
<stylesheet src="Fridge.css" />
</module>

View File

@@ -0,0 +1,235 @@
package se.chalmers.cs.gf.gwt.client;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
public class FridgeApp implements EntryPoint {
private static final String pgfBaseURL = "/pgf";
private PGF pgf;
private FlowPanel textPanel;
private Panel bagPanel;
private SettingsPanel settingsPanel;
private VerticalPanel outputPanel;
private StatusPopup statusPopup;
//
// Translation
//
private void translate() {
outputPanel.clear();
setStatus("Translating...");
pgf.translate(settingsPanel.getGrammarName(),
getText(),
settingsPanel.getInputLanguages(), null,
settingsPanel.getOutputLanguages(),
new PGF.TranslateCallback() {
public void onResult (PGF.Translations translations) {
for (final PGF.Translation t : translations.iterable()) {
PushButton l = new PushButton(t.getText());
l.addStyleName("my-translation");
l.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
setText(t.getTo(), t.getText());
}
});
PGF.Language lang = settingsPanel.getGrammar().getLanguage(t.getTo());
if (lang != null) {
l.getElement().setLang(lang.getLanguageCode());
}
outputPanel.add(l);
}
clearStatus();
}
public void onError (Throwable e) {
showError("Translation failed", e);
}
});
}
//
// Magnets
//
private List<String> getWords() {
List<String> l = new ArrayList<String>();
for (Widget w : textPanel) {
l.add(((Magnet)w).getText());
}
return l;
}
private String getText () {
StringBuilder sb = new StringBuilder();
for (String word : getWords()) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(word);
}
return sb.toString();
}
private void setText (String language, String text) {
settingsPanel.setInputLanguages(Collections.singletonList(language));
textPanel.clear();
for (String word : text.split("\\s+")) {
textPanel.add(new Magnet(language, word));
}
updateBag();
}
private ClickListener magnetClickListener = new ClickListener () {
public void onClick(Widget sender) {
Magnet magnet = (Magnet)sender;
textPanel.add(new Magnet(magnet));
update();
}
};
private void updateBag () {
bagPanel.clear();
int limit = 100;
pgf.complete(settingsPanel.getGrammarName(),
getText() + " ",
settingsPanel.getInputLanguages(), null,
limit, new PGF.CompleteCallback() {
public void onResult(PGF.Completions completions) {
boolean empty = true;
List<String> oldWords = getWords();
for (PGF.Completion completion : completions.iterable()) {
String[] newWords = completion.getText().split("\\s+");
if (newWords.length == oldWords.size()+1) {
String word = newWords[newWords.length-1];
bagPanel.add(new Magnet(completion.getFrom(), word, magnetClickListener));
empty = false;
}
}
if (empty) {
bagPanel.add(new Label("<empty>"));
}
}
public void onError(Throwable e) {
showError("Error getting completions.", e);
}
});
}
public void update() {
updateBag();
translate();
}
public void clear() {
textPanel.clear();
update();
}
public void deleteLastMagnet() {
int c = textPanel.getWidgetCount();
if (c > 0) {
textPanel.remove(c-1);
update();
}
}
//
// Status stuff
//
private void setStatus(String msg) {
statusPopup.setStatus(msg);
}
private void showError(String msg, Throwable e) {
statusPopup.showError(msg, e);
}
private void clearStatus() {
statusPopup.clearStatus();
}
//
// GUI
//
private void createTranslationUI() {
statusPopup = new StatusPopup();
setStatus("Loading...");
settingsPanel = new SettingsPanel(pgf);
settingsPanel.addSettingsListener(new SettingsPanel.SettingsListener() {
public void grammarChanged(String pgfName) {
}
public void languagesChanged(List<String> inputLangs, List<String> outputLangs) {
update();
}
public void settingsError(String msg, Throwable e) {
showError(msg,e);
}
});
Panel buttons = new HorizontalPanel();
buttons.add(new Button("Clear", new ClickListener () {
public void onClick(Widget sender) {
clear();
}
}));
buttons.add(new Button("Delete last", new ClickListener () {
public void onClick(Widget sender) {
deleteLastMagnet();
}
}));
outputPanel = new VerticalPanel();
outputPanel.addStyleName("my-translations");
textPanel = new FlowPanel();
textPanel.setStylePrimaryName("my-TextPanel");
bagPanel = new FlowPanel();
bagPanel.setStylePrimaryName("my-BagPanel");
VerticalPanel mainPanel = new VerticalPanel();
mainPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
mainPanel.setWidth("100%");
mainPanel.add(textPanel);
mainPanel.add(buttons);
mainPanel.add(bagPanel);
mainPanel.add(outputPanel);
mainPanel.add(settingsPanel);
RootPanel.get().add(mainPanel);
}
//
// Initialization
//
public void onModuleLoad() {
pgf = new PGF(pgfBaseURL);
createTranslationUI();
settingsPanel.updateAvailableGrammars();
}
}

View File

@@ -0,0 +1,66 @@
package se.chalmers.cs.gf.gwt.client;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.Widget;
public class Magnet extends Composite implements SourcesClickEvents {
private String language;
private String text;
private ClickListenerCollection clickListeners = null;
public Magnet(Magnet magnet) {
this(magnet.language, magnet.text);
}
public Magnet(String language, String text) {
this(language, text, null);
}
public Magnet(String language, String text, ClickListener listener) {
this.language = language;
this.text = text;
final PushButton button = new PushButton(text);
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
button.setFocus(false);
fireClick();
}
});
initWidget(button);
addStyleName("my-Magnet");
if (listener != null) {
addClickListener(listener);
}
}
public String getText() {
return text;
}
public void addClickListener(ClickListener listener) {
if (clickListeners == null) {
clickListeners = new ClickListenerCollection();
}
clickListeners.add(listener);
}
public void removeClickListener(ClickListener listener) {
if (clickListeners != null) {
clickListeners.remove(listener);
}
}
public void fireClick () {
if (clickListeners != null) {
clickListeners.fireClick(this);
}
}
}

View File

@@ -1,24 +1,34 @@
package se.chalmers.cs.gf.gwt.client;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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));
}
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;
}
public void setSelectedValues(List<String> values) {
Set<String> vs = new HashSet<String>(values);
int c = getItemCount();
for (int i = 0; i < c; i++) {
setItemSelected(i, vs.contains(getValue(i)));
}
}
return l;
}
}

View File

@@ -105,6 +105,11 @@ public class PGF {
/* Completion */
/**
* Get suggestions for completing the input.
* @param limit The number of suggestions to get.
* If -1 is passed, all available suggestions are retrieved.
*/
public JSONRequest complete (String pgfName, String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) {
List<Arg> args = new ArrayList<Arg>();
args.add(new Arg("input", input));
@@ -114,7 +119,9 @@ public class PGF {
}
}
args.add(new Arg("cat", cat));
args.add(new Arg("limit", limit));
if (limit != -1) {
args.add(new Arg("limit", limit));
}
return sendGrammarRequest(pgfName, "complete", args, callback);
}

View File

@@ -121,12 +121,16 @@ public class SettingsPanel extends Composite {
public List<String> getInputLanguages() {
return fromLangBox.getSelectedValues();
}
}
public List<String> getOutputLanguages() {
return toLangBox.getSelectedValues();
}
public void setInputLanguages(List<String> langs) {
fromLangBox.setSelectedValues(langs);
}
private void updateAvailableLanguages() {
pgf.grammar(getGrammarName(), new PGF.GrammarCallback() {
public void onResult(PGF.Grammar grammar) {

View File

@@ -0,0 +1,32 @@
.my-TextPanel, .my-BagPanel, .my-translations, .my-SettingsPanel {
margin: 1em;
}
.my-TextPanel {
font-size: 150%;
min-height: 2em;
border: 3px dashed silver;
overflow: auto;
}
.my-BagPanel {
border: 1px solid silver;
overflow: auto;
}
.my-Magnet {
float: left;
margin: 0.3em;
padding: 0.3em;
}
.my-translations {
border: 1px solid silver;
}
.my-translation {
margin: 0.2em;
}
.my-SettingsPanel {
}

View File

@@ -0,0 +1,36 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set -->
<!-- the browser's rendering engine into -->
<!-- "Quirks Mode". Replacing this declaration -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout. -->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Fridge</title>
<!-- -->
<!-- This script loads your compiled module. -->
<!-- If you add any GWT meta tags, they must -->
<!-- be added before this line. -->
<!-- -->
<script type="text/javascript" language="javascript" src="se.chalmers.cs.gf.gwt.FridgeApp.nocache.js"></script>
</head>
<!-- -->
<!-- The body can have arbitrary html, or -->
<!-- you can leave the body empty if you want -->
<!-- to create a completely dynamic UI. -->
<!-- -->
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
</body>
</html>