com.databasesandlife.util.wicket
Class MultipleValueAutoCompleteTextField
java.lang.Object
org.apache.wicket.Component
org.apache.wicket.MarkupContainer
org.apache.wicket.markup.html.WebMarkupContainer
org.apache.wicket.markup.html.form.LabeledWebMarkupContainer
org.apache.wicket.markup.html.form.FormComponent<T>
org.apache.wicket.markup.html.form.FormComponentPanel<String[]>
com.databasesandlife.util.wicket.MultipleValueAutoCompleteTextField
- All Implemented Interfaces:
- Serializable, IClusterable, IConverterLocator, IFormModelUpdateListener, IFormVisitorParticipant, ILabelProvider<String>, IHeaderPartContainerProvider
public class MultipleValueAutoCompleteTextField
- extends FormComponentPanel<String[]>
A text-field where the user may enter multiple values, but each value may only be one of a pre-defined list of values. Similar to Facebook's message compose screen, where users may only enter recipients who are in their address book.

If the user should be allowed to enter values which are not in the pre-defined list of values (e.g. the "To:" field in Gmail, where email addresses are suggested, but any email address may be entered), see MultipleValueAutoSuggestTextField.
The list of values that are allowed may either be client-side (meaning they are specified at the time the text field is created, and are placed in the generated HTML) or they may be server-side (meaning the application must specify a callback, which is called each time the user starts to enter something, and which must return a list of values matching what the user has entered). The former is appropriate for smaller lists, as it is faster (no server round-trip), the latter for larger lists (as otherwise the generated HTML would be too large.)
Each value may have an internal ID and an external string that the user sees. The model of the field is a list of the internal IDs. The data source for the suggestions (either client-side or server-side) is an ordered list of ID and displayable string pairs. IDs are strings (but that doesn't stop them being string representations of numbers, of course).
Usage:
<!-- In HTML -->
<input type="text" wicket:id="languages"></input>
// In Java
MultipleValueAutoCompleteTextField f =
new MultipleValueAutoCompleteTextField("languages");
f.setClientSideOptions(new AutoCompleteOption[] {
new AutoCompleteOption("en", "English"),
new AutoCompleteOption("de", "German"),
}); // or...
f.setServerSideDataSource(new AutoCompleteDataSource() {
AutoCompleteOption[] suggestOptions(String userEnteredText) {
return ....;
}
});
form.add(f);
By default the values (e.g. "English") are plain text strings. If you want to use HTML effects (e.g. have a few <span>s inside the value, with different styles), then subclass AutoCompleteOption and override getHtmlDisplayText. Due to the way the JS is programmed, use <font class="x"> as opposed to <span class="x">.
The text field is always on a separate line, this is a consequence of the Javascript library used, and seemed to be the case for all other Javascript libraries I saw as well. The solution, in case you want to have the text field on a line with a field name such as "To:", is either to use absolute positioning, or use tables.
If there is an error about a missing </span> tag, make sure the <input> tag is closed with a </input>, even though HTML would not normally require it to be closed. This is a consequence of an implementation issue.
The Javascript software used is jQuery Tokeninput.
Bugs
- Text such as "Loading..." should be localizable
- Version:
- $Revision: 2679 $
- Author:
- The Java source is copyright Adrian Smith and licensed under the LGPL 3.
- See Also:
- Serialized Form
| Methods inherited from class org.apache.wicket.markup.html.form.FormComponent |
add, add, clearInput, error, getConvertedInput, getForm, getInput, getInputAsArray, getInputName, getModel, getModelObject, getRawInput, getType, getValidatorKeyPrefix, getValidators, getValue, hasRawInput, inputChanged, invalid, isInputNullable, isMultiPart, isPersistent, isRequired, isValid, newValidatable, processChildren, processInput, setConvertedInput, setLabel, setModel, setModelObject, setModelValue, setModelValue, setPersistent, setRequired, setType, updateModel, valid, validate, visitComponentsPostOrder, visitFormComponentsPostOrder |
| Methods inherited from class org.apache.wicket.MarkupContainer |
add, addOrReplace, autoAdd, autoAdd, contains, get, get, getAssociatedMarkupStream, getMarkupStream, hasAssociatedMarkup, internalAdd, isTransparentResolver, iterator, iterator, newMarkupResourceStream, remove, remove, removeAll, renderAssociatedMarkup, replace, setDefaultModel, size, swap, toString, toString, visitChildren, visitChildren |
| Methods inherited from class org.apache.wicket.Component |
add, afterRender, beforeRender, configure, continueToOriginalDestination, debug, detach, detachBehaviors, detachModels, determineVisibility, error, fatal, findParent, findParentWithAssociatedMarkup, getApplication, getBehaviors, getBehaviorsRawList, getClassRelativePath, getComponentBorder, getConverter, getConverter, getDefaultModel, getDefaultModelObject, getDefaultModelObjectAsString, getDefaultModelObjectAsString, getEscapeModelStrings, getFeedbackMessage, getId, getInnermostModel, getLocale, getLocalizer, getMarkupAttributes, getMarkupId, getMarkupId, getMarkupIdImpl, getMetaData, getModelComparator, getOutputMarkupId, getOutputMarkupPlaceholderTag, getPage, getPageRelativePath, getParent, getPath, getRenderBodyOnly, getRequest, getRequestCycle, getResponse, getSession, getSizeInBytes, getString, getString, getString, getStyle, getVariation, hasBeenRendered, hasErrorMessage, hasFeedbackMessage, info, internalAttach, internalDetach, isActionAuthorized, isAncestorOf, isEnableAllowed, isEnabled, isEnabledInHierarchy, isRenderAllowed, isStateless, isVersioned, isVisibilityAllowed, isVisible, isVisibleInHierarchy, markRendering, modelChanged, modelChanging, prepareForRender, prepareForRender, redirectToInterceptPage, remove, remove, render, render, renderComponent, renderComponent, rendered, replaceWith, sameInnermostModel, sameInnermostModel, setComponentBorder, setDefaultModelObject, setEnabled, setEscapeModelStrings, setMarkupId, setMarkupIdImpl, setMetaData, setOutputMarkupId, setOutputMarkupPlaceholderTag, setRedirect, setRenderBodyOnly, setResponsePage, setResponsePage, setResponsePage, setVersioned, setVisibilityAllowed, setVisible, urlFor, urlFor, urlFor, urlFor, urlFor, urlFor, visitParents, warn |
MultipleValueAutoCompleteTextField
public MultipleValueAutoCompleteTextField(String wicketId)
setClientSideOptions
public MultipleValueAutoCompleteTextField setClientSideOptions(MultipleValueAutoCompleteTextField.AutoCompleteOption[] options)
- Returns:
- this
setServerSideDataSource
public MultipleValueAutoCompleteTextField setServerSideDataSource(MultipleValueAutoCompleteTextField.AutoCompleteDataSource dataSource)
- Returns:
- this
setTheme
public MultipleValueAutoCompleteTextField setTheme(String theme)
- If the default style is not acceptable, copy "token-input-facebook.css" (from databases and life source) into your
CSS directory as "token-input-XXX.css", include it from your HTML, and call
setTheme("XXX").
- Returns:
- this
getJavascript
public String getJavascript()
- Internal method - Do not use