mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-21 00:52:51 -06:00
Fridge: show initial letters in magnet bag when needed.
This commit is contained in:
@@ -1,8 +1,14 @@
|
|||||||
package se.chalmers.cs.gf.gwt.client;
|
package se.chalmers.cs.gf.gwt.client;
|
||||||
|
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
|
||||||
import com.google.gwt.core.client.GWT;
|
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.Composite;
|
||||||
import com.google.gwt.user.client.ui.FlowPanel;
|
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 {
|
public class FridgeBagPanel extends Composite {
|
||||||
|
|
||||||
@@ -12,13 +18,25 @@ public class FridgeBagPanel extends Composite {
|
|||||||
|
|
||||||
private JSONRequest completeRequest = null;
|
private JSONRequest completeRequest = null;
|
||||||
|
|
||||||
|
private FlowPanel prefixPanel;
|
||||||
|
|
||||||
private FlowPanel mainPanel;
|
private FlowPanel mainPanel;
|
||||||
|
|
||||||
|
private int maxMagnets = 100;
|
||||||
|
|
||||||
|
private LinkedHashSet<String> prefixes = new LinkedHashSet<String>();
|
||||||
|
|
||||||
|
|
||||||
public FridgeBagPanel (PGFWrapper pgf, MagnetFactory magnetFactory) {
|
public FridgeBagPanel (PGFWrapper pgf, MagnetFactory magnetFactory) {
|
||||||
this.pgf = pgf;
|
this.pgf = pgf;
|
||||||
this.magnetFactory = magnetFactory;
|
this.magnetFactory = magnetFactory;
|
||||||
|
prefixPanel = new FlowPanel();
|
||||||
mainPanel = 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");
|
setStylePrimaryName("my-FridgeBagPanel");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,11 +45,13 @@ public class FridgeBagPanel extends Composite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateBag (final String text, String prefix) {
|
public void updateBag (final String text, String prefix) {
|
||||||
mainPanel.clear();
|
|
||||||
int limit = 100;
|
|
||||||
if (completeRequest != null) {
|
if (completeRequest != null) {
|
||||||
completeRequest.cancel();
|
completeRequest.cancel();
|
||||||
}
|
}
|
||||||
|
final boolean updatePrefixes = prefix.equals("");
|
||||||
|
mainPanel.clear();
|
||||||
|
if (updatePrefixes) { clearPrefixes(); }
|
||||||
|
int limit = updatePrefixes ? 0 : maxMagnets;
|
||||||
completeRequest = pgf.complete(text + " " + prefix,
|
completeRequest = pgf.complete(text + " " + prefix,
|
||||||
limit, new PGF.CompleteCallback() {
|
limit, new PGF.CompleteCallback() {
|
||||||
public void onResult(PGF.Completions completions) {
|
public void onResult(PGF.Completions completions) {
|
||||||
@@ -41,8 +61,17 @@ public class FridgeBagPanel extends Composite {
|
|||||||
String[] words = newText.split("\\s+");
|
String[] words = newText.split("\\s+");
|
||||||
if (words.length > 0) {
|
if (words.length > 0) {
|
||||||
String word = words[words.length - 1];
|
String word = words[words.length - 1];
|
||||||
|
if (word.length() > 0) {
|
||||||
|
if (updatePrefixes) {
|
||||||
|
addPrefix(text, word.substring(0,1));
|
||||||
|
}
|
||||||
|
if (mainPanel.getWidgetCount() < maxMagnets) {
|
||||||
Magnet magnet = magnetFactory.createMagnet(word, completion.getFrom());
|
Magnet magnet = magnetFactory.createMagnet(word, completion.getFrom());
|
||||||
mainPanel.add(magnet);
|
mainPanel.add(magnet);
|
||||||
|
} else {
|
||||||
|
prefixPanel.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,6 +83,24 @@ 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) {
|
public void cloneMagnet (Magnet magnet) {
|
||||||
int i = getWidgetIndex(magnet);
|
int i = getWidgetIndex(magnet);
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ public class PGF {
|
|||||||
args.add(new Arg("input", input));
|
args.add(new Arg("input", input));
|
||||||
args.add(new Arg("from", fromLang));
|
args.add(new Arg("from", fromLang));
|
||||||
args.add(new Arg("cat", cat));
|
args.add(new Arg("cat", cat));
|
||||||
if (limit != -1) {
|
if (limit > 0) {
|
||||||
args.add(new Arg("limit", limit));
|
args.add(new Arg("limit", limit));
|
||||||
}
|
}
|
||||||
return sendGrammarRequest(pgfName, "complete", args, callback);
|
return sendGrammarRequest(pgfName, "complete", args, callback);
|
||||||
|
|||||||
Reference in New Issue
Block a user