Hi,

I don't know about extending current choice renderer. It seems to be too specific to be part of the generic ChoiceRenderer.

I've made a choice renderer just for this use case.

IdChoiceRenderer<Person, Long> renderer = new IdChoiceRenderer<Person, Long>() {
   protected Long getItemId(Person person) {
     return person.getId();
   }
   protected List<Person> loadItems() {
     return personDao().getAllPersons();
   }
}

add(new DropDownChoice("person",
                       new PropertyModel(this, "personId"),
                       render.getChoicesModel(),
                       renderer));

 ...

public int getPersonId() {
  ...
}

public void setPersonId() {
  ...
}

-MAtej

Johan Compagner wrote:
you could just forward the call

new InBetweenModel(realModel)
{
  getObject(component)
{
int id = realModel.getObject(component)
return idToObject(id);
}
}

I will see if i can make it a bit easier for people that have this specific case. Now and then this question will popup. So maybe we could patch the choices and introduce a special choicerenderer
so that it is a bit simpler.

If you (or matej) could look into the choice and ichoicerenderer impl and see what could be patches
so that is easy to have such a thing and how that then can be configured...

johan


On 6/2/06, *Ralf Ebert* < [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    Hi,

     > a one<->one relation ship between whats in the list and whats in
    the model.
     > if you want to map that then the fastest way that i can think of
    right now
     > is to use
     > a model that sits between it. That maps CategoryVO <-> int back
    and forward.
    I also thought about that, but how would I get the value then (which i
    would get through the compoundpropertymodel normally) ? When I plug in
    my own model, property resolving doesn't take place anymore...

     > Why do you work with ints in the model objects? Why not just full
    blown java
     > objects?
    Mostly because these objects (which are created by the persistence
    layer) are not written by me. They are as they are and I have to live
    with that. I could extend these classes and provide getters/setters
    for doing this mapping, that would be a quite clean solution. But I
    need this for a lot of objects, so handling it using wicket models
    somehow would make some things easier for me...

    Ralf


    -------------------------------------------------------
    All the advantages of Linux Managed Hosting--Without the Cost and Risk!
    Fully trained technicians. The highest number of Red Hat
    certifications in
    the hosting industry. Fanatical Support. Click to learn more
    http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
    <http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642>
    _______________________________________________
    Wicket-user mailing list
    Wicket-user@lists.sourceforge.net
    <mailto:Wicket-user@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/wicket-user
    <https://lists.sourceforge.net/lists/listinfo/wicket-user>



package framework.wicket.util;

import java.util.AbstractList;
import java.util.List;

import wicket.Component;
import wicket.markup.html.form.IChoiceRenderer;
import wicket.model.AbstractReadOnlyDetachableModel;
import wicket.model.IModel;

/**
 * Choice renderer that can be used in situations where the model object is an 
id. 
 * <p>
 * Usage: 
 * <pre>
 *  IdChoiceRenderer&lt;Person, Long&gt; renderer = new 
IdChoiceRenderer&lt;Person, Long&gt;() { 
 *     protected Long getItemId(Person person) {
 *       return person.getId();
 *     }
 *     protected List&lt;Person&gt; loadItems() {
 *       return personDao().getAllPersons();
 *     }
 *   }
 *   
 *   add(new DropDownChoice("person", 
 *                          new PropertyModel(this, "personId"), 
 *                          render.getChoicesModel(),
 *                          renderer)); 
 *   
 *   ...
 *   
 *   public int getPersonId() {
 *     ...
 *   }
 *   
 *   public void setPersonId() {
 *     ...
 *   }
 * </pre>
 * 
 * @author Matej Knopp
 *
 * @param <ItemType> type of item (entity)
 * @param <IdType> type of identifier (usually long or int)
 */
public abstract class IdChoiceRenderer<ItemType, IdType> implements 
IChoiceRenderer {
        
        /**
         * Default constructor.
         */
        public IdChoiceRenderer() {
        }

        /**
         * Returns the display value for the given object (which is an id). 
         */
        final public Object getDisplayValue(Object object) {
                if (items == null)
                        throw new NullPointerException("Items are not 
initialized properly!");
                
                for (ItemType item : items) {
                        if (object.equals(getItemId(item)))
                                return getItemDisplayValue(item);
                }
                return "";
        }

        /**
         * Returns the id as string.
         */
        @SuppressWarnings("unchecked")
        final public String getIdValue(Object object, int index) {
                return getIdAsString((IdType) object);
        }
        
        private List<ItemType> items = null;
        
        /**
         * Model that is used for selection choices. 
         */
        private IModel choicesModel = new AbstractReadOnlyDetachableModel() {
                @Override
                public IModel getNestedModel() {
                        return null;
                }
                @Override
                protected void onAttach() {
                        items = loadItems();
                }               
                @Override
                protected void onDetach() {
                        items = null;
                }
                /**
                 * Returns the list of ids.
                 */
                @Override
                protected Object onGetObject(Component component) {
                        return new AbstractList<IdType>() {
                                @Override
                                public IdType get(int index) {
                                        return getItemId(items.get(index));
                                }
                                @Override
                                public int size() {
                                        return items != null ? items.size() : 0;
                                }
                        };                      
                }
        };
        
        /**
         * Returns the model to be used as choices.
         */
        final public IModel getChoicesModel() {
                return choicesModel;
        }
        
        /**
         * Implementation of this method should load and return
         * list of all items to be used as choices.
         */
        protected abstract List<ItemType> loadItems();

        /**
         * Implementation of this method should return the id of 
         * the given item. 
         */
        protected abstract IdType getItemId(ItemType item);

        /**
         * Returns the value to be displayed for given item.
         */
        protected String getItemDisplayValue(ItemType item) {
                return item.toString();
        }

        /**
         * Returns the string representation of the id.
         */
        protected String getIdAsString(IdType id) {
                return id.toString();
        }
}

Reply via email to