forked from GitHub/gf-core
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;
|
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 {
|
||||||
|
|
||||||
private PGFWrapper pgf;
|
private PGFWrapper pgf;
|
||||||
|
|
||||||
private MagnetFactory magnetFactory;
|
private MagnetFactory magnetFactory;
|
||||||
|
|
||||||
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,22 +45,33 @@ 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) {
|
||||||
for (PGF.Completion completion : completions.iterable()) {
|
for (PGF.Completion completion : completions.iterable()) {
|
||||||
String newText = completion.getText();
|
String newText = completion.getText();
|
||||||
if (!newText.equals(text + " ")) {
|
if (!newText.equals(text + " ")) {
|
||||||
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];
|
||||||
Magnet magnet = magnetFactory.createMagnet(word, completion.getFrom());
|
if (word.length() > 0) {
|
||||||
mainPanel.add(magnet);
|
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) {
|
public void cloneMagnet (Magnet magnet) {
|
||||||
int i = getWidgetIndex(magnet);
|
int i = getWidgetIndex(magnet);
|
||||||
GWT.log("cloneMagnet: " + magnet.getParent(), null);
|
GWT.log("cloneMagnet: " + magnet.getParent(), null);
|
||||||
@@ -63,6 +110,6 @@ public class FridgeBagPanel extends Composite {
|
|||||||
insert(magnetFactory.createMagnet(magnet), i);
|
insert(magnetFactory.createMagnet(magnet), i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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