001: package org.apache.turbine.util.template;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.ecs.html.Option;
023: import org.apache.ecs.html.Select;
024:
025: /**
026: * This class is for generating a SelectorBox. It is good when used
027: * with WM because you can stuff it into the context and then just
028: * call it to generate the HTML. It can be used in other cases as
029: * well, but WM is the best case for it right now.
030: *
031: * <p>For example code showing the usage for this module, please see
032: * the toString() method below to see how it would be refered to from
033: * WM.
034: *
035: * <pre>
036: * // get the roles for a user
037: * RoleSet userRoles = new DefaultAccessControl().getRoles(loginid, null);
038: * if ( userRoles != null )
039: * {
040: * context.put("hasRoleSet", Boolean.TRUE);
041: *
042: * // get an array of the users roles
043: * Role[] usersRoles = userRoles.getRolesArray();
044: * // get an array of all the roles in the system
045: * Role[] allRoles = ((RoleSet)RolePeer.retrieveSet()).getRolesArray();
046: *
047: * Object[] names = new Object[allRoles.length];
048: * Object[] values = new Object[allRoles.length];
049: * for ( int i=0;i<allRoles.length; i++ )
050: * {
051: * names[i] = new Integer(allRoles[i].getPrimaryKey()).toString();
052: * values[i] = allRoles[i].getName();
053: * }
054: *
055: * SelectorBox sb = new SelectorBox("roleSetBox", names, values);
056: * sb.buildBooleans(usersRoles, allRoles);
057: * context.put("roleSetBox", sb);
058: * }
059: * else
060: * {
061: * context.put("hasRoleSet", Boolean.FALSE);
062: * }
063: * </pre>
064: *
065: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
066: * @version $Id: SelectorBox.java 534527 2007-05-02 16:10:59Z tv $
067: */
068: public class SelectorBox {
069: /** This is the Select ECS element. */
070: private Select sel = null;
071:
072: /** This is the size of the Select statement. */
073: private int size = 1;
074:
075: /** This is the name= value. */
076: private String name = null;
077:
078: /** This is the value= portion of the option element. */
079: private Object[] names = null;
080:
081: /** This is the data after the option element. */
082: private Object[] values = null;
083:
084: /** This is an array of which items are selected. */
085: private boolean[] selected = null;
086:
087: /**
088: * Generic constructor, builds a select box with a default size of
089: * 1 and no selected items.
090: *
091: * @param name A String with the name for the select box.
092: * @param names An Object[] with the names.
093: * @param values An Object[] with the values.
094: */
095: public SelectorBox(String name, Object[] names, Object[] values) {
096: this (name, names, values, 1, null);
097: }
098:
099: /**
100: * Generic constructor builds a select box.
101: *
102: * @param name A String with the name for the select box.
103: * @param names An Object[] with the names.
104: * @param values An Object[] with the values.
105: * @param size An int specifying the size.
106: */
107: public SelectorBox(String name, Object[] names, Object[] values,
108: int size) {
109: this (name, names, values, size, null);
110: }
111:
112: /**
113: * Generic constructor builds a select box.
114: *
115: * @param name A String with the name for the select box.
116: * @param names An Object[] with the names.
117: * @param values An Object[] with the values.
118: * @param selected A boolean[] with the selected items.
119: */
120: public SelectorBox(String name, Object[] names, Object[] values,
121: boolean[] selected) {
122: this (name, names, values, 1, selected);
123: }
124:
125: /**
126: * Primary constructor for everything.
127: *
128: * @param name A String with the name for the select box.
129: * @param names An Object[] with the names.
130: * @param values An Object[] with the values.
131: * @param size An int specifying the size.
132: * @param selected A boolean[] with the selected items.
133: */
134: public SelectorBox(String name, Object[] names, Object[] values,
135: int size, boolean[] selected) {
136: this .name = name;
137: this .names = names;
138: this .values = values;
139: this .size = size;
140: this .selected = selected;
141:
142: sel = new Select(name, size);
143: sel.setName(name);
144: sel.setSize(size);
145: }
146:
147: /**
148: * Pass in an array of selected items and the entire set of items
149: * and it will determine which items in the selected set are also
150: * in the entireset and then build a boolean[] up that is the same
151: * size as the entireSet with markings to tell whether or not the
152: * items are marked or not. It uses toString().equalsIgnoreCase()
153: * on the Object in the Object[] to determine if the items are
154: * equal.
155: *
156: * @param selectedSet An Object[].
157: * @param entireSet An Object[].
158: */
159: public void buildBooleans(Object[] selectedSet, Object[] entireSet) {
160: selected = new boolean[entireSet.length];
161: for (int j = 0; j < entireSet.length; j++) {
162: Object r2 = entireSet[j];
163: for (int i = 0; i < selectedSet.length; i++) {
164: Object r1 = selectedSet[i];
165: if (r1 != null
166: && r2 != null
167: && r1.toString()
168: .equalsIgnoreCase(r2.toString())) {
169: selected[j] = true;
170: }
171: }
172: }
173: }
174:
175: /**
176: * This builds out the select box at a certain size. To use this
177: * element in WM, you simply build this object in your java code,
178: * put it into the context and then call $selectBox.toString(5).
179: *
180: * @param size An int with the size.
181: * @return A String with the HTML code.
182: */
183: public String toString(int size) {
184: sel.setSize(size);
185: sel.setName(name);
186: for (int f = 0; f < values.length; f++) {
187: Option opt = new Option((String) values[f]);
188: opt.addElement((String) names[f]);
189: if (selected != null && selected[f] == true) {
190: opt.setSelected(true);
191: }
192: sel.addElement(opt);
193: }
194: String output = sel.toString();
195: reset();
196: return output;
197: }
198:
199: /**
200: * Resets the internal state of the SelectorBox.
201: */
202: public void reset() {
203: sel = new Select(name, size);
204: }
205:
206: /**
207: * This builds out the select box at a certain size. To use this
208: * element in WM, you simply build this object in your java code,
209: * put it into the context and then call $selectBox and it will
210: * build it with the default size of 1.
211: *
212: * @return A String with the HTML code.
213: */
214: public String toString() {
215: return this .toString(size);
216: }
217:
218: /**
219: * This allows you to set the multiple attribute to the select
220: * element. Example usage from within WM is like this:
221: *
222: * <p>
223: * $selectBox.setMultiple(true).toString(4)
224: *
225: * @param val True if multiple selection should be allowed.
226: * @return A SelectorBox (self).
227: */
228: public SelectorBox setMultiple(boolean val) {
229: sel.setMultiple(val);
230: return this ;
231: }
232:
233: /**
234: * This allows one to set the name= attribute to the select
235: * element.
236: *
237: * @param name A String with the name.
238: * @return A SelectorBox (self).
239: */
240: public SelectorBox setName(String name) {
241: this .name = name;
242: sel.setName(name);
243: return this ;
244: }
245:
246: /**
247: * This allows one to set the size of the select element.
248: *
249: * @param size An int with the size.
250: * @return A SelectorBox (self).
251: */
252: public SelectorBox setSize(int size) {
253: this .size = size;
254: sel.setSize(size);
255: return this ;
256: }
257:
258: /**
259: * This allows one to set an onChange attribute on the select tag
260: *
261: * @param script A string with the script to put in onChange
262: * @return A SelectorBox (self).
263: */
264: public SelectorBox setOnChange(String script) {
265: sel.setOnChange(script);
266: return this ;
267: }
268:
269: /**
270: * This allows one to set the array of selected booleans.
271: *
272: * @param an array of booleans
273: * @return A SelectorBox (self).
274: */
275: public SelectorBox setSelected(boolean[] bools) {
276: this .selected = bools;
277: return this ;
278: }
279:
280: /**
281: * This will set all elements as unselected, except for the
282: * element(s) with the given name.
283: *
284: * @param name The name to appear as selected.
285: * @return A SelectorBox (self).
286: */
287: public SelectorBox setSelected(Object name) {
288: if (name != null) {
289: selected = new boolean[names.length];
290: for (int i = 0; i < names.length; i++) {
291: Object o = names[i];
292: if (o != null
293: && o.toString().equalsIgnoreCase(
294: name.toString())) {
295: selected[i] = true;
296: }
297: }
298: }
299: return this;
300: }
301: }
|