use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.
the class SuggestionPopupTest method startup.
@Override
public void startup(final Display display, final Map<String, String> properties) throws Exception {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
window = (Window) bxmlSerializer.readObject(SuggestionPopupTest.class, "suggestion_popup_test.bxml");
bxmlSerializer.bind(this);
textInput.getTextInputContentListeners().add(new TextInputContentListener() {
@Override
public void textInserted(final TextInput textInputArgument, final int index, final int count) {
ArrayList<String> suggestions = new ArrayList<>("One", "Two", "Three", "Four", "Five");
suggestionPopup.setSuggestionData(suggestions);
suggestionPopup.open(textInputArgument, new SuggestionPopupCloseListener() {
@Override
public void suggestionPopupClosed(final SuggestionPopup suggestionPopupArgument) {
if (suggestionPopupArgument.getResult()) {
selectedIndexLabel.setText("You selected suggestion number " + suggestionPopupArgument.getSelectedIndex() + ".");
} else {
selectedIndexLabel.setText("You didn't select anything.");
}
}
});
}
@Override
public void textRemoved(final TextInput textInputArgument, final int index, final int count) {
suggestionPopup.close();
}
});
window.open(display);
}
use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.
the class TerraSuggestionPopupSkin method install.
@Override
public void install(Component component) {
super.install(component);
SuggestionPopup suggestionPopup = (SuggestionPopup) component;
suggestionPopup.getSuggestionPopupListeners().add(this);
suggestionPopup.getSuggestionPopupSelectionListeners().add(this);
suggestionPopup.getSuggestionPopupStateListeners().add(this);
suggestionPopup.setContent(listViewBorder);
listView.setListData(suggestionPopup.getSuggestionData());
listView.setItemRenderer(suggestionPopup.getSuggestionRenderer());
if (!themeIsFlat()) {
// Attach the drop-shadow decorator
dropShadowDecorator = new DropShadowDecorator();
suggestionPopup.getDecorators().add(dropShadowDecorator);
}
}
use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.
the class TerraSuggestionPopupSkin method mouseClick.
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
SuggestionPopup suggestionPopup = (SuggestionPopup) getComponent();
suggestionPopup.close(true);
return true;
}
use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.
the class TerraSuggestionPopupSkin method windowOpened.
@Override
public void windowOpened(Window window) {
super.windowOpened(window);
// Adjust for list size
SuggestionPopup suggestionPopup = (SuggestionPopup) window;
int listSize = suggestionPopup.getListSize();
if (listSize == -1) {
listViewBorder.setPreferredHeight(-1);
} else {
if (!listViewBorder.isPreferredHeightSet()) {
ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
int borderHeight = itemRenderer.getPreferredHeight(-1) * listSize + 2;
if (listViewBorder.getPreferredHeight() > borderHeight) {
listViewBorder.setPreferredHeight(borderHeight);
} else {
listViewBorder.setPreferredHeight(-1);
}
}
}
Display display = window.getDisplay();
display.getContainerMouseListeners().add(displayMouseListener);
if (dropShadowDecorator != null) {
dropShadowDecorator.setShadowOpacity(DropShadowDecorator.DEFAULT_SHADOW_OPACITY);
}
returnFocusToTextInput = true;
TextInput textInput = suggestionPopup.getTextInput();
textInput.getComponentStateListeners().add(textInputStateListener);
textInput.getComponentKeyListeners().add(textInputKeyListener);
// Size and position the popup
Point location = textInput.mapPointToAncestor(textInput.getDisplay(), 0, 0);
window.setLocation(location.x, location.y + textInput.getHeight() - 1);
window.setMinimumWidth(textInput.getWidth());
window.setMaximumHeight(display.getHeight() - window.getY());
}
use of org.apache.pivot.wtk.SuggestionPopup in project pivot by apache.
the class SuggestionDemo method getSuggestions.
private void getSuggestions() {
if (suggestionQuery != null && suggestionQuery.isPending()) {
suggestionQuery.abort();
}
// Get the query text
String text;
try {
text = URLEncoder.encode(textInput.getText(), "UTF-8");
} catch (UnsupportedEncodingException exception) {
throw new RuntimeException(exception);
}
// Create query
suggestionQuery = new GetQuery("search.yahooapis.com", "/WebSearchService/V1/relatedSuggestion");
suggestionQuery.getParameters().put("appid", getClass().getName());
suggestionQuery.getParameters().put("query", text);
suggestionQuery.getParameters().put("output", "json");
suggestionQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {
@Override
public void taskExecuted(Task<Object> task) {
if (task == suggestionQuery) {
List<?> suggestions = null;
Object result = JSON.get(task.getResult(), "ResultSet.Result");
if (result instanceof List<?>) {
suggestions = (List<?>) result;
}
if (suggestions == null || suggestions.getLength() == 0) {
suggestionPopup.close();
} else {
suggestionPopup.setSuggestionData(suggestions);
suggestionPopup.open(textInput, new SuggestionPopupCloseListener() {
@Override
public void suggestionPopupClosed(SuggestionPopup suggestionPopupArgument) {
if (suggestionPopupArgument.getResult()) {
String textLocal;
try {
textLocal = URLEncoder.encode(textInput.getText(), "UTF-8");
} catch (UnsupportedEncodingException exception) {
throw new RuntimeException(exception);
}
String location = "http://search.yahoo.com/search?p=" + textLocal;
try {
Desktop.getDesktop().browse(new URI(location));
} catch (IOException exception) {
System.err.println(exception);
} catch (URISyntaxException exception) {
System.err.println(exception);
}
}
}
});
}
activityIndicator.setActive(false);
suggestionQuery = null;
}
}
@Override
public void executeFailed(Task<Object> task) {
if (task == suggestionQuery) {
System.err.println(task.getFault());
activityIndicator.setActive(false);
suggestionQuery = null;
}
}
}));
activityIndicator.setActive(true);
}
Aggregations