Maybe I have found another solution using the standard AutoCompleteTextField
and a custom converter by overwriting getConverter():

public IConverter getConverter(final Class type) {
        return new MyAutoCompleteConverter();
}

private class MyAutoCompleteConverter implements IConverter {

        public MyDomainModelObject convertToObject(final String value, final 
Locale
locale) {
                if (Strings.isEmpty(value)) {
                        return null;
                }
                MyDomainModelObject myModelObject = findChoice(value);
                if (myModelObject == null) {
                        try {
                                myModelObject = (MyDomainModelObject)
                                        
MyAutoCompleteTextField.this.getType().newInstance();
                                myModelObject.setValue(value);
                        } catch (Exception e) {
                                return null;
                        }
                }
                return myModelObject;
        }

        private MyDomainModelObject findChoice(final String value) {
                MyDomainModelObject myChoice = null;
                if (value != null) {
                        for (MyDomainModelObject choice : choices) {
                                if (choice.getValue() != null
                                                && 
choice.getValue().equals(value)) {
                                        myChoice = choice;
                                        break;
                                }
                        }
                }
                return myChoice;
        }
}

I do not need any special AjaxFormComponentUpdatingBehavior because the
findChoice() method is invoked from convertToObject(). I haven't tested it
enough but it seems to work.

Cheers,
Kai


Hoover, William <mailto:[EMAIL PROTECTED]> wrote:
> // optional, but probably needed
> final AjaxFormComponentUpdatingBehavior afcub = new
>       AjaxFormComponentUpdatingBehavior("onchange") { protected final void
>               onUpdate(final AjaxRequestTarget target) { // TODO : do 
> something
>       }
> };
> // optional
> final AbstractAutoCompleteRenderer autoCompleteRenderer = new
>       AbstractAutoCompleteRenderer() { protected final String
>               getTextValue(final Object object) { // TODO : get the text value
>       representation of our domain model object }
>       protected final void renderChoice(final Object object, final
>               Response response, final String criteria) {
>       response.write(getTextValue(object)); }
> };
> // required
> final AbstractAutoCompleteTextField<MyDomainModelObject>
>       autoCompleteField = new
>               AbstractAutoCompleteTextField<MyDomainModelObject>(id,
>       autoCompleteRenderer) { protected final List<MyDomainModelObject>
> getChoiceList(final String searchTextInput) { // TODO : return your
> choice list }
>
>       protected final String getChoiceValue(final MyDomainModelObject
>               choice) throws Throwable { // TODO : get the value that will be
>       displayed for the choice in the autocomplete list }
> };
> autoCompleteField.add(afcub);
>
> -----Original Message-----
> From: Hoover, William [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, October 29, 2008 9:03 AM
> To: users@wicket.apache.org; [EMAIL PROTECTED]
> Subject: RE: Compatibility of objectautocomplete
>
> The CHOICE is your domain model object (there was an error in the
> WIKI). You should be able to use any object in your domain. Can you
> post your code example?
>
> -----Original Message-----
> From: Kai Mütz [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, October 29, 2008 8:57 AM
> To: users@wicket.apache.org
> Subject: RE: Compatibility of objectautocomplete
>
> Hoover, William <mailto:[EMAIL PROTECTED]> wrote:
>> or you can go with this solution:
>> http://cwiki.apache.org/WICKET/autocomplete-using-a-wicket-model.html
>
> Hi William,
> I have tried it but not successfully. I can select a choice from the
> choicelist. But if I want to save it I get a
>
> java.lang.IllegalArgumentException: argument type mismatch
>      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>      at java.lang.reflect.Method.invoke(Unknown Source)
>      at
>
org.apache.wicket.util.lang.PropertyResolver$MethodGetAndSet.setValue(Proper
> tyResolver.java:1093)
>      at
>
org.apache.wicket.util.lang.PropertyResolver$ObjectAndGetSetter.setValue(Pro
> pertyResolver.java:583)
>      at
>
org.apache.wicket.util.lang.PropertyResolver.setValue(PropertyResolver.java:
> 137)
>      at
>
org.apache.wicket.model.AbstractPropertyModel.setObject(AbstractPropertyMode
> l.java:164)
>      at
> org.apache.wicket.Component.setModelObject(Component.java:2889)
>
> This is because the model object seems to be a String. Do I have to
> use a special IModel for CHOICE?
> Where is the findChoice methode invoked? Or do I have to invoke it
> within a behavior?
>
> Regards, Kai
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to