I created a HashMapChoiceRenderer which extends ChoiceRenderer. I'm not sure if 
this is the best way to go about it, but it worked for me to specifically set 
the value with a set of checkboxes.
disclaimer : hotmail munches code, so you will want to throw this code into an 
ide and format it.
Here is how I add my new component:
###########################
DropDownChoice groupSelect = new DropDownChoice("selectGroup", new 
PropertyModel(this, "selectGroup"), userGroups, new HashMapChoiceRenderer()) 
{  /**   * Every object will be a HashMap that has just one item. So, it   * is 
safe to grab the first item from the keySet iterator and   * test with the 
value persisted.   *    * @see 
org.apache.wicket.markup.html.form.AbstractSingleSelectChoice#isSelected(java.lang.Object,  
 *      int, java.lang.String)   */�...@override  protected boolean 
isSelected(Object object, int index, String selected) {      HashMap<String, 
String> testee = (HashMap<String, String>) object;      String id = 
testee.keySet().iterator().next();      return 
(id.equals(getSelectGroup()));  }};
###########################
When Wicket calls your setter, it gives you a string that has both the id and 
the value - a serialized view of the hashmap, so I did this with the setter

###########################/** * Added logic to look for the occurance of { 
since the dropdown list * will usually supply {id=value} as a string. *  * 
@param sortListBy *            the sortListBy to set */public void 
setSortListBy(String sortListBy) {    if (sortListBy != null && 
sortListBy.contains("{"))        this.sortListBy = 
HashMapChoiceRenderer.getListSelectionValue(sortListBy);    else        
this.sortListBy = sortListBy;}
Here is the component
###########################

import java.util.HashMap;
import org.apache.commons.logging.Log;import 
org.apache.commons.logging.LogFactory;import 
org.apache.wicket.markup.html.form.ChoiceRenderer;
/** * This class will let you use a HashMap<String,String> as an IModel for 
your * DropDownChoice. *  * @author <a 
href="mailto:russellsimpk...@hotmail.com";>Russell Simpkins</a> */public class 
HashMapChoiceRenderer<T> extends ChoiceRenderer<T> {
    private static final long serialVersionUID = 1904209513899620301L;    
public static final Log log = LogFactory.getLog(HashMapChoiceRenderer.class);
    /**     * A utility method for getting back the value of a list selection 
value     * where the input is a String wiht a format of {id=value} where we 
want the     * "id" part     *      * @param selection     * @return String the 
"value=" part of an HTML select field     */    public static final String 
getListSelectionValue(String selection) {        String[] parts = ((String) 
selection).replaceAll("([{]|[}])", "").split("[=]");        return parts[0];    
}
    /**     * A utility method for getting back the value of a list selection 
value     * where the input is a String wiht a format of {id=value} where we 
want the     * "value" part     *      * @param selection     * @return String 
- the display value a user would have seen     */    public static final String 
getListSelectionDisplay(String selection) {        String[] parts = ((String) 
selection).replaceAll("([{]|[}])", "").split("[=]");        return parts[1];    
}
    /*     * (non-Javadoc)     *      * @see     * 
org.apache.wicket.markup.html.form.ChoiceRenderer#getDisplayValue(java     * 
.lang.Object)     */   �...@suppresswarnings("unchecked")   �...@override    
public Object getDisplayValue(T object) {        try {            
HashMap<String, String> item = (HashMap<String, String>) object;            
return item.values().iterator().next();        } catch (ClassCastException cce) 
{            log.debug("getDisplayValue was passed something other than a 
HashMap", cce);        } catch (Exception e) {            log.error("There was 
an unforseen error. ", e);        }        return 
super.getDisplayValue(object);    }
    /*     * (non-Javadoc)     *      * @see     * 
org.apache.wicket.markup.html.form.ChoiceRenderer#getIdValue(java.lang     * 
.Object, int)     */   �...@suppresswarnings("unchecked")   �...@override    
public String getIdValue(T object, int index) {        try {            if 
(object instanceof String) {                return 
HashMapChoiceRenderer.getListSelectionValue((String) object);            } else 
{                HashMap<String, String> item = (HashMap<String, String>) 
object;                return item.keySet().iterator().next();            }     
   } catch (ClassCastException cce) {            log.debug("getIdValue was 
passed something other than a HashMap: " + object + " index: " + index, cce);   
     } catch (Exception e) {            log.error("There was an unforseen 
error. ", e);        }        return super.getIdValue(object, index);    }
}
###########################
Russ

> I have a bean with enum field.
> When I implements radiochoice model by this enum field in html radiochoise
> code i see:
>
> 
> type="radio">YES

> 
> type="radio">NO

>
>
> Labels contents enum values. But input values contents order element index
> and this is no good!
> How implements radiochoise so value of input contents enum value? (no index
> element!)
>

                                          
_________________________________________________________________
The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to