1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.parameters;
18
19
20 import org.apache.ecs.html.Select;
21 import org.apache.ecs.html.Option;
22
23
24 import java.util.Map;
25 import java.util.Arrays;
26 import java.util.StringTokenizer;
27
28
29 import org.apache.turbine.util.RunData;
30
31 /***
32 * Returns simple list box control.
33 * <p>Options:
34 * <UL>
35 * <LI><code>items</code> - comma-separated list of list box items</LI>
36 * <LI><code>layout</code> [<strong>$combo</strong>|$list] - combo box vs list box</LI>
37 * <LI><code>size</code> - size of the list box for $list style</LI>
38 * <LI><code>sort</code> [<strong>false</strong>|true] - return sorted list of items</LI>
39 * <LI><code>multiplechoice</code> [<strong>false</strong>|true] - allow multiple selections</LI>
40 * <LI><code>null-if-empty</code> [<strong>false</strong>|true] - do not return a select control if item list is empty</LI>
41 * </UL>
42 * @author <a href="mark_orciuch@ngsltd.com">Mark Orciuch</a>
43 * @author <a href="mailto:solutioncenter@infointegrators.com">Dave Trapp</a>
44 * @version $Id: ListBox.java,v 1.5 2004/02/23 03:01:20 jford Exp $
45 */
46 public class ListBox extends ParameterPresentationStyle
47 {
48
49 public static final String SORT = "sort";
50 public static final String ITEMS = "items";
51 public static final String LAYOUT = "layout";
52 public static final String LAYOUT_COMBO = "$combo";
53 public static final String LAYOUT_LIST = "$list";
54 public static final String LIST_SIZE = "listsize";
55 public static final String MULTIPLE_CHOICE = "multiplechoice";
56 public static final String NULL_IF_EMPTY = "null-if-empty";
57
58 protected String layout = null;
59 protected String items[] = null;
60 protected String size = null;
61 protected boolean multiple = false;
62
63 /***
64 * Returns presentation control
65 *
66 * @param data
67 * @param name
68 * @param value
69 * @param parms
70 * @return string
71 */
72 public String getContent(RunData data, String name, String value, Map parms)
73 {
74
75 init(data);
76
77 Select select = null;
78 if ( layout.equalsIgnoreCase(LAYOUT_LIST) )
79 {
80 select = new Select(name, new Integer(size).intValue());
81 } else
82 {
83 select = new Select(name);
84 }
85
86 if ( multiple )
87 {
88 select.setMultiple(multiple);
89 }
90
91 if ( items != null )
92 {
93
94 boolean sort = new Boolean((String)this.getParm(SORT, "false")).booleanValue();
95 if ( sort )
96 {
97 Arrays.sort(items);
98 }
99
100 for ( int i=0; i < items.length; i++ )
101 {
102 Option option = new Option(items[i]).addElement(items[i]);
103 if (multiple)
104 {
105 option.setSelected(value.indexOf(items[i]) >= 0);
106 }
107 else
108 {
109 option.setSelected(items[i].equalsIgnoreCase(value));
110 }
111 select.addElement(option);
112 }
113 }
114
115
116 boolean nullIfEmpty = new Boolean((String)this.getParm(NULL_IF_EMPTY, "false")).booleanValue();
117 if ( this.items == null || (nullIfEmpty && items.length == 0) )
118 {
119 return null;
120 }
121
122 return select.toString();
123
124 }
125
126 /***
127 * Initialize options
128 *
129 * @param data
130 */
131 protected void init(RunData data)
132 {
133
134 this.layout = (String)this.getParm(LAYOUT, LAYOUT_COMBO);
135 this.items = this.getItems(data);
136 this.size = (String)this.getParm(LIST_SIZE, "1");
137 this.multiple = new Boolean((String)this.getParm(MULTIPLE_CHOICE, "false")).booleanValue();
138
139 }
140
141 /***
142 * Parse items into an arrary of strings
143 *
144 * @param data
145 * @return string array
146 */
147 protected String[] getItems(RunData data)
148 {
149
150 String[] result = null;
151 String list = (String)this.getParm(ITEMS, "");
152
153 StringTokenizer it = new StringTokenizer(list, ",");
154 int size = it.countTokens();
155
156 if ( size > 0 )
157 {
158 result = new String[size];
159
160 int i = 0;
161 while ( it.hasMoreTokens() )
162 {
163 String item = (String)it.nextToken();
164 result[i] = item;
165 i++;
166 }
167 }
168
169 return result;
170
171 }
172
173 }