/* * PresetCheckedAndSelected.java * * Created on October 20, 2003, 9:22 PM */ package downey; import java.util.HashMap; import java.io.Serializable; /** A class to simplify the presetting of radio groups, checkbox groups and * selection lists * @author Tim Downey */ public class PresetCheckedAndSelected implements Serializable { /** Creates a new instance of PresetCheckedAndSelected */ public PresetCheckedAndSelected() { mapSelected = new HashMap(); } /** Reinitializes the map to empty. * * This method will not be used very often. * If you need to clear all the old entries, then use this. */ public void initChoice() { mapSelected = new HashMap(); } /** Retrieve a choice from the map. *
Access this from the HTML for the input element
* Assume that rec is a record object whose class extends
* this class, and that team is a checkbox group.
*
* <input type="checkbox" name="team" value="heat" rec.getChoice("heat")> ** @return The String value for the given key * @param key The value from the input element in the form */ public String getChoice(String key) { if (key == null) { return ""; } if (mapSelected == null) { initChoice(); } if (mapSelected.containsKey(key)) { return (String) mapSelected.get(key); } else { return ""; } } /** Retrieve a choice from the map: should be either a radio or checkbox element. * *
Access this from the HTML for the input element
* Assume that rec is a record object whose class extends
* this class, and that team is a checkbox group.
*
* <input type="checkbox" name="team" value="heat" rec.getChecked("heat")> ** @return The String value for the given key * @param key The value from the input element in the form */ public String getChecked(String key) { return getChoice(key); } /** Retrieve a choice from the map: should be used in an option for a selection list element. * *
Access this from the HTML option for the select element
* Assume that rec is a record object whose class extends
* this class, and that heat is an option in a multiple selection list.
*
* <option value="heat" rec.getSelected("heat")> ** @return The String value for the given key * @param key The value from the input element in the form */ public String getSelected(String key) { return getChoice(key); } /** Set a choice in the map: the type is either "checked" or "selected". * This will be the word * associated with this entry in the map. *
Usually called with value(s) from the query string
* Assume that rec is a record object whose class extends
* this class, and that color is a radio group.
*
* rec.setChoice(request.getParamter("color"), "checked"); ** Call this once for each radio or checkbox group, or selection list
Usually called with value(s) from the query string
* Assume that rec is a record object whose class extends
* this class, and that team is a checkbox group.
*
* rec.setChoice(request.getParamterValues("team"), "checked"); ** Call this once for each radio group, checkbox group or selection list
Usually called with value(s) from the query string
* Assume that rec is a record object whose class extends
* this class, and that color is a radio group.
*
* rec.setChecked(request.getParamter("color")); ** Call this once for each radio or checkbox group
Usually called with value(s) from the query string
* Assume that rec is a record object whose class extends
* this class, and that team is a checkbox group.
*
* rec.setChecked(request.getParamterValues("team")); ** Call this once for each radio group, checkbox group
Usually called with value(s) from the query string
* Assume that rec is a record object whose class extends
* this class, and that color is a single selection list.
*
* rec.setSelected(request.getParamter("color")); ** Call this once for each selection list
Usually called with value(s) from the query string
* Assume that rec is a record object whose class extends
* this class, and that team is a multiple selection list.
*
* rec.setSelected(request.getParamterValues("team")); ** Call this once for each selection list.