import com.vaadin.terminal.gwt.client.VTooltip;
/**
+ * Client side implementation of the Select component.
*
* TODO needs major refactoring (to be extensible etc)
*/
KeyDownHandler, KeyUpHandler, ClickHandler, FocusHandler, BlurHandler,
Focusable {
+ /**
+ * Represents a suggestion in the suggestion popup box
+ */
public class FilterSelectSuggestion implements Suggestion, Command {
private final String key;
private final String caption;
private String iconUri;
+ /**
+ * Constructor
+ *
+ * @param uidl
+ * The UIDL recieved from the server
+ */
public FilterSelectSuggestion(UIDL uidl) {
key = uidl.getStringAttribute("key");
caption = uidl.getStringAttribute("caption");
}
}
+ /**
+ * Gets the visible row in the popup as a HTML string. The string
+ * contains an image tag with the rows icon (if an icon has been
+ * specified) and the caption of the item
+ */
public String getDisplayString() {
final StringBuffer sb = new StringBuffer();
if (iconUri != null) {
return sb.toString();
}
+ /**
+ * Get a string that represents this item. This is used in the text box.
+ */
public String getReplacementString() {
return caption;
}
+ /**
+ * Get the option key which represents the item on the server side.
+ *
+ * @return The key of the item
+ */
public int getOptionKey() {
return Integer.parseInt(key);
}
+ /**
+ * Get the URI of the icon. Used when constructing the displayed option.
+ *
+ * @return
+ */
public String getIconUri() {
return iconUri;
}
+ /**
+ * Executes a selection of this item.
+ */
public void execute() {
onSuggestionSelected(this);
}
}
+ /**
+ * Represents the popup box with the selection options. Wraps a suggestion
+ * menu.
+ */
public class SuggestionPopup extends VOverlay implements PositionCallback,
CloseHandler<PopupPanel> {
private int topPosition;
+ /**
+ * Default constructor
+ */
SuggestionPopup() {
super(true, false, true);
menu = new SuggestionMenu();
addCloseHandler(this);
}
+ /**
+ * Shows the popup where the user can see the filtered options
+ *
+ * @param currentSuggestions
+ * The filtered suggestions
+ * @param currentPage
+ * The current page number
+ * @param totalSuggestions
+ * The total amount of suggestions
+ */
public void showSuggestions(
Collection<FilterSelectSuggestion> currentSuggestions,
int currentPage, int totalSuggestions) {
}
- private void setNextButtonActive(boolean b) {
- if (b) {
+ /**
+ * Should the next page button be visible to the user?
+ *
+ * @param active
+ */
+ private void setNextButtonActive(boolean active) {
+ if (active) {
DOM.sinkEvents(down, Event.ONCLICK);
DOM.setElementProperty(down, "className", CLASSNAME
+ "-nextpage");
}
}
- private void setPrevButtonActive(boolean b) {
- if (b) {
+ /**
+ * Should the previous page button be visible to the user
+ *
+ * @param active
+ */
+ private void setPrevButtonActive(boolean active) {
+ if (active) {
DOM.sinkEvents(up, Event.ONCLICK);
DOM
.setElementProperty(up, "className", CLASSNAME
}
+ /**
+ * Selects the next item in the filtered selections
+ */
public void selectNextItem() {
final MenuItem cur = menu.getSelectedItem();
final int index = 1 + menu.getItems().indexOf(cur);
}
}
+ /**
+ * Selects the previous item in the filtered selections
+ */
public void selectPrevItem() {
final MenuItem cur = menu.getSelectedItem();
final int index = -1 + menu.getItems().indexOf(cur);
}
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt
+ * .user.client.Event)
+ */
@Override
public void onBrowserEvent(Event event) {
final Element target = DOM.eventGetTarget(event);
tb.setFocus(true);
}
+ /**
+ * Should paging be enabled. If paging is enabled then only a certain
+ * amount of items are visible at a time and a scrollbar or buttons are
+ * visible to change page. If paging is turned of then all options are
+ * rendered into the popup menu.
+ *
+ * @param paging
+ * Should the paging be turned on?
+ */
public void setPagingEnabled(boolean paging) {
if (isPagingEnabled == paging) {
return;
}
/**
+ * Was the popup just closed?
+ *
* @return true if popup was just closed
*/
public boolean isJustClosed() {
return (lastAutoClosed > 0 && (now - lastAutoClosed) < 200);
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.event.logical.shared.CloseHandler#onClose(com.google
+ * .gwt.event.logical.shared.CloseEvent)
+ */
public void onClose(CloseEvent<PopupPanel> event) {
if (event.isAutoClosed()) {
lastAutoClosed = (new Date()).getTime();
}
+ /**
+ * The menu where the suggestions are rendered
+ */
public class SuggestionMenu extends MenuBar {
+ /**
+ * Default constructor
+ */
SuggestionMenu() {
super(true);
setStyleName(CLASSNAME + "-suggestmenu");
}
}
+ /**
+ * Sets the suggestions rendered in the menu
+ *
+ * @param suggestions
+ * The suggestions to be rendered in the menu
+ */
public void setSuggestions(
Collection<FilterSelectSuggestion> suggestions) {
clearItems();
}
}
+ /**
+ * Send the current selection to the server. Triggered when a selection
+ * is made or on a blur event.
+ */
public void doSelectedItemAction() {
// do not send a value change event if null was and stays selected
final String enteredItemValue = tb.getText();
}
}
+ /**
+ * Triggered after a selection has been made
+ */
public void doPostFilterSelectedItemAction() {
final MenuItem item = getSelectedItem();
final String enteredItemValue = tb.getText();
suggestionPopup.hide();
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.terminal.gwt.client.ui.MenuBar#onBrowserEvent(com.google
+ * .gwt.user.client.Event)
+ */
@Override
public void onBrowserEvent(Event event) {
if (event.getTypeInt() == Event.ONLOAD) {
private final FlowPanel panel = new FlowPanel();
+ /**
+ * The text box where the filter is written
+ */
private final TextBox tb = new TextBox() {
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.user.client.ui.TextBoxBase#onBrowserEvent(com.google
+ * .gwt.user.client.Event)
+ */
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
private final SuggestionPopup suggestionPopup = new SuggestionPopup();
+ /**
+ * Used when measuring the width of the popup
+ */
private final HTML popupOpener = new HTML("") {
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt
+ * .user.client.Event)
+ */
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
private boolean focused = false;
private int horizPaddingAndBorder = 2;
+ /**
+ * Default constructor
+ */
public VFilterSelect() {
selectedItemIcon.setStyleName("v-icon");
selectedItemIcon.addLoadHandler(new LoadHandler() {
popupOpener.addClickHandler(this);
}
+ /**
+ * Does the Select have more pages?
+ *
+ * @return true if a next page exists, else false if the current page is the
+ * last page
+ */
public boolean hasNextPage() {
if (totalMatches > (currentPage + 1) * pageLength) {
return true;
}
}
+ /**
+ * Filters the options at a certain page. Uses the text box input as a
+ * filter
+ *
+ * @param page
+ * The page which items are to be filtered
+ */
public void filterOptions(int page) {
filterOptions(page, tb.getText());
}
+ /**
+ * Filters the options at certain page using the given filter
+ *
+ * @param page
+ * The page to filter
+ * @param filter
+ * The filter to apply to the components
+ */
public void filterOptions(int page, String filter) {
if (filter.equals(lastFilter) && currentPage == page) {
if (!suggestionPopup.isAttached()) {
currentPage = page;
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.terminal.gwt.client.Paintable#updateFromUIDL(com.vaadin.terminal
+ * .gwt.client.UIDL, com.vaadin.terminal.gwt.client.ApplicationConnection)
+ */
@SuppressWarnings("deprecation")
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
paintableId = uidl.getId();
initDone = true;
}
+ /**
+ * Turns prompting on. When prompting is turned on a command prompt is shown
+ * in the text box if nothing has been entered.
+ */
private void setPromptingOn() {
if (!prompting) {
prompting = true;
tb.setText(inputPrompt);
}
+ /**
+ * Turns prompting off. When prompting is turned on a command prompt is
+ * shown in the text box if nothing has been entered.
+ *
+ * @param text
+ * The text the text box should contain.
+ */
private void setPromptingOff(String text) {
tb.setText(text);
if (prompting) {
}
}
+ /**
+ * Triggered when a suggestion is selected
+ *
+ * @param suggestion
+ * The suggestion that just got selected.
+ */
public void onSuggestionSelected(FilterSelectSuggestion suggestion) {
selecting = false;
suggestionPopup.hide();
}
+ /**
+ * Sets the icon URI of the selected item. The icon is shown on the left
+ * side of the item caption text. Set the URI to null to remove the icon.
+ *
+ * @param iconUri
+ * The URI of the icon
+ */
private void setSelectedItemIcon(String iconUri) {
if (iconUri == null || iconUri == "") {
panel.remove(selectedItemIcon);
}
}
+ /**
+ * Positions the icon vertically in the middle. Should be called after the
+ * icon has loaded
+ */
private void updateSelectedIconPosition() {
// Position icon vertically to middle
int availableHeight = getOffsetHeight();
marginTop + "px");
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
+ * .event.dom.client.KeyDownEvent)
+ */
public void onKeyDown(KeyDownEvent event) {
if (enabled && !readonly) {
if (suggestionPopup.isAttached()) {
}
}
+ /**
+ * Triggered when a key is pressed in the text box
+ *
+ * @param event
+ * The KeyDownEvent
+ */
private void inputFieldKeyDown(KeyDownEvent event) {
switch (event.getNativeKeyCode()) {
case KeyCodes.KEY_DOWN:
}
+ /**
+ * Triggered when a key was pressed in the suggestion popup
+ *
+ * @param event
+ * The KeyDownEvent of the key
+ */
private void popupKeyDown(KeyDownEvent event) {
switch (event.getNativeKeyCode()) {
case KeyCodes.KEY_DOWN:
}
+ /**
+ * Triggered when a key was depressed
+ *
+ * @param event
+ * The KeyUpEvent of the key depressed
+ */
public void onKeyUp(KeyUpEvent event) {
if (enabled && !readonly) {
switch (event.getNativeKeyCode()) {
}
}
+ /**
+ * Resets the Select to its initial state
+ */
private void reset() {
if (currentSuggestion != null) {
String text = currentSuggestion.getReplacementString();
}
}
- /*
- * Calculate minumum width for FilterSelect textarea
+ /**
+ * Calculate minimum width for FilterSelect textarea
*/
private native int minWidth(String captions)
/*-{
return w;
}-*/;
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
+ * .dom.client.FocusEvent)
+ */
public void onFocus(FocusEvent event) {
focused = true;
if (prompting && !readonly) {
}
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
+ * .dom.client.BlurEvent)
+ */
public void onBlur(BlurEvent event) {
focused = false;
if (!readonly) {
}
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.terminal.gwt.client.Focusable#focus()
+ */
public void focus() {
focused = true;
if (prompting && !readonly) {
tb.setFocus(true);
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.google.gwt.user.client.ui.UIObject#setWidth(java.lang.String)
+ */
@Override
public void setWidth(String width) {
if (width == null || width.equals("")) {
}
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.google.gwt.user.client.ui.UIObject#setHeight(java.lang.String)
+ */
@Override
public void setHeight(String height) {
super.setHeight(height);
Util.setHeightExcludingPaddingAndBorder(tb, height, 3);
}
+ /**
+ * Calculates the width of the select if the select has undefined width.
+ * Should be called when the width changes or when the icon changes.
+ */
private void updateRootWidth() {
if (width == null) {
/*
}
}
+ /**
+ * Get the width of the select in pixels where the text area and icon has
+ * been included.
+ *
+ * @return The width in pixels
+ */
private int getMainWidth() {
int componentWidth;
if (BrowserInfo.get().isIE6()) {
return componentWidth;
}
+ /**
+ * Sets the text box width in pixels.
+ *
+ * @param componentWidth
+ * The width of the text box in pixels
+ */
private void setTextboxWidth(int componentWidth) {
int padding = getTextboxPadding();
int popupOpenerWidth = Util.getRequiredWidth(popupOpener);
tb.setWidth(textboxWidth + "px");
}
+ /**
+ * Gets the horizontal padding of the text box in pixels. The measurement
+ * includes the border width.
+ *
+ * @return The padding in pixels
+ */
private int getTextboxPadding() {
if (textboxPadding < 0) {
textboxPadding = Util.measureHorizontalPaddingAndBorder(tb
return textboxPadding;
}
+ /**
+ * Gets the horizontal padding of the select. The measurement includes the
+ * border width.
+ *
+ * @return The padding in pixels
+ */
private int getComponentPadding() {
if (componentPadding < 0) {
componentPadding = Util.measureHorizontalPaddingAndBorder(