Class SuggestBox
- java.lang.Object
-
- com.google.gwt.user.client.ui.UIObject
-
- com.google.gwt.user.client.ui.Widget
-
- com.google.gwt.user.client.ui.Composite
-
- com.google.gwt.user.client.ui.SuggestBox
-
- All Implemented Interfaces:
IsEditor<LeafValueEditor<java.lang.String>>,HasAllKeyHandlers,HasKeyDownHandlers,HasKeyPressHandlers,HasKeyUpHandlers,HasAttachHandlers,HasSelectionHandlers<SuggestOracle.Suggestion>,HasValueChangeHandlers<java.lang.String>,HasHandlers,EventListener,TakesValue<java.lang.String>,FiresSuggestionEvents,Focusable,HasAnimation,HasEnabled,HasFocus,HasText,HasValue<java.lang.String>,HasVisibility,IsRenderable,IsWidget,SourcesChangeEvents,SourcesClickEvents,SourcesFocusEvents,SourcesKeyboardEvents
public class SuggestBox extends Composite implements HasText, HasFocus, HasAnimation, HasEnabled, SourcesClickEvents, SourcesChangeEvents, SourcesKeyboardEvents, FiresSuggestionEvents, HasAllKeyHandlers, HasValue<java.lang.String>, HasSelectionHandlers<SuggestOracle.Suggestion>, IsEditor<LeafValueEditor<java.lang.String>>
ASuggestBoxis a text box or text area which displays a pre-configured set of selections that match the user's input. EachSuggestBoxis associated with a singleSuggestOracle. TheSuggestOracleis used to provide a set of selections given a specific query string.By default, the
SuggestBoxuses aMultiWordSuggestOracleas its oracle. Below we show how aMultiWordSuggestOraclecan be configured:MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(); oracle.add("Cat"); oracle.add("Dog"); oracle.add("Horse"); oracle.add("Canary"); SuggestBox box = new SuggestBox(oracle);Using the example above, if the user types "C" into the text widget, the oracle will configure the suggestions with the "Cat" and "Canary" suggestions. Specifically, whenever the user types a key into the text widget, the value is submitted to theMultiWordSuggestOracle.Note that there is no method to retrieve the "currently selected suggestion" in a SuggestBox, because there are points in time where the currently selected suggestion is not defined. For example, if the user types in some text that does not match any of the SuggestBox's suggestions, then the SuggestBox will not have a currently selected suggestion. It is more useful to know when a suggestion has been chosen from the SuggestBox's list of suggestions. A SuggestBox fires
SelectionEventswhenever a suggestion is chosen, and handlers for these events can be added using theaddSelectionHandler(SelectionHandler)method.
CSS Style Rules
- .gwt-SuggestBox
- the suggest box itself
- See Also:
SuggestOracle,MultiWordSuggestOracle,ValueBoxBase
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSuggestBox.DefaultSuggestionDisplayThe default implementation ofSuggestBox.SuggestionDisplaydisplays suggestions in aPopupPanelbeneath theSuggestBox.static interfaceSuggestBox.SuggestionCallbackThe callback used when a user selects aSuggestOracle.Suggestion.static classSuggestBox.SuggestionDisplayUsed to display suggestions to the user.-
Nested classes/interfaces inherited from class com.google.gwt.user.client.ui.UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabled
-
-
Field Summary
-
Fields inherited from class com.google.gwt.user.client.ui.Widget
eventsToSink
-
Fields inherited from class com.google.gwt.user.client.ui.UIObject
DEBUG_ID_PREFIX, MISSING_ELEMENT_ERROR, SETELEMENT_TWICE_ERROR
-
-
Constructor Summary
Constructors Constructor Description SuggestBox()Constructor forSuggestBox.SuggestBox(SuggestOracle oracle)Constructor forSuggestBox.SuggestBox(SuggestOracle oracle, ValueBoxBase<java.lang.String> box)Constructor forSuggestBox.SuggestBox(SuggestOracle oracle, ValueBoxBase<java.lang.String> box, SuggestBox.SuggestionDisplay suggestDisplay)Constructor forSuggestBox.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddChangeListener(ChangeListener listener)Deprecated.usegetTextBox()().addChangeHandler insteadvoidaddClickListener(ClickListener listener)Deprecated.usegetTextBox()().addClickHandler insteadvoidaddEventHandler(SuggestionHandler handler)voidaddFocusListener(FocusListener listener)Deprecated.usegetTextBox()().addFocusHandler/addBlurHandler() insteadvoidaddKeyboardListener(KeyboardListener listener)HandlerRegistrationaddKeyDownHandler(KeyDownHandler handler)Adds aKeyDownEventhandler.HandlerRegistrationaddKeyPressHandler(KeyPressHandler handler)Adds aKeyPressEventhandler.HandlerRegistrationaddKeyUpHandler(KeyUpHandler handler)Adds aKeyUpEventhandler.HandlerRegistrationaddSelectionHandler(SelectionHandler<SuggestOracle.Suggestion> handler)Adds aSelectionEventhandler.HandlerRegistrationaddValueChangeHandler(ValueChangeHandler<java.lang.String> handler)Adds aValueChangeEventhandler.LeafValueEditor<java.lang.String>asEditor()Returns aTakesValueEditorbacked by the SuggestBox.intgetLimit()Gets the limit for the number of suggestions that should be displayed for this box.SuggestBox.SuggestionDisplaygetSuggestionDisplay()Get theSuggestBox.SuggestionDisplayused to display suggestions.SuggestOraclegetSuggestOracle()Gets the suggest box'sSuggestOracle.intgetTabIndex()Gets the widget's position in the tab index.java.lang.StringgetText()Gets this object's text.TextBoxBasegetTextBox()Deprecated.in favour of getValueBoxjava.lang.StringgetValue()Gets this object's value.ValueBoxBase<java.lang.String>getValueBox()Get the ValueBoxBase associated with this suggest box.voidhideSuggestionList()Deprecated.booleanisAnimationEnabled()Deprecated.booleanisAutoSelectEnabled()Returns whether or not the first suggestion will be automatically selected.booleanisEnabled()Gets whether this widget is enabled.booleanisSuggestionListShowing()Check if theSuggestBox.SuggestionDisplayis showing.protected voidonEnsureDebugId(java.lang.String baseID)Called when the user sets the id using theUIObject.ensureDebugId(String)method.voidrefreshSuggestionList()Refreshes the current list of suggestions.voidremoveChangeListener(ChangeListener listener)Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().addChangeHandler insteadvoidremoveClickListener(ClickListener listener)Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().addClickHandler insteadvoidremoveEventHandler(SuggestionHandler handler)Deprecated.Use theHandlerRegistration.removeHandler()method no the object returned byaddSelectionHandler(com.google.gwt.event.logical.shared.SelectionHandler<com.google.gwt.user.client.ui.SuggestOracle.Suggestion>)insteadvoidremoveFocusListener(FocusListener listener)Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().addFocusListener insteadvoidremoveKeyboardListener(KeyboardListener listener)Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().add*Handler insteadvoidsetAccessKey(char key)Sets the widget's 'access key'.voidsetAnimationEnabled(boolean enable)Deprecated.voidsetAutoSelectEnabled(boolean selectsFirstItem)Turns on or off the behavior that automatically selects the first suggested item.voidsetEnabled(boolean enabled)Sets whether this widget is enabled.voidsetFocus(boolean focused)Explicitly focus/unfocus this widget.voidsetLimit(int limit)Sets the limit to the number of suggestions the oracle should provide.voidsetPopupStyleName(java.lang.String style)Deprecated.voidsetTabIndex(int index)Sets the widget's position in the tab index.voidsetText(java.lang.String text)Sets this object's text.voidsetValue(java.lang.String newValue)Sets this object's value without firing any events.voidsetValue(java.lang.String value, boolean fireEvents)Sets this object's value.voidshowSuggestionList()Show the current list of suggestions.(package private) voidshowSuggestions(java.lang.String query)static SuggestBoxwrap(SuggestOracle oracle, Element element)Creates aSuggestBoxwidget that wraps an existing <input type='text'> element.-
Methods inherited from class com.google.gwt.user.client.ui.Composite
claimElement, getWidget, initializeClaimedElement, initWidget, isAttached, onAttach, onBrowserEvent, onDetach, render, render, resolvePotentialElement, setWidget
-
Methods inherited from class com.google.gwt.user.client.ui.Widget
addAttachHandler, addBitlessDomHandler, addDomHandler, addHandler, asWidget, asWidgetOrNull, createHandlerManager, delegateEvent, doAttachChildren, doDetachChildren, ensureHandlers, fireEvent, getHandlerCount, getHandlerManager, getLayoutData, getParent, isOrWasAttached, onLoad, onUnload, removeFromParent, replaceElement, setLayoutData, setParent, sinkEvents, unsinkEvents
-
Methods inherited from class com.google.gwt.user.client.ui.UIObject
addStyleDependentName, addStyleName, ensureDebugId, ensureDebugId, ensureDebugId, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, removeStyleDependentName, removeStyleName, setElement, setElement, setHeight, setPixelSize, setSize, setStyleDependentName, setStyleName, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkBitlessEvent, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.google.gwt.event.shared.HasHandlers
fireEvent
-
-
-
-
Constructor Detail
-
SuggestBox
public SuggestBox()
Constructor forSuggestBox. Creates aMultiWordSuggestOracleandTextBoxto use with thisSuggestBox.
-
SuggestBox
public SuggestBox(SuggestOracle oracle)
- Parameters:
oracle- the oracle for thisSuggestBox
-
SuggestBox
public SuggestBox(SuggestOracle oracle, ValueBoxBase<java.lang.String> box)
Constructor forSuggestBox. The text box will be removed from it's current location and wrapped by theSuggestBox.- Parameters:
oracle- supplies suggestions based upon the current contents of the text widgetbox- the text widget
-
SuggestBox
public SuggestBox(SuggestOracle oracle, ValueBoxBase<java.lang.String> box, SuggestBox.SuggestionDisplay suggestDisplay)
Constructor forSuggestBox. The text box will be removed from it's current location and wrapped by theSuggestBox.- Parameters:
oracle- supplies suggestions based upon the current contents of the text widgetbox- the text widgetsuggestDisplay- the class used to display suggestions
-
-
Method Detail
-
wrap
public static SuggestBox wrap(SuggestOracle oracle, Element element)
Creates aSuggestBoxwidget that wraps an existing <input type='text'> element. This element must already be attached to the document. If the element is removed from the document, you must callRootPanel.detachNow(Widget).- Parameters:
oracle- the suggest box oracle to useelement- the element to be wrapped
-
addChangeListener
@Deprecated public void addChangeListener(ChangeListener listener)
Deprecated.usegetTextBox()().addChangeHandler insteadAdds a listener to receive change events on the SuggestBox's text box. The source Widget for these events will be the SuggestBox.- Specified by:
addChangeListenerin interfaceSourcesChangeEvents- Parameters:
listener- the listener interface to add
-
addClickListener
@Deprecated public void addClickListener(ClickListener listener)
Deprecated.usegetTextBox()().addClickHandler insteadAdds a listener to receive click events on the SuggestBox's text box. The source Widget for these events will be the SuggestBox.- Specified by:
addClickListenerin interfaceSourcesClickEvents- Parameters:
listener- the listener interface to add
-
addEventHandler
@Deprecated public void addEventHandler(SuggestionHandler handler)
Deprecated.Adds an event to this handler.- Specified by:
addEventHandlerin interfaceFiresSuggestionEvents- Parameters:
handler- the handler to add
-
addFocusListener
@Deprecated public void addFocusListener(FocusListener listener)
Deprecated.usegetTextBox()().addFocusHandler/addBlurHandler() insteadAdds a listener to receive focus events on the SuggestBox's text box. The source Widget for these events will be the SuggestBox.- Specified by:
addFocusListenerin interfaceSourcesFocusEvents- Parameters:
listener- the listener interface to add
-
addKeyboardListener
@Deprecated public void addKeyboardListener(KeyboardListener listener)
Deprecated.Description copied from interface:SourcesKeyboardEventsAdds a listener interface to receive keyboard events.- Specified by:
addKeyboardListenerin interfaceSourcesKeyboardEvents- Parameters:
listener- the listener interface to add
-
addKeyDownHandler
public HandlerRegistration addKeyDownHandler(KeyDownHandler handler)
Description copied from interface:HasKeyDownHandlersAdds aKeyDownEventhandler.- Specified by:
addKeyDownHandlerin interfaceHasKeyDownHandlers- Parameters:
handler- the key down handler- Returns:
HandlerRegistrationused to remove this handler
-
addKeyPressHandler
public HandlerRegistration addKeyPressHandler(KeyPressHandler handler)
Description copied from interface:HasKeyPressHandlersAdds aKeyPressEventhandler.- Specified by:
addKeyPressHandlerin interfaceHasKeyPressHandlers- Parameters:
handler- the key press handler- Returns:
HandlerRegistrationused to remove this handler
-
addKeyUpHandler
public HandlerRegistration addKeyUpHandler(KeyUpHandler handler)
Description copied from interface:HasKeyUpHandlersAdds aKeyUpEventhandler.- Specified by:
addKeyUpHandlerin interfaceHasKeyUpHandlers- Parameters:
handler- the key up handler- Returns:
HandlerRegistrationused to remove this handler
-
addSelectionHandler
public HandlerRegistration addSelectionHandler(SelectionHandler<SuggestOracle.Suggestion> handler)
Description copied from interface:HasSelectionHandlersAdds aSelectionEventhandler.- Specified by:
addSelectionHandlerin interfaceHasSelectionHandlers<SuggestOracle.Suggestion>- Parameters:
handler- the handler- Returns:
- the registration for the event
-
addValueChangeHandler
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<java.lang.String> handler)
Description copied from interface:HasValueChangeHandlersAdds aValueChangeEventhandler.- Specified by:
addValueChangeHandlerin interfaceHasValueChangeHandlers<java.lang.String>- Parameters:
handler- the handler- Returns:
- the registration for the event
-
asEditor
public LeafValueEditor<java.lang.String> asEditor()
Returns aTakesValueEditorbacked by the SuggestBox.- Specified by:
asEditorin interfaceIsEditor<LeafValueEditor<java.lang.String>>- Returns:
- an
Editorof type E
-
getLimit
public int getLimit()
Gets the limit for the number of suggestions that should be displayed for this box. It is up to the currentSuggestOracleto enforce this limit.- Returns:
- the limit for the number of suggestions
-
getSuggestionDisplay
public SuggestBox.SuggestionDisplay getSuggestionDisplay()
Get theSuggestBox.SuggestionDisplayused to display suggestions.- Returns:
- the
SuggestBox.SuggestionDisplay
-
getSuggestOracle
public SuggestOracle getSuggestOracle()
Gets the suggest box'sSuggestOracle.- Returns:
- the
SuggestOracle
-
getTabIndex
public int getTabIndex()
Description copied from interface:FocusableGets the widget's position in the tab index.- Specified by:
getTabIndexin interfaceFocusable- Returns:
- the widget's tab index
-
getText
public java.lang.String getText()
Description copied from interface:HasTextGets this object's text.
-
getTextBox
@Deprecated public TextBoxBase getTextBox()
Deprecated.in favour of getValueBoxGet the text box associated with this suggest box.- Returns:
- this suggest box's text box
- Throws:
java.lang.ClassCastException- if this suggest box's value box is not an instance of TextBoxBase
-
getValue
public java.lang.String getValue()
Description copied from interface:HasValueGets this object's value.- Specified by:
getValuein interfaceHasValue<java.lang.String>- Specified by:
getValuein interfaceTakesValue<java.lang.String>- Returns:
- the object's value
- See Also:
TakesValue.setValue(V)
-
getValueBox
public ValueBoxBase<java.lang.String> getValueBox()
Get the ValueBoxBase associated with this suggest box.- Returns:
- this suggest box's value box
-
hideSuggestionList
@Deprecated public void hideSuggestionList()
Deprecated.Hide current suggestions in theSuggestBox.DefaultSuggestionDisplay. Note that this method is a no-op unless theSuggestBox.DefaultSuggestionDisplayis used.
-
isAnimationEnabled
@Deprecated public boolean isAnimationEnabled()
Deprecated.Check whether or not theSuggestBox.DefaultSuggestionDisplayhas animations enabled. Note that this method only has a meaningful return value when theSuggestBox.DefaultSuggestionDisplayis used.- Specified by:
isAnimationEnabledin interfaceHasAnimation
-
isAutoSelectEnabled
public boolean isAutoSelectEnabled()
Returns whether or not the first suggestion will be automatically selected. This behavior is on by default.- Returns:
- true if the first suggestion will be automatically selected
-
isEnabled
public boolean isEnabled()
Gets whether this widget is enabled.- Specified by:
isEnabledin interfaceHasEnabled- Returns:
trueif the widget is enabled
-
isSuggestionListShowing
public boolean isSuggestionListShowing()
Check if theSuggestBox.SuggestionDisplayis showing.- Returns:
- true if the list of suggestions is currently showing, false if not
-
refreshSuggestionList
public void refreshSuggestionList()
Refreshes the current list of suggestions.
-
removeChangeListener
@Deprecated public void removeChangeListener(ChangeListener listener)
Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().addChangeHandler insteadDescription copied from interface:SourcesChangeEventsRemoves a previously added listener interface.- Specified by:
removeChangeListenerin interfaceSourcesChangeEvents- Parameters:
listener- the listener interface to remove
-
removeClickListener
@Deprecated public void removeClickListener(ClickListener listener)
Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().addClickHandler insteadDescription copied from interface:SourcesClickEventsRemoves a previously added listener interface.- Specified by:
removeClickListenerin interfaceSourcesClickEvents- Parameters:
listener- the listener interface to remove
-
removeEventHandler
@Deprecated public void removeEventHandler(SuggestionHandler handler)
Deprecated.Use theHandlerRegistration.removeHandler()method no the object returned byaddSelectionHandler(com.google.gwt.event.logical.shared.SelectionHandler<com.google.gwt.user.client.ui.SuggestOracle.Suggestion>)insteadDescription copied from interface:FiresSuggestionEventsRemoves a previously added handler interface.- Specified by:
removeEventHandlerin interfaceFiresSuggestionEvents- Parameters:
handler- the handler to remove
-
removeFocusListener
@Deprecated public void removeFocusListener(FocusListener listener)
Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().addFocusListener insteadDescription copied from interface:SourcesFocusEventsRemoves a previously added listener interface.- Specified by:
removeFocusListenerin interfaceSourcesFocusEvents- Parameters:
listener- the listener interface to remove
-
removeKeyboardListener
@Deprecated public void removeKeyboardListener(KeyboardListener listener)
Deprecated.Use theHandlerRegistration.removeHandler()method on the object returned bygetTextBox()().add*Handler insteadDescription copied from interface:SourcesKeyboardEventsRemoves a previously added listener interface.- Specified by:
removeKeyboardListenerin interfaceSourcesKeyboardEvents- Parameters:
listener- the listener interface to remove
-
setAccessKey
public void setAccessKey(char key)
Description copied from interface:FocusableSets the widget's 'access key'. This key is used (in conjunction with a browser-specific modifier key) to automatically focus the widget.- Specified by:
setAccessKeyin interfaceFocusable- Parameters:
key- the widget's access key
-
setAnimationEnabled
@Deprecated public void setAnimationEnabled(boolean enable)
Deprecated.Enable or disable animations in theSuggestBox.DefaultSuggestionDisplay. Note that this method is a no-op unless theSuggestBox.DefaultSuggestionDisplayis used.- Specified by:
setAnimationEnabledin interfaceHasAnimation- Parameters:
enable- true to enable, false to disable
-
setAutoSelectEnabled
public void setAutoSelectEnabled(boolean selectsFirstItem)
Turns on or off the behavior that automatically selects the first suggested item. This behavior is on by default.- Parameters:
selectsFirstItem- Whether or not to automatically select the first suggestion
-
setEnabled
public void setEnabled(boolean enabled)
Sets whether this widget is enabled.- Specified by:
setEnabledin interfaceHasEnabled- Parameters:
enabled-trueto enable the widget,falseto disable it
-
setFocus
public void setFocus(boolean focused)
Description copied from interface:FocusableExplicitly focus/unfocus this widget. Only one widget can have focus at a time, and the widget that does will receive all keyboard events. NOTE: Most browsers fire FocusEvents asynchronously. Especially within GWT tests, you'll need to make your test asynchronous to properly do verifications. SeeGWTTestCase#delayTestFinishfor more information on how to do this.
-
setLimit
public void setLimit(int limit)
Sets the limit to the number of suggestions the oracle should provide. It is up to the oracle to enforce this limit.- Parameters:
limit- the limit to the number of suggestions provided
-
setPopupStyleName
@Deprecated public void setPopupStyleName(java.lang.String style)
Deprecated.Sets the style name of the suggestion popup in theSuggestBox.DefaultSuggestionDisplay. Note that this method is a no-op unless theSuggestBox.DefaultSuggestionDisplayis used.- Parameters:
style- the new primary style name- See Also:
UIObject.setStyleName(String)
-
setTabIndex
public void setTabIndex(int index)
Description copied from interface:FocusableSets the widget's position in the tab index. If more than one widget has the same tab index, each such widget will receive focus in an arbitrary order. Setting the tab index to-1will cause this widget to be removed from the tab order.- Specified by:
setTabIndexin interfaceFocusable- Parameters:
index- the widget's tab index
-
setText
public void setText(java.lang.String text)
Description copied from interface:HasTextSets this object's text.
-
setValue
public void setValue(java.lang.String newValue)
Description copied from interface:HasValueSets this object's value without firing any events. This should be identical to calling setValue(value, false).It is acceptable to fail assertions or throw (documented) unchecked exceptions in response to bad values.
Widgets must accept null as a valid value. By convention, setting a widget to null clears value, calling getValue() on a cleared widget returns null. Widgets that can not be cleared (e.g.
CheckBox) must find another valid meaning for null input.- Specified by:
setValuein interfaceHasValue<java.lang.String>- Specified by:
setValuein interfaceTakesValue<java.lang.String>- Parameters:
newValue- the object's new value- See Also:
TakesValue.getValue()
-
setValue
public void setValue(java.lang.String value, boolean fireEvents)Description copied from interface:HasValueSets this object's value. FiresValueChangeEventwhen fireEvents is true and the new value does not equal the existing value.It is acceptable to fail assertions or throw (documented) unchecked exceptions in response to bad values.
-
showSuggestionList
public void showSuggestionList()
Show the current list of suggestions.
-
onEnsureDebugId
protected void onEnsureDebugId(java.lang.String baseID)
Description copied from class:UIObjectCalled when the user sets the id using theUIObject.ensureDebugId(String)method. Subclasses ofUIObjectcan override this method to add IDs to their sub elements. If a subclass does override this method, it should list the IDs (relative to the base ID), that will be applied to each subElementwith a short description. For example:- -mysubelement = Applies to my sub element.
<inherits name="com.google.gwt.user.Debug"/>
- Overrides:
onEnsureDebugIdin classUIObject- Parameters:
baseID- the base ID used by the main element
-
showSuggestions
void showSuggestions(java.lang.String query)
-
-