Class FTokenField

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.Composite<com.vaadin.flow.component.Component>
com.vaadin.flow.component.AbstractCompositeField<com.vaadin.flow.component.Component,CustomField<Object>,Object>
com.vaadin.featurepack.ui.CustomField<Object>
com.vaadin.featurepack.ui.FTokenField
All Implemented Interfaces:
Buffered, BufferedValidatable, Container.Editor, Container.Viewer, Property<Object>, Property.Editor, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer, Field<Object>, Validatable, ClientConnector, FAbstractClientConnector, FSizeable, HasSizeable, FAbstractComponent, Focusable, HasComponents, HasFInternalValue<Object>, com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.BlurNotifier<com.vaadin.flow.component.html.Div>, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.Focusable<com.vaadin.flow.component.html.Div>, com.vaadin.flow.component.FocusNotifier<com.vaadin.flow.component.html.Div>, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CustomField<Object>,Object>,Object>, com.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CustomField<Object>,Object>,Object>, Serializable, Iterable<com.vaadin.flow.component.Component>

public class FTokenField extends CustomField<Object> implements Container.Editor
A kind of multiselect ComboBox with Vaadin Framework 7 API. When the user selects a token (or inputs a new token, TokenField defaults to allowing new tokens), the value is added as a clickable "token button" before or after the input box. Duplicate selections are not allowed.

TokenField defaults to using Div, but virtually any layout can be used.

Can be customized in several ways by overriding certain methods. When the user select or enters a new token, the following happens:

