KickJava   Java API By Example, From Geeks To Geeks.

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


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.html.Select;
21 import org.apache.ecs.html.Option;
22
23 // java stuff
24
import java.util.Map JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 //turbine support
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 JavaDoc SORT = "sort";
50     public static final String JavaDoc ITEMS = "items";
51     public static final String JavaDoc LAYOUT = "layout";
52     public static final String JavaDoc LAYOUT_COMBO = "$combo";
53     public static final String JavaDoc LAYOUT_LIST = "$list";
54     public static final String JavaDoc LIST_SIZE = "listsize";
55     public static final String JavaDoc MULTIPLE_CHOICE = "multiplechoice";
56     public static final String JavaDoc NULL_IF_EMPTY = "null-if-empty";
57
58     protected String JavaDoc layout = null;
59     protected String JavaDoc items[] = null;
60     protected String JavaDoc 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 JavaDoc getContent(RunData data, String JavaDoc name, String JavaDoc value, Map JavaDoc 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 JavaDoc(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 JavaDoc((String JavaDoc)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         // If no items to display, do not display empty control
116
boolean nullIfEmpty = new Boolean JavaDoc((String JavaDoc)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 JavaDoc)this.getParm(LAYOUT, LAYOUT_COMBO);
135         this.items = this.getItems(data);
136         this.size = (String JavaDoc)this.getParm(LIST_SIZE, "1");
137         this.multiple = new Boolean JavaDoc((String JavaDoc)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 JavaDoc[] getItems(RunData data)
148     {
149
150         String JavaDoc[] result = null;
151         String JavaDoc list = (String JavaDoc)this.getParm(ITEMS, "");
152
153         StringTokenizer JavaDoc it = new StringTokenizer JavaDoc(list, ",");
154         int size = it.countTokens();
155
156         if ( size > 0 )
157         {
158             result = new String JavaDoc[size];
159
160             int i = 0;
161             while ( it.hasMoreTokens() )
162             {
163                 String JavaDoc item = (String JavaDoc)it.nextToken();
164                 result[i] = item;
165                 i++;
166             }
167         }
168
169         return result;
170
171     }
172
173 }
174
Popular Tags