KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > parameters > CheckBoxGroup


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.modules.parameters;
18
19 //ecs stuff
20
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 // java stuff
27
import java.util.StringTokenizer JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Enumeration JavaDoc;
31
32 //turbine support
33
import org.apache.turbine.util.RunData;
34
35 /**
36  * Returns a group of check boxes using the following options:
37  * <UL>
38  * <LI><code>items</code>: list of comma-delimited check box names/values</LI>
39  * <LI><code>layout</code> [$northsouth|<strong>$eastwest</strong>]: presentation layout</LI>
40  * <LI><code>prefix</code>: prefix to use for check box names, default="cb"</LI>
41  * </UL>
42  *
43  * @author <a HREF="mailto:mark_orciuch@ngsltd.com">Mark Orciuch</a>
44  * @version $Id: CheckBoxGroup.java,v 1.4 2004/02/23 03:01:20 jford Exp $
45  */

46 public class CheckBoxGroup extends ParameterPresentationStyle
47 {
48
49     public static final String JavaDoc ITEMS = "items";
50     public static final String JavaDoc LAYOUT = "layout";
51     public static final String JavaDoc LAYOUT_EW = "$eastwest";
52     public static final String JavaDoc LAYOUT_NS = "$northsouth";
53     public static final String JavaDoc PREFIX = "prefix";
54     public static final String JavaDoc PREFIX_DEFAULT = "cb";
55
56     /**
57      * Returns presentation control
58      */

59     public String JavaDoc getContent(RunData data, String JavaDoc name, String JavaDoc value, Map JavaDoc parms)
60     {
61
62         ElementContainer result = new ElementContainer();
63         String JavaDoc items = (String JavaDoc)parms.get(ITEMS);
64         String JavaDoc layout = (String JavaDoc)parms.get(LAYOUT);
65         String JavaDoc prefix = (String JavaDoc)parms.get(PREFIX);
66         if (prefix == null)
67         {
68             prefix = PREFIX_DEFAULT;
69         }
70
71         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(items, ",");
72         Vector JavaDoc v = new Vector JavaDoc();
73         while ( st.hasMoreTokens() )
74         {
75             String JavaDoc 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 JavaDoc e = v.elements(); e.hasMoreElements(); )
85         {
86             String JavaDoc item = ((String JavaDoc)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("&nbsp;").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     /**
113      *
114      * @param name
115      * @param v
116      * @return string
117      */

118     private String JavaDoc getJavascript(String JavaDoc name, Vector JavaDoc v, String JavaDoc prefix)
119     {
120
121         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
122         result.append(name).append(".value = ");
123
124         for ( Enumeration JavaDoc e = v.elements(); e.hasMoreElements(); )
125         {
126             String JavaDoc item = prefix + (String JavaDoc)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     /**
142      * Test method
143      */

144     public static void main(String JavaDoc args[])
145     {
146
147         CheckBoxGroup cbg = new CheckBoxGroup();
148         java.util.Hashtable JavaDoc parms = new java.util.Hashtable JavaDoc();
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