Custom functionality when adding and removing tokens, such as showing a notification for duplicates or confirming removal, is done by overriding onTokenInput(Object) and onTokenClick(Object) respectively. In much the same way, onTokenDelete(Object) is called when the user presses delete or backspace when the input is empty, and can be customized.
The token buttons can be styled customized by overriding
invalid @link
{@link #configureTokenButton(Object, FButton).

The content of the input (ComboBox) can be bound to a Container datasource, and filtering can be used. Note that the TokenField can select values that are not present in the ComboBox.

Also note that if you use setTokenCaptionPropertyId(Object) (to use a specific property as token caption) AND allow new tokens to be input ( setNewTokensAllowed(boolean), you should probably use a custom

invalid @link
{@link NewItemHandler) ({@link
#setNewTokenHandler(NewItemHandler)}) in order to provide a sensible caption for the new token.

TokenField is a full-fledged field - it can be bound to a Property datasource, and supports buffering.

Note "token" and "tokenId" is often used interchangeably in the documentation - usually the token is just a string that is the id as well. The term Token as used in the method names is often interchangeable with the term item seen elsewhere in the Vaadin API; e.g setTokenCaption(Object, String) works exactly as FComboBox.setItemCaption(Object, String), and tokenId is the same as itemId.

See Also:
  • Field Details

    • STYLE_TOKENFIELD

      public static final String STYLE_TOKENFIELD
      See Also:
    • STYLE_TOKENTEXTFIELD

      public static final String STYLE_TOKENTEXTFIELD
      See Also:
    • STYLE_BUTTON_EMPHAZISED

      public static final String STYLE_BUTTON_EMPHAZISED
      See Also:
    • layout

      protected com.vaadin.flow.component.HasComponents layout
      The layout currently in use
    • insertPosition

      protected FTokenField.InsertPosition insertPosition
      Current insert position
    • cb

      protected FComboBox cb
      The ComboBox used for input
    • buttons

      protected LinkedHashMap<Object,FButton> buttons
      Maps the tokenId (itemId) to the token button
    • rememberNewTokens

      protected boolean rememberNewTokens
  • Constructor Details

    • FTokenField

      public FTokenField(String caption, FTokenField.InsertPosition insertPosition)
      Create a new TokenField with a caption and a FTokenField.InsertPosition.
      Parameters:
      caption - the desired caption
      insertPosition - the desired insert position
    • FTokenField

      public FTokenField(String caption)
      Create a new TokenField with a caption.
      Parameters:
      caption - the desired caption
    • FTokenField

      public FTokenField()
      Create a new TokenField.
    • FTokenField

      public FTokenField(String caption, Layout lo)
      Create a new TokenField with a caption and a given layout.
      Parameters:
      caption - the desired caption
      lo - the desired layout
    • FTokenField

      public FTokenField(String caption, Layout lo, FTokenField.InsertPosition insertPosition)
      Create a new TokenField with a caption, a given layout, and the specified token insert position.
      Parameters:
      caption - the desired caption
      lo - the desired layout
      insertPosition - the desired token insert position
    • FTokenField

      public FTokenField(Layout lo, FTokenField.InsertPosition insertPosition)
      Create a new TokenField with the given layout, and the specified token insert position.
      Parameters:
      lo - the desired layout
      insertPosition - the desired token insert position
    • FTokenField

      public FTokenField(com.vaadin.flow.component.HasComponents lo)
      Create a new TokenField with the given layout.
      Parameters:
      lo - the desired layout
  • Method Details

    • rememberToken

      protected void rememberToken(String tokenId)
    • setInternalValue

      protected void setInternalValue(Object newValue)
      Overrides:
      setInternalValue in class CustomField<Object>
    • onTokenInput

      protected void onTokenInput(Object tokenId)
      Called when the user is adding a new token via the UI; called after the newItemHandler. Can be used to make customize the adding process; e.g. to notify that the token was not added because it's duplicate, to ask for additional information, or to disallow addition due to some heuristics (not both A and Q).
      The default is to call addToken(Object) which will add the token if it's not a duplicate.
      Parameters:
      tokenId - the token id selected (or input)
    • onTokenClick

      protected void onTokenClick(Object tokenId)
      Called when the token button is clicked, which by default removes the token by calling removeToken(Object). The behavior can be customized, e.g. present a confirmation dialog.
      Parameters:
      tokenId - the id of the token that was clicked
    • onTokenDelete

      protected void onTokenDelete(Object tokenId)
      Called with the last added token when the delete or backspace -key (depending on insert position) is pressed in an empty input. The default is to call onTokenClick(Object) with the last added token, i.e. remove last. The behavior can be customized, e.g. present a confirmation dialog.
    • addToken

      public void addToken(Object tokenId)
      Adds a token if that token does not already exist.

      Note that tokens are not automatically added to the token container. This means you can add tokens without adding them to the container (that might be bound to some data store), and without making them available to the user in the suggestion dropdown.
      This also means that when new tokens are disallowed ( setNewTokensAllowed(boolean)) you can programmatically add tokens that the user can not add him/herself.
      Consider adding the token to the container before calling addToken(Object) if you're using a custom captions based on container/item properties, or if you want the token to be available to the user as a suggestion later.

      Parameters:
      tokenId - the token to add
    • removeToken

      public void removeToken(Object tokenId)
      Removes the given token.

      Note that the token is not removed from the container, so if it exists in the container, the token will still be available to the user.

      Parameters:
      tokenId - the token to remove
    • configureTokenButton

      protected void configureTokenButton(Object tokenId, FButton button)
      Configures the token button.

      By default, the caption, icon, description, and style is set. Override to customize.
      Note that the default click-listener is added elsewhere and can not be changed here.

      Parameters:
      tokenId - the token this button pertains to
      button - the button to be configured
    • getLayout

      public com.vaadin.flow.component.HasComponents getLayout()
      Gets the layout currently in use.
      Returns:
      the current layout
    • setLayout

      protected void setLayout(com.vaadin.flow.component.HasComponents newLayout)
      Sets layout used for laying out the tokens and the input.
      Parameters:
      newLayout - the layout to use
    • getTokenInsertPosition

      public FTokenField.InsertPosition getTokenInsertPosition()
      Gets the current token FTokenField.InsertPosition.
      The token buttons are be placed at this position, relative to the input box.
      Returns:
      the current token insert position
      See Also:
    • setTokenInsertPosition

      public void setTokenInsertPosition(FTokenField.InsertPosition insertPosition)
      Sets the token FTokenField.InsertPosition.
      The token buttons will be placed at this position, relative to the input box.
      See Also:
    • addValueChangeListener

      public com.vaadin.flow.shared.Registration addValueChangeListener(com.vaadin.flow.component.HasValue.ValueChangeListener valueChangeListener)
      Specified by:
      addValueChangeListener in interface com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CustomField<Object>,Object>,Object>
      Overrides:
      addValueChangeListener in class com.vaadin.flow.component.AbstractCompositeField<com.vaadin.flow.component.Component,CustomField<Object>,Object>
    • getOptionalValue

      public Optional getOptionalValue()
      Specified by:
      getOptionalValue in interface com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CustomField<Object>,Object>,Object>
    • clear

      public void clear()
      Specified by:
      clear in interface com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CustomField<Object>,Object>,Object>
    • setReadOnly

      public void setReadOnly(boolean readOnly)
      Specified by:
      setReadOnly in interface com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CustomField<Object>,Object>,Object>
      Specified by:
      setReadOnly in interface com.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CustomField<Object>,Object>,Object>
    • setContainerDataSource

      public void setContainerDataSource(Container c)
      Sets the Container data source used for the input box. This works exactly as Select.setContainerDataSource(Container).
      Specified by:
      setContainerDataSource in interface Container.Viewer
      Parameters:
      c - the token container data source
      See Also:
    • getContainerDataSource

      public Container getContainerDataSource()
      Gets the Container data source currently used for the input box. This works exactly as Select.getContainerDataSource().
      Specified by:
      getContainerDataSource in interface Container.Viewer
      Returns:
      the container used to tokens
      See Also:
    • setNewTokensAllowed

      public void setNewTokensAllowed(boolean allowNewTokens)
      Sets whether or not tokens entered by the user that not present in the container are allowed. When true, the token is added, and if setRememberNewTokens(boolean) is true, the new token will be added to the container as well.
      Parameters:
      allowNewTokens - true to allow tokens that are not in the container
      See Also:
    • isNewTokensAllowed

      public boolean isNewTokensAllowed()
      Checks whether or not new tokens are allowed
      Returns:
      See Also:
    • isRememberNewTokens

      public boolean isRememberNewTokens()
      If true, new tokens entered by the user are automatically added to the container.
      Returns:
      true if tokens are automatically added
    • setRememberNewTokens

      public void setRememberNewTokens(boolean rememberNewTokens)
      Provided new tokens are allowed (setNewTokensAllowed(boolean)), this sets whether or not new tokens entered by the user are automatically added to the container.
      Parameters:
      rememberNewTokens - true to add new tokens automatically
    • setFilteringMode

      public void setFilteringMode(FilteringMode filteringMode)
      Parameters:
      filteringMode - the desired filtering mode
      See Also:
    • getFilteringMode

      public FilteringMode getFilteringMode()
      See Also:
    • focus

      public void focus()
      Description copied from interface: Focusable
      Sets the focus to this component.
      Specified by:
      focus in interface Focusable
      Specified by:
      focus in interface com.vaadin.flow.component.Focusable<com.vaadin.flow.component.html.Div>
      Overrides:
      focus in class CustomField<Object>
    • getInputPrompt

      public String getInputPrompt()
      Gets the input prompt; works as HasPlaceholder.getPlaceholder().
      Returns:
      the current input prompt
      See Also:
      • HasPlaceholder.getPlaceholder()
    • getTokenCaption

      public String getTokenCaption(Object tokenId)
      Gets the caption for the given token; the caption can be based on a property, just as in a ComboBox. Note that the string representation of the tokenId itself is always used if the container does not contain the id.
      Parameters:
      tokenId - the id of the token
      Returns:
      the caption
    • getTokenCaptionMode

      public FAbstractSelect.ItemCaptionMode getTokenCaptionMode()
      Returns:
      the current caption mode
      See Also:
    • getTokenCaptionPropertyId

      public Object getTokenCaptionPropertyId()
      Returns:
      the current caption property id
      See Also:
    • getTokenIcon

      public Resource getTokenIcon(Object tokenId)
      Returns:
      the icon for the given resource
      See Also:
    • getTokenIconPropertyId

      public Object getTokenIconPropertyId()
      Returns:
      the current item icon property id
      See Also:
    • getTokenIds

      public Collection getTokenIds()
      Gets all tokenIds currently in the token container.
      Returns:
      a collection of all tokenIds in the container
    • getTabIndex

      public int getTabIndex()
      Description copied from interface: Focusable
      Gets the tabulator index of the Focusable component.
      Specified by:
      getTabIndex in interface Focusable
      Specified by:
      getTabIndex in interface com.vaadin.flow.component.Focusable<com.vaadin.flow.component.html.Div>
      Overrides:
      getTabIndex in class CustomField<Object>
      Returns:
      tab index set for the Focusable component
      See Also:
    • setHeight

      public void setHeight(float height, com.vaadin.flow.component.Unit unit)
      Description copied from interface: FSizeable
      Sets the height of the object. Negative number implies unspecified size (terminal is free to set the size).
      Specified by:
      setHeight in interface FSizeable
      Specified by:
      setHeight in interface com.vaadin.flow.component.HasSize
      Specified by:
      setHeight in interface HasSizeable
      Overrides:
      setHeight in class CustomField<Object>
      Parameters:
      height - the height of the object.
      unit - the unit used for the width.
    • setWidth

      public void setWidth(float width, com.vaadin.flow.component.Unit unit)
      Description copied from interface: FSizeable
      Sets the width of the object. Negative number implies unspecified size (terminal is free to set the size).
      Specified by:
      setWidth in interface FSizeable
      Specified by:
      setWidth in interface com.vaadin.flow.component.HasSize
      Specified by:
      setWidth in interface HasSizeable
      Overrides:
      setWidth in class CustomField<Object>
      Parameters:
      width - the width of the object.
      unit - the unit used for the width.
    • setSizeFull

      public void setSizeFull()
      Description copied from interface: FSizeable
      Sets the size to 100% x 100%.
      Specified by:
      setSizeFull in interface FSizeable
      Specified by:
      setSizeFull in interface com.vaadin.flow.component.HasSize
      Specified by:
      setSizeFull in interface HasSizeable
      Overrides:
      setSizeFull in class CustomField<Object>
    • setSizeUndefined

      public void setSizeUndefined()
      Description copied from interface: FSizeable
      Clears any size settings.
      Specified by:
      setSizeUndefined in interface FSizeable
      Specified by:
      setSizeUndefined in interface com.vaadin.flow.component.HasSize
      Specified by:
      setSizeUndefined in interface HasSizeable
      Overrides:
      setSizeUndefined in class CustomField<Object>
    • setInputHeight

      public void setInputHeight(String height)
    • setInputWidth

      public void setInputWidth(String width)
    • setInputHeight

      public void setInputHeight(float height, com.vaadin.flow.component.Unit unit)
    • setInputWidth

      public void setInputWidth(float width, com.vaadin.flow.component.Unit unit)
    • setInputSizeFull

      public void setInputSizeFull()
    • setInputSizeUndefined

      public void setInputSizeUndefined()
    • setInputPrompt

      public void setInputPrompt(String inputPrompt)
      Sets the input prompt; works as HasPlaceholder.setPlaceholder(String).
      See Also:
      • HasPlaceholder.setPlaceholder(String)
    • setTokenCaption

      public void setTokenCaption(Object tokenId, String caption)
      sets the caption for the given token.
      Parameters:
      tokenId - token whose caption to set
      caption - the desired caption
      See Also:
    • setTokenCaptionMode

      public void setTokenCaptionMode(FAbstractSelect.ItemCaptionMode mode)
      Parameters:
      mode -
      See Also:
    • setTokenCaptionPropertyId

      public void setTokenCaptionPropertyId(Object propertyId)
      Parameters:
      propertyId -
      See Also:
    • setTokenIcon

      public void setTokenIcon(Object tokenId, Resource icon)
      Parameters:
      tokenId -
      icon -
      See Also:
    • setTokenIconPropertyId

      public void setTokenIconPropertyId(Object propertyId)
      Parameters:
      propertyId -
      See Also:
    • setTabIndex

      public void setTabIndex(int tabIndex)
      Description copied from interface: Focusable
      Sets the tabulator index of the Focusable component. The tab index property is used to specify the order in which the fields are focused when the user presses the Tab key. Components with a defined tab index are focused sequentially first, and then the components with no tab index.

      After all focusable user interface components are done, the browser can begin again from the component with the smallest tab index, or it can take the focus out of the page, for example, to the location bar.

      If the tab index is not set (is set to zero), the default tab order is used. The order is somewhat browser-dependent, but generally follows the HTML structure of the page.

      A negative value means that the component is completely removed from the tabulation order and can not be reached by pressing the Tab key at all.

      Specified by:
      setTabIndex in interface Focusable
      Specified by:
      setTabIndex in interface com.vaadin.flow.component.Focusable<com.vaadin.flow.component.html.Div>
      Overrides:
      setTabIndex in class CustomField<Object>
      Parameters:
      tabIndex - the tab order of this component. Indexes usually start from 1. Zero means that default tab order should be used. A negative value means that the field should not be included in the tabbing sequence.
      See Also:
    • getType

      public Class<?> getType()
      Description copied from interface: Property
      Returns the type of the Property. The methods getValue and setValue must be compatible with this type: one must be able to safely cast the value returned from getValue to the given type and pass any variable assignable to this type as an argument to setValue.
      Specified by:
      getType in interface Field<Object>
      Specified by:
      getType in interface Property<Object>
      Returns:
      type of the Property
    • initContent

      protected com.vaadin.flow.component.Component initContent()
      Description copied from class: CustomField
      Create the content component or layout for the field. Subclasses of CustomField should implement this method.

      Note that this method is called when the CustomField is attached to a layout or when CustomField.getContent() is called explicitly for the first time. It is only called once for a CustomField.

      Specified by:
      initContent in class CustomField<Object>
      Returns:
      Component representing the UI of the CustomField