View Javadoc

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;
28  import java.util.Vector;
29  import java.util.Map;
30  import java.util.Enumeration;
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 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      /***
57       * Returns presentation control
58       */
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("&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 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     /***
142      *  Test method
143      */
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 }