/* * 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
* Call it before calling any of the get methods from this class. * @param key The value from the query string * @param value Either "checked" or "seleted" */ public void setChoice(String key, String value) { if (key != null && !key.equals("")) { if (mapSelected == null) { initChoice(); } mapSelected.put(key, value); } } /** Set a choice in the map: the type is either "checked" or "selected". * This will be the word * associated with these entries 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 team is a checkbox group. *

     * rec.setChoice(request.getParamterValues("team"), "checked");
     * 
* Call this once for each radio group, checkbox group or selection list
* Call it before calling any of the get methods from this class. * @param keys The array of values from the query string * @param value Either "checked" or "seleted" */ public void setChoice(String[] keys, String value) { if (keys != null) { for(int pos=0; pos < keys.length; pos++) { setChoice(keys[pos], value); } } } /** Set a type of "checked" for 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.setChecked(request.getParamter("color"));
     * 
* Call this once for each radio or checkbox group
* Call it before calling any of the get methods from this class. * @param key The value from the query string */ public void setChecked(String key) { if (key != null && !key.equals("")) { if (mapSelected == null) { initChoice(); } mapSelected.put(key, "checked"); } } /** Set a type of "checked" for an array of String keys. * *

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
* Call it before calling any of the get methods from this class. * @param keys The array of values from the query string * * @param keys The array of values from the query string */ public void setChecked(String[] keys) { if (keys != null) { for(int pos=0; pos < keys.length; pos++) { setChoice(keys[pos], "checked"); } } } /** Set a type of "selected" for 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 single selection list. *

     * rec.setSelected(request.getParamter("color"));
     * 
* Call this once for each selection list
* Call it before calling any of the get methods from this class. * @param key The value from the query string */ public void setSelected(String key) { if (key != null && !key.equals("")) { if (mapSelected == null) { initChoice(); } mapSelected.put(key, "selected"); } } /** Set a type of "selected" for an array of String keys. * *

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.
* Call it before calling any of the get methods from this class. * @param keys The array of values from the query string */ public void setSelected(String[] keys) { if (keys != null) { for(int pos=0; pos < keys.length; pos++) { setChoice(keys[pos], "selected"); } } } /** The implementation of the map */ protected HashMap mapSelected; }