1 16 17 package org.apache.jetspeed.modules.parameters; 18 19 import org.apache.ecs.ElementContainer; 21 import org.apache.ecs.html.Table; 22 import org.apache.ecs.html.Input; 23 import org.apache.ecs.html.TD; 24 import org.apache.ecs.html.TR; 25 26 import java.util.StringTokenizer ; 28 import java.util.Vector ; 29 import java.util.Map ; 30 import java.util.Enumeration ; 31 32 import org.apache.turbine.util.RunData; 34 35 46 public class CheckBoxGroup extends ParameterPresentationStyle 47 { 48 49 public static final String ITEMS = "items"; 50 public static final String LAYOUT = "layout"; 51 public static final String LAYOUT_EW = "$eastwest"; 52 public static final String LAYOUT_NS = "$northsouth"; 53 public static final String PREFIX = "prefix"; 54 public static final String PREFIX_DEFAULT = "cb"; 55 56 59 public String getContent(RunData data, String name, String value, Map parms) 60 { 61 62 ElementContainer result = new ElementContainer(); 63 String items = (String )parms.get(ITEMS); 64 String layout = (String )parms.get(LAYOUT); 65 String prefix = (String )parms.get(PREFIX); 66 if (prefix == null) 67 { 68 prefix = PREFIX_DEFAULT; 69 } 70 71 StringTokenizer st = new StringTokenizer (items, ","); 72 Vector v = new Vector (); 73 while ( st.hasMoreTokens() ) 74 { 75 String token = st.nextToken().trim(); 76 if ( !token.equalsIgnoreCase("") ) 77 { 78 v.add(token); 79 } 80 } 81 82 Table t = new Table(); 83 84 for ( Enumeration e = v.elements(); e.hasMoreElements(); ) 85 { 86 String item = ((String )e.nextElement()).trim(); 87 Input cb = new Input(Input.CHECKBOX, prefix + item, item); 88 cb.setChecked(value.indexOf(item) >= 0); 89 cb.setOnClick(getJavascript(name, v, prefix)); 90 ElementContainer temp = new ElementContainer(); 91 temp.addElement(cb).addElement(" ").addElement(item); 92 if ( layout.equalsIgnoreCase(LAYOUT_NS) ) 93 { 94 t.addElement(new TR().addElement(new TD(temp))); 95 } else 96 { 97 result.addElement(temp); 98 } 99 } 100 101 if ( layout.equalsIgnoreCase(LAYOUT_NS) ) 102 { 103 result.addElement(t); 104 } 105 106 result.addElement(new Input(Input.HIDDEN, name, value)); 107 108 return result.toString(); 109 110 } 111 112 118 private String getJavascript(String name, Vector v, String prefix) 119 { 120 121 StringBuffer result = new StringBuffer (); 122 result.append(name).append(".value = "); 123 124 for ( Enumeration e = v.elements(); e.hasMoreElements(); ) 125 { 126 String item = prefix + (String )e.nextElement(); 127 result.append("(("); 128 result.append(item); 129 result.append(".checked) ? "); 130 result.append(item); 131 result.append(".value : '')"); 132 if ( e.hasMoreElements() ) 133 { 134 result.append(" + \',\' + "); 135 } 136 } 137 138 return result.toString(); 139 } 140 141 144 public static void main(String args[]) 145 { 146 147 CheckBoxGroup cbg = new CheckBoxGroup(); 148 java.util.Hashtable parms = new java.util.Hashtable (); 149 parms.put(ITEMS, "Tomaszewski,Gorgon,Zmuda,Szymanowski,Musial,Kasperczak,Deyna,Cmikiewicz,Lato,Szarmach,Gadocha"); 150 System.out.println(cbg.getContent(null, "test", "Deyna,,,,Gorgon,Lato,Szarmach,", parms)); 151 152 } 153 154 155 } 156 | Popular Tags |