mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Fridge: show initial letters in magnet bag when needed.
This commit is contained in:
@@ -1,24 +1,42 @@
|
||||
package se.chalmers.cs.gf.gwt.client;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
import com.google.gwt.core.client.GWT;
|
||||
import com.google.gwt.user.client.ui.Button;
|
||||
import com.google.gwt.user.client.ui.ClickListener;
|
||||
import com.google.gwt.user.client.ui.Composite;
|
||||
import com.google.gwt.user.client.ui.FlowPanel;
|
||||
import com.google.gwt.user.client.ui.VerticalPanel;
|
||||
import com.google.gwt.user.client.ui.Widget;
|
||||
|
||||
public class FridgeBagPanel extends Composite {
|
||||
|
||||
private PGFWrapper pgf;
|
||||
|
||||
private MagnetFactory magnetFactory;
|
||||
|
||||
|
||||
private JSONRequest completeRequest = null;
|
||||
|
||||
private FlowPanel prefixPanel;
|
||||
|
||||
private FlowPanel mainPanel;
|
||||
|
||||
|
||||
private int maxMagnets = 100;
|
||||
|
||||
private LinkedHashSet<String> prefixes = new LinkedHashSet<String>();
|
||||
|
||||
|
||||
public FridgeBagPanel (PGFWrapper pgf, MagnetFactory magnetFactory) {
|
||||
this.pgf = pgf;
|
||||
this.magnetFactory = magnetFactory;
|
||||
prefixPanel = new FlowPanel();
|
||||
mainPanel = new FlowPanel();
|
||||
initWidget(mainPanel);
|
||||
VerticalPanel vPanel = new VerticalPanel();
|
||||
vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
|
||||
vPanel.add(prefixPanel);
|
||||
vPanel.add(mainPanel);
|
||||
initWidget(vPanel);
|
||||
setStylePrimaryName("my-FridgeBagPanel");
|
||||
}
|
||||
|
||||
@@ -27,22 +45,33 @@ public class FridgeBagPanel extends Composite {
|
||||
}
|
||||
|
||||
public void updateBag (final String text, String prefix) {
|
||||
mainPanel.clear();
|
||||
int limit = 100;
|
||||
if (completeRequest != null) {
|
||||
completeRequest.cancel();
|
||||
}
|
||||
final boolean updatePrefixes = prefix.equals("");
|
||||
mainPanel.clear();
|
||||
if (updatePrefixes) { clearPrefixes(); }
|
||||
int limit = updatePrefixes ? 0 : maxMagnets;
|
||||
completeRequest = pgf.complete(text + " " + prefix,
|
||||
limit, new PGF.CompleteCallback() {
|
||||
public void onResult(PGF.Completions completions) {
|
||||
for (PGF.Completion completion : completions.iterable()) {
|
||||
for (PGF.Completion completion : completions.iterable()) {
|
||||
String newText = completion.getText();
|
||||
if (!newText.equals(text + " ")) {
|
||||
String[] words = newText.split("\\s+");
|
||||
if (words.length > 0) {
|
||||
String word = words[words.length - 1];
|
||||
Magnet magnet = magnetFactory.createMagnet(word, completion.getFrom());
|
||||
mainPanel.add(magnet);
|
||||
if (word.length() > 0) {
|
||||
if (updatePrefixes) {
|
||||
addPrefix(text, word.substring(0,1));
|
||||
}
|
||||
if (mainPanel.getWidgetCount() < maxMagnets) {
|
||||
Magnet magnet = magnetFactory.createMagnet(word, completion.getFrom());
|
||||
mainPanel.add(magnet);
|
||||
} else {
|
||||
prefixPanel.setVisible(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,8 +82,26 @@ public class FridgeBagPanel extends Composite {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
protected void clearPrefixes () {
|
||||
prefixes.clear();
|
||||
prefixPanel.clear();
|
||||
prefixPanel.setVisible(false);
|
||||
}
|
||||
|
||||
protected void addPrefix(final String text, final String prefix) {
|
||||
if (prefixes.add(prefix)) {
|
||||
Button prefixButton = new Button(prefix, new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
updateBag(text, prefix);
|
||||
}
|
||||
});
|
||||
prefixPanel.add(prefixButton);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
public void cloneMagnet (Magnet magnet) {
|
||||
int i = getWidgetIndex(magnet);
|
||||
GWT.log("cloneMagnet: " + magnet.getParent(), null);
|
||||
@@ -63,6 +110,6 @@ public class FridgeBagPanel extends Composite {
|
||||
insert(magnetFactory.createMagnet(magnet), i);
|
||||
}
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ public class PGF {
|
||||
args.add(new Arg("input", input));
|
||||
args.add(new Arg("from", fromLang));
|
||||
args.add(new Arg("cat", cat));
|
||||
if (limit != -1) {
|
||||
if (limit > 0) {
|
||||
args.add(new Arg("limit", limit));
|
||||
}
|
||||
return sendGrammarRequest(pgfName, "complete", args, callback);
|
||||
|
||||
Reference in New Issue
Block a user