You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by denisa <ak...@gmail.com> on 2010/06/11 10:46:36 UTC

radiochoice values

Hi!
I have a bean with enum field. 
When I implements radiochoice model by this enum field in html radiochoise
code i see:

<input name="rubrics:0:rubricAccessType" value="0" id="rubricAccessType5-0"
type="radio"><label for="rubricAccessType5-0">YES</label><br>
<input name="rubrics:0:rubricAccessType" value="1" id="rubricAccessType5-1"
type="radio"><label for="rubricAccessType5-1">NO</label><br>
<input name="rubrics:0:rubricAccessType" value="2" id="rubricAccessType5-2"
type="radio"><label for="rubricAccessType5-2">AUTO</label><br>
<input name="rubrics:0:rubricAccessType" value="3" id="rubricAccessType5-3"
type="radio"><label for="rubricAccessType5-3">MODERATION</label><br>
<input name="rubrics:0:rubricAccessType" value="4" id="rubricAccessType5-4"
type="radio"><label for="rubricAccessType5-4">USER_DEFINED</label><br>


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!)

Thanks! Sorry for my english. Ask me questions!


 

-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/radiochoice-values-tp2251468p2251468.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


RE: radiochoice values

Posted by Russell Simpkins <ru...@hotmail.com>.
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:russellsimpkins@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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org