KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > tags > MultiEntryListBoxTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.input.tags;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.struts.taglib.TagUtils;
29
30 import com.sslexplorer.core.CoreUtil;
31
32 /**
33  * Custom tag than renders a component that allows the user to entry some value
34  * in a field, click on an <i>Add</i> button and have the value moved into a
35  * second list field. Values may also be removed from the second list. *
36  * <p>
37  * Originally written to lists of users to be built up, this component is now
38  * generic.
39  * <p>
40  * Values are passed between the component and the format implementation in
41  * newline delimited <i>Property List</i> format. See
42  * {@link com.sslexplorer.boot.PropertyList} for more details on this.
43  * <p>
44  * Titles may be provide for each side of the component using message resource
45  * keys.
46  * <h3>Supported Attributes</h3>
47  *
48  * TODO document the supported attributes
49  *
50  * @author Brett Smith <brett@3sp.com>
51  */

52 public class MultiEntryListBoxTag extends AbstractMultiFieldTag {
53     
54     final static Log log = LogFactory.getLog(MultiEntryListBoxTag.class);
55
56     // Protected instance variables
57

58     protected String JavaDoc entrySize;
59     protected String JavaDoc entryTitleKey;
60     protected String JavaDoc entryStyleClass;
61     protected String JavaDoc entryName;
62     protected String JavaDoc targetUnique;
63     protected String JavaDoc indicator;
64     protected boolean includeUserAttributes;
65     protected boolean includeSession;
66     protected boolean showReplacementVariables;
67     protected String JavaDoc replacementVariablesTitleKey;
68     protected String JavaDoc variables;
69     protected String JavaDoc replacementVariablesBundle;
70
71     /**
72      * Constructor
73      */

74     public MultiEntryListBoxTag() {
75         super();
76         this.type = "hidden";
77         targetUnique = "true";
78         indicator = null;
79         name = null;
80         showReplacementVariables = false;
81         replacementVariablesBundle = null;
82         includeSession = true;
83         includeUserAttributes = true;
84         setStyleId("multiEntry");
85         setRows("5");
86
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see org.apache.struts.taglib.html.BaseFieldTag#doStartTag()
93      */

94     public int doStartTag() throws JspException JavaDoc {
95
96         String JavaDoc entryTitle = null;
97         if (entryTitleKey != null) {
98             entryTitle = TagUtils.getInstance().message(pageContext, getBundle(), getLocale(), entryTitleKey, new String JavaDoc[] {});
99
100             if (entryTitle == null) {
101                 JspException JavaDoc e = new JspException JavaDoc(messages.getMessage("message.message", "\"" + entryTitleKey + "\""));
102                 TagUtils.getInstance().saveException(pageContext, e);
103                 throw e;
104             }
105         }
106
107         String JavaDoc targetTitle = prepareTargetTitle();
108
109         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<div ");
110         results.append(prepareStyles());
111         results.append(">");
112         results.append(this.renderInputElement());
113         results.append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
114         if (entryTitle != null || targetTitle != null) {
115             results.append("<tr class=\"header\"><td class=\"entry\">");
116             if (entryTitle != null) {
117                 results.append(entryTitle);
118             }
119             results.append("</td><td class=\"actions\"/><td class=\"target\">");
120             if (targetTitle != null) {
121                 results.append(targetTitle);
122             }
123             results.append("</td></tr>");
124         }
125         results.append("<tr class=\"body\"><td class=\"entry\">");
126         results.append(renderEntryComponent());
127         results.append("</td><td class=\"actions\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
128         results.append("<tr><td>");
129         results.append(renderAddComponent());
130         results.append("</td></tr><tr><td>");
131         results.append(renderRemoveSelectedComponent());
132         if (isAllowReordering()) {
133             results.append("</td></tr><tr><td>");
134             results.append(renderUpComponent());
135             results.append("</td></tr><tr><td>");
136             results.append(renderDownComponent());
137         }
138         results.append("</td></tr></table></td><td class=\"target\">");
139         results.append(renderTargetComponent());
140         results.append("</td></tr></table></div>");
141         TagUtils.getInstance().write(this.pageContext, results.toString());
142
143         return (EVAL_BODY_BUFFERED);
144     }
145
146     /**
147      * Get the size of the field to use for the entry side.
148      *
149      * @return the entrySize.
150      */

151     public String JavaDoc getEntrySize() {
152         return entrySize;
153     }
154
155     /**
156      * Set the size of the field to use on the entry side.
157      *
158      * @param entrySize size of entry field
159      */

160     public void setEntrySize(String JavaDoc entrySize) {
161         this.entrySize = entrySize;
162     }
163
164     /**
165      * Get the message resources key to use for the title on the entry side
166      *
167      * @return message resources key for entry title
168      */

169     public String JavaDoc getEntryTitleKey() {
170         return entryTitleKey;
171     }
172
173     /**
174      * Set the message resources key to use for the title on the entry side
175      *
176      * @param entryTitleKey message resources key for entry title
177      */

178     public void setEntryTitleKey(String JavaDoc entryTitleKey) {
179         this.entryTitleKey = entryTitleKey;
180     }
181
182     /**
183      * Set the CSS class to use for the entry field
184      *
185      * @param entryStyleClass CSS class to use for entry field
186      */

187     public void setEntryStyleClass(String JavaDoc entryStyleClass) {
188         this.entryStyleClass = entryStyleClass;
189     }
190
191     /**
192      * Set the name attribute to use for the entry field
193      *
194      * @param entryName name attribute use for entry field
195      */

196     public void setEntryName(String JavaDoc entryName) {
197         this.entryName = entryName;
198     }
199
200     /**
201      * Set the name of the <i>Indicator</i>. Using this in conjunction with
202      * AjaxTags allows a throbber to be displayed whilst the XML data is being
203      * retrieved.
204      * <p>
205      * The value of the indicator must match the value provided to the AjaxTags
206      * autocomplete tag and there must also be an image with the same name (with
207      * .gif append) in the themes top level image directory.
208      *
209      * @param indicator indicator name
210      */

211     public void setIndicator(String JavaDoc indicator) {
212         this.indicator = indicator;
213     }
214
215     /**
216      * Set the messsage resources key to use for the title of the replacement
217      * variables popup.
218      *
219      * @param replacementVariablesTitleKey replacement variables title message
220      * resources key
221      */

222     public void setReplacementVariablesTitleKey(String JavaDoc replacementVariablesTitleKey) {
223         this.replacementVariablesTitleKey = replacementVariablesTitleKey;
224     }
225
226     /**
227      * Set the messsage resources bundle to use for the title of the replacement
228      * variables popup and the descriptions of the replacement variables
229      * themselves
230      *
231      * @param replacementVariablesBundle replacement variables message resources
232      * bundle
233      */

234     public void setReplacementVariablesBundle(String JavaDoc replacementVariablesBundle) {
235         this.replacementVariablesBundle = replacementVariablesBundle;
236     }
237
238     /**
239      * Set the comma separated list of variables names
240      *
241      * @param variables variables.
242      */

243     public void setVariables(String JavaDoc variables) {
244         this.variables = variables;
245     }
246
247     /**
248      * Set whether all of the user attributes should be added to the list of
249      * variables
250      *
251      * @param includeUserAttributes include user attributes
252      *
253      */

254     public void setIncludeUserAttributes(boolean includeUserAttributes) {
255         this.includeUserAttributes = includeUserAttributes;
256     }
257
258     /**
259      * Set whether all of the <i>session</i> replacement variables
260      *
261      * @param includeSession include session
262      *
263      */

264     public void setIncludeSession(boolean includeSession) {
265         this.includeSession = includeSession;
266     }
267
268     /**
269      * Set whether the replacement variables selection box should be displayed.
270      *
271      * @param showReplacementVariables show replacement variables
272      */

273     public void setShowReplacementVariables(boolean showReplacementVariables) {
274         this.showReplacementVariables = showReplacementVariables;
275     }
276
277     /*
278      * (non-Javadoc)
279      *
280      * @see javax.servlet.jsp.tagext.Tag#release()
281      */

282     public void release() {
283         super.release();
284         this.type = "hidden";
285         entryName = null;
286         targetUnique = "true";
287         indicator = null;
288         setStyleId("multiEntry");
289         setRows("5");
290         variables = null;
291         includeUserAttributes = true;
292         includeSession = true;
293         showReplacementVariables = false;
294         replacementVariablesBundle = null;
295     }
296
297     protected String JavaDoc renderEntryComponent() throws JspException JavaDoc {
298         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input type=\"text\"");
299         results.append(prepareDisabled());
300         results.append(" id=\"");
301         String JavaDoc entryId = prepareEntryId();
302         results.append(entryId);
303         results.append("\"");
304         results.append(" class=\"");
305         results.append(showReplacementVariables ? (indicator != null && !indicator.equals("") ? "twoButtons" : "oneButton")
306                         : (indicator != null && !indicator.equals("") ? "oneButton" : "noButtons"));
307         results.append("\"");
308         if (entrySize != null && !entrySize.equals("")) {
309             results.append(" size=\"");
310             results.append(entrySize);
311             results.append("\"");
312         }
313         if (entryName != null && !entryName.equals("")) {
314             results.append(" name=\"");
315             results.append(entryName);
316             results.append("\"");
317         }
318         results.append("/>");
319         if (showReplacementVariables) {
320             results.append(VariablesTag.generateReplacementVariableChooserFragment(replacementVariablesTitleKey, pageContext,
321                             replacementVariablesBundle == null ? "navigation" : replacementVariablesBundle, getLocale(), entryId,
322                             variables, includeSession, includeUserAttributes, false));
323         }
324         if (indicator != null && !indicator.equals("")) {
325             results.append("<span id=\"");
326             results.append(indicator);
327             results.append("\" style=\"display:none;\"><img SRC=\"");
328             results.append(CoreUtil.getThemePath(pageContext.getSession()));
329             results.append("/images/indicator.gif\"/></span>");
330         }
331         return results.toString();
332
333     }
334
335     protected String JavaDoc renderAddComponent() throws JspException JavaDoc {
336         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input class=\"multiAdd\"");
337         results.append(prepareDisabled());
338         results.append(" onclick=\"multiEntryAddEntry(");
339         results.append("document.getElementById('");
340         if (indexed) {
341             this.prepareIndex(results, name);
342         }
343         results.append(property);
344         results.append("'), document.getElementById('");
345         results.append(prepareTargetId());
346         results.append("'), document.getElementById('");
347         results.append(prepareEntryId());
348         results.append("'),'");
349         results.append(targetUnique);
350         results.append("');\" type=\"button\" value=\"");
351         results.append(prepareAdd());
352         results.append("\"/>");
353         return results.toString();
354     }
355
356     protected String JavaDoc renderRemoveSelectedComponent() throws JspException JavaDoc {
357         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input class=\"multiRemove\"");
358         results.append(prepareDisabled());
359         results.append(" onclick=\"multiEntryRemoveSelectedEntry(");
360         results.append("document.getElementById('");
361         if (indexed) {
362             this.prepareIndex(results, name);
363         }
364         results.append(property);
365         results.append("'), document.getElementById('");
366         results.append(prepareTargetId());
367         results.append("'), document.getElementById('");
368         results.append(prepareEntryId());
369         results.append("'));\" type=\"button\" value=\"");
370         results.append(prepareRemove());
371         results.append("\"/>");
372         return results.toString();
373     }
374
375     protected String JavaDoc prepareEntryId() throws JspException JavaDoc {
376         StringBuffer JavaDoc results = new StringBuffer JavaDoc("entryValue_");
377         if (indexed) {
378             prepareIndex(results, name);
379         }
380         results.append(property);
381         return results.toString();
382     }
383
384     protected String JavaDoc renderTargetComponent() throws JspException JavaDoc {
385         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<select multiple id=\"");
386         results.append(prepareTargetId());
387         results.append("\"");
388         if (rows != null) {
389             results.append(" size=\"");
390             results.append(rows);
391             results.append("\"");
392         }
393         results.append(">");
394         Object JavaDoc value = TagUtils.getInstance().lookup(pageContext, name, property, null);
395         if (value == null || !(value instanceof String JavaDoc)) {
396             log.error("Name / property attributes (" + name + "/" + property + ") must specify a newline (\\n) separated string.");
397         }
398         else {
399             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(value.toString(), "\n");
400             while (t.hasMoreTokens()) {
401                 String JavaDoc val = t.nextToken();
402                 results.append("<option value=\"");
403                 results.append(val);
404                 results.append("\">");
405                 results.append(val);
406                 results.append("</option>");
407             }
408         }
409         results.append("</select>");
410         return results.toString();
411
412     }
413
414     protected String JavaDoc renderInputElement() throws JspException JavaDoc {
415         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input type=\"");
416         results.append(this.type);
417         results.append("\" name=\"");
418
419         if (indexed) {
420             this.prepareIndex(results, name);
421         }
422
423         results.append(property);
424         results.append("\"");
425
426         if (entryStyleClass != null) {
427             results.append(" class=\"");
428             results.append(entryStyleClass);
429             results.append("\"");
430         }
431
432         results.append(" id=\"");
433         if (indexed) {
434             this.prepareIndex(results, name);
435         }
436
437         results.append(property);
438         results.append("\"");
439
440         results.append(" value=\"");
441         if (value != null) {
442             results.append(this.formatValue(value));
443
444         } else if (redisplay || !"password".equals(type)) {
445             Object JavaDoc value = TagUtils.getInstance().lookup(pageContext, name, property, null);
446
447             results.append(this.formatValue(value));
448         }
449         results.append('"');
450         results.append(prepareDisabled());
451         results.append(this.prepareEventHandlers());
452         results.append(this.getElementClose());
453         return results.toString();
454     }
455 }
456
Popular Tags