-
Notifications
You must be signed in to change notification settings - Fork 292
make selection machinery pluggable so that for instance we can use an… #1537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,14 +16,15 @@ | |||||
| */ | ||||||
| package org.wicketstuff.jquery.ui.form.autocomplete; | ||||||
|
|
||||||
| import java.util.Collections; | ||||||
| import java.util.List; | ||||||
| import java.util.Locale; | ||||||
| import org.apache.wicket.ajax.AjaxRequestTarget; | ||||||
| import org.apache.wicket.markup.ComponentTag; | ||||||
| import org.apache.wicket.markup.html.form.TextField; | ||||||
| import org.apache.wicket.model.IModel; | ||||||
| import org.apache.wicket.util.convert.IConverter; | ||||||
| import org.apache.wicket.util.lang.Args; | ||||||
| import org.danekja.java.util.function.serializable.SerializableSupplier; | ||||||
| import org.wicketstuff.jquery.core.IJQueryWidget; | ||||||
| import org.wicketstuff.jquery.core.JQueryBehavior; | ||||||
| import org.wicketstuff.jquery.core.renderer.ITextRenderer; | ||||||
|
|
@@ -40,7 +41,7 @@ | |||||
| * @author Sebastien Briquet - sebfz1 | ||||||
| * @author reiern70 | ||||||
| */ | ||||||
| public abstract class AbstractAutoCompleteTextField<T> extends TextField<T> implements IJQueryWidget, IAutoCompleteListener // NOSONAR | ||||||
| public abstract class AbstractAutoCompleteTextField<T> extends TextField<T> implements IJQueryWidget, IAutoCompleteListener<T> // NOSONAR | ||||||
| { | ||||||
| private static final long serialVersionUID = 1L; | ||||||
|
|
||||||
|
|
@@ -51,6 +52,7 @@ public abstract class AbstractAutoCompleteTextField<T> extends TextField<T> impl | |||||
|
|
||||||
| private final ITextRenderer<? super T> renderer; | ||||||
| private final IConverter<T> converter; | ||||||
| private final IElementSelectionStrategy<T> elementSelectionStrategy; | ||||||
|
|
||||||
| private final IJQueryTemplate template; | ||||||
| private JQueryAbstractTemplateBehavior templateBehavior = null; | ||||||
|
|
@@ -108,6 +110,7 @@ public AbstractAutoCompleteTextField(String id, ITextRenderer<? super T> rendere | |||||
| this.renderer = renderer; | ||||||
| this.template = this.newTemplate(); | ||||||
| this.converter = this.newConverter(); | ||||||
| this.elementSelectionStrategy = this.newElementSelectionStrategy(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -145,7 +148,8 @@ public AbstractAutoCompleteTextField(String id, IModel<T> model, ITextRenderer<? | |||||
| this(id, model, renderer, null); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| /** | ||||||
| * Constructor | ||||||
| * | ||||||
| * @param id the markup id | ||||||
|
|
@@ -160,6 +164,7 @@ public AbstractAutoCompleteTextField(String id, IModel<T> model, ITextRenderer<? | |||||
| this.renderer = renderer; | ||||||
| this.template = this.newTemplate(); | ||||||
| this.converter = this.newConverter(); | ||||||
| this.elementSelectionStrategy = this.newElementSelectionStrategy(); | ||||||
| } | ||||||
|
|
||||||
| // Methods // | ||||||
|
|
@@ -243,6 +248,16 @@ public ITextRenderer<? super T> getRenderer() | |||||
| return this.renderer; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the {@link IElementSelectionStrategy} used to identify and resolve selected choices. | ||||||
| * | ||||||
| * @return the selection strategy, never {@code null} | ||||||
| */ | ||||||
| public IElementSelectionStrategy<T> getElementSelectionStrategy() | ||||||
| { | ||||||
| return this.elementSelectionStrategy; | ||||||
| } | ||||||
|
|
||||||
| // Events // | ||||||
|
|
||||||
| @Override | ||||||
|
|
@@ -292,17 +307,14 @@ protected void onComponentTag(final ComponentTag tag) | |||||
| tag.put("autocomplete", "off"); // disable browser's autocomplete | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public final void onSelect(AjaxRequestTarget target, int index) | ||||||
| { | ||||||
| if (-1 < index && index < this.choices.getObject().size()) | ||||||
| { | ||||||
| T choice = this.choices.getObject().get(index); | ||||||
|
|
||||||
| this.setModelObject(choice); | ||||||
| this.onSelected(target); | ||||||
| } | ||||||
| } | ||||||
| @Override | ||||||
| public void onSelect(AjaxRequestTarget target, T choice,String identifier) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if (choice != null) { | ||||||
| LOG.error("Cannot select choice with ID: {}", identifier); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this code :( |
||||||
| } | ||||||
| this.setModelObject(choice); | ||||||
| this.onSelected(target); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Triggered when the user selects an item from results that matched its input | ||||||
|
|
@@ -318,7 +330,13 @@ protected void onSelected(AjaxRequestTarget target) | |||||
| @Override | ||||||
| public JQueryBehavior newWidgetBehavior(String selector) | ||||||
| { | ||||||
| return new AutoCompleteBehavior(selector, this) { // NOSONAR | ||||||
| return new AutoCompleteBehavior<T>(selector, this, new SerializableSupplier<List<T>>() { | ||||||
|
|
||||||
| @Override | ||||||
| public List<T> get() { | ||||||
| return choices != null ? choices.getObject() : Collections.emptyList(); | ||||||
| } | ||||||
| }) { // NOSONAR | ||||||
|
|
||||||
| private static final long serialVersionUID = 1L; | ||||||
|
|
||||||
|
|
@@ -394,18 +412,34 @@ public String convertToString(T value, Locale locale) | |||||
| }; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets a new {@link IElementSelectionStrategy}. Index-based selection is used by default. | ||||||
| * | ||||||
| * @return the selection strategy | ||||||
| */ | ||||||
| protected IElementSelectionStrategy<T> newElementSelectionStrategy() | ||||||
| { | ||||||
| return IndexBasedElementSelectionStrategy.get(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets a new {@link AutoCompleteChoiceModelBehavior} | ||||||
| * | ||||||
| * @return the {@link AutoCompleteChoiceModelBehavior} | ||||||
| */ | ||||||
| private AutoCompleteChoiceModelBehavior<T> newChoiceModelBehavior() | ||||||
| { | ||||||
| return new AutoCompleteChoiceModelBehavior<T>(this.renderer, this.template) { // NOSONAR | ||||||
|
|
||||||
| return new AutoCompleteChoiceModelBehavior<T>(this.renderer, this.template) // NOSONAR | ||||||
| { | ||||||
| private static final long serialVersionUID = 1L; | ||||||
| private static final String TERM = "term"; | ||||||
|
|
||||||
| @Override | ||||||
| protected IElementSelectionStrategy<T> getElementSelectionStrategy() | ||||||
| { | ||||||
| return AbstractAutoCompleteTextField.this.getElementSelectionStrategy(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public List<T> getChoices() | ||||||
| { | ||||||
|
|
@@ -415,4 +449,34 @@ public List<T> getChoices() | |||||
| } | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the {@link AutoCompleteChoiceModelBehavior} that serves the choices to the widget | ||||||
| * | ||||||
| * @return the {@link AutoCompleteChoiceModelBehavior}, or {@code null} if the component has not been initialized yet | ||||||
| */ | ||||||
| public final AutoCompleteChoiceModelBehavior<T> getChoiceModelBehavior() | ||||||
| { | ||||||
| return this.choiceModelBehavior; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the cached choices of the last query, used to resolve the user selected object | ||||||
| * | ||||||
| * @return the {@link IModel} of choices, or {@code null} if no query has been performed yet | ||||||
| */ | ||||||
| public final IModel<List<T>> getChoices() | ||||||
| { | ||||||
| return this.choices; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the {@link JQueryAbstractTemplateBehavior} supplied by {@link #newTemplate()} | ||||||
| * | ||||||
| * @return the {@link JQueryAbstractTemplateBehavior}, or {@code null} if there is no template | ||||||
| */ | ||||||
| public final JQueryAbstractTemplateBehavior getTemplateBehavior() | ||||||
| { | ||||||
| return this.templateBehavior; | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this one :))