========================================================================================================================
I'm not sure why the built-in Wicket component doesn't support this feature 
out-of-the-box, but here is a simple fix/solution:
========================================================================================================================
        /**
         * Auto-Complete text field that allows capture of choice selections by 
choice type (rather than just strings)
         * 
         * @author whoover
         * 
         * @param <CHOICE>
         *            the choice type
         */
        private abstract class AbstractAutoCompleteTextField<CHOICE> extends 
AutoCompleteTextField {

                private static final long serialVersionUID = 1L;
                private transient Iterator<CHOICE> choiceIterator;

                /**
                 * Renderer constrcutor TODO : you can obviously add all of the 
constructors- just added this one for demo
                 * 
                 * @param id
                 *            the ID to set
                 * @param renderer
                 *            the renderer to set
                 */
                public AbstractAutoCompleteTextField(final String id, final 
IAutoCompleteRenderer renderer) {
                        super(id, renderer);
                }

                /**
                 * [EMAIL PROTECTED]
                 */
                @Override
                protected final Iterator<CHOICE> getChoices(final String 
searchTextInput) {
                        // find list of items to display in auto-complete (we 
need to cache the list because the auto-complete only deals with strings)
                        choiceIterator = getChoiceIterator(searchTextInput);
                        return choiceIterator;
                }

                /**
                 * Call-back method that should return an iterator over all 
possible assist choice objects. 
                 * These objects will be passed to the renderer to generate 
output.
                 * 
                 * @param input
                 *            current input
                 * @return iterator over all possible choice objects
                 */
                protected abstract Iterator<CHOICE> getChoiceIterator(final 
String searchTextInput);

                /**
                 * Gets the string value from the specified choice
                 * 
                 * @param choice
                 *            the choice that needs value extraction
                 * @return the unique string value of the choice
                 */
                protected abstract String getChoiceValue(final CHOICE choice);

                /**
                 * Finds the selection by cycling through the current choices 
and matching the choices value. 
                 * If the selected choice is found the choices will be reset 
and the choice will be returned.
                 * <p>
                 * <b>NOTE:</b> Assumes that the selection choice values are 
unique
                 * </p>
                 * 
                 * @return the found selection choice value (null if it cannot 
be found)
                 */
                public final CHOICE findChoice() {
                        CHOICE choice;
                        while (choiceIterator.hasNext()) {
                                choice = choiceIterator.next();
                                if 
(getModelObject().equals(getChoiceValue(choice))) {
                                        // found result- clear choices cache 
(would be nice to use detach() method, but it has been finalized)
                                        choiceIterator = null;
                                        return choice;
                                }
                        }
                        return null;
                }
        }
========================================================================================================================
Using the auto-complete above we can get our specific model instead of a string
========================================================================================================================
                final AbstractAutoCompleteRenderer autoCompleteRenderer = new 
AbstractAutoCompleteRenderer() {
                        private static final long serialVersionUID = 1L;

                        /**
                         * [EMAIL PROTECTED]
                         */
                        @SuppressWarnings("unchecked")
                        @Override
                        protected final String getTextValue(final Object 
object) {
                                // TODO : get the choice text value
                                ((MyObject) object).getMyStringValue();
                        }

                        /**
                         * [EMAIL PROTECTED]
                         */
                        @Override
                        protected final void renderChoice(final Object object, 
final Response response, final String criteria) {
                                // TODO : write choices to response
                                response.write(getTextValue(object));
                        }
                };
                final AbstractAutoCompleteTextField<MyObject> autoCompleteField 
= new AbstractAutoCompleteTextField<MyObject>(id, autoCompleteRenderer){
                        private static final long serialVersionUID = 1L;

                        /**
                         * [EMAIL PROTECTED]
                         */
                        @Override
                        protected final Iterator<MyObject> 
getChoiceIterator(final String searchTextInput) {
                                // TODO : find my objects somehow
                                return 
findMyObjectChoices(searchTextInput).iterator();
                        }

                        /**
                         * [EMAIL PROTECTED]
                         */
                        @Override
                        protected final String getChoiceValue(final MyObject 
choice) {
                                // TODO : get the choice text value (this can 
even be a common call from the renderer getTextValue if desired)
                                return choice.getMyStringValue();
                        }
                };
                autoCompleteField.add(new 
AjaxFormComponentUpdatingBehavior("onchange") {
                        private static final long serialVersionUID = 1L;

                        /**
                         * [EMAIL PROTECTED]
                         */
                        @Override
                        protected final void onUpdate(final AjaxRequestTarget 
target) {
                                // TODO : do something with my object that was 
selected from the auto-complete choices
                                final MyObject myObjectSelection = 
autoCompleteField.findChoice();
                        }
                });
========================================================================================================================

-----Original Message-----
From: Ricky [mailto:[EMAIL PROTECTED]
Sent: Friday, April 25, 2008 3:09 PM
To: [email protected]
Subject: Re: Wicket Auto complete text Issue


Hi,

I took a look at the solution below, unfortunately the above solution
doesn't work because if the model in getTextValue( ) method is set (for the
containing panel or any component for that matter) the *last* choice is
selected and set as a model instead of the right one. It works only when
there is only one selection in the drop down list. Any ideas would be
appreciable.

Rick

On Fri, Apr 25, 2008 at 12:40 PM, Ryan Gravener <[EMAIL PROTECTED]>
wrote:

> Take a look at
>
> http://www.nabble.com/1.3%2C-resource-locator-and-properties-to16707905.html#a16708440
>
> control+f to help wanted for autocomplete
>
> On Fri, Apr 25, 2008 at 12:29 PM, Ricky <[EMAIL PROTECTED]> wrote:
>
> > Hi,
> > I am trying to  get a model object when an autocomplete selection is
> made;
> > but model object comes back as a string rather than the model that was
> > selected. Is there a way to get the selected Model (Actor) in the example
> > below ?
> >
> >  final AutoCompleteTextField actorAutoComplete = new
> > AutoCompleteTextField("input-auto-complete-actor-search", new
> Model((Actor)
> >  getModelObject()), new AbstractAutoCompleteRenderer() {
> >        protected final String getTextValue(final Object object) {
> >                return object.toString();
> >        }
> >        protected final void renderChoice(final Object object, final
> > Response response, final String criteria) {
> >                response.write(object.toString());
> >        }
> >    }) {
> >            protected final Iterator<Actor> getChoices(final String
> > searchTextInput) {
> >                return getCollection().iterator();
> >            }
> >        };
> >        actorAutoComplete.add(new AjaxFormSubmitBehavior("onchange") {
> >                protected final void onSubmit(final AjaxRequestTarget
> > target) {
> >                    // MODEL OBJECT RETURNS STRING RATHER THAN AN ACTOR!!
> >                    this.getComponent().getModelObject();
> >                }
> >        });
> >    }
> >    add(actorAutoComplete);
> >
> > Rick
> >
>
>
>
> --
> Ryan Gravener
> http://wmwm.us/wmwm-date
>



-- 

Regards
Vyas, Anirudh
|| ॐ ||

Reply via email to