KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > ActionsTag


1 /*
2  * Copyright 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
18 package org.apache.webapp.admin;
19
20
21 import java.io.IOException JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
28
29
30 /**
31  * <p>JSP custom tag that renders an "instant actions" control. To the user,
32  * it appears as an HTML &lt;select&gt; element (i.e. a combo box), with
33  * the behavior of selecting a new page for the current frame or window when
34  * a different option is selected, without requiring a submit action.
35  * This tag has the following user-settable attributes:</p>
36  * <ul>
37  * <li><strong>size</strong> - (Integer) number of rows that will be visible
38  * to the user. If not specified, one row will be visible.</li>
39  * <li><strong>style</strong> - The CSS style class to be applied to the
40  * entire rendered output of the instant actions control, if any.</li>
41  * </ul>
42  *
43  * <strong>FIXME</strong> - Internationalize the exception messages!
44  *
45  * @author Craig R. McClanahan
46  * @version $Revision: 1.3 $ $Date: 2004/02/27 14:59:01 $
47  */

48
49 public class ActionsTag extends BodyTagSupport JavaDoc {
50
51
52     // ----------------------------------------------------- Manifest Constants
53

54
55     /**
56      * Attribute name used to indicate that we have generated the JavaScript
57      * function already on the current page. The value stored for this
58      * attribute is arbitrary - only its existence is relevant.
59      */

60     protected static final String JavaDoc FUNCTION_TAG =
61         "org.apache.webapp.admin.ActionsTag.FUNCTION_TAG";
62
63
64     // ----------------------------------------------------- Instance Variables
65

66
67     /**
68      * The set of labels for the Actions displayed by this control.
69      */

70     protected ArrayList JavaDoc labels = new ArrayList JavaDoc();
71
72
73     /**
74      * The set of "selected" flags for Actions displayed by this control.
75      */

76     protected ArrayList JavaDoc selecteds = new ArrayList JavaDoc();
77
78     /**
79      * The set of "disabled" flags for Actions displayed by this control.
80      */

81     protected ArrayList JavaDoc disableds = new ArrayList JavaDoc();
82
83     /**
84      * The set of URLs for the Actions displayed by this control.
85      */

86     protected ArrayList JavaDoc urls = new ArrayList JavaDoc();
87
88
89     // ------------------------------------------------------------- Properties
90

91
92     /**
93      * The number of elements that will be displayed to the user.
94      */

95     protected int size = 1;
96
97     public int getSize() {
98         return (this.size);
99     }
100
101     public void setSize(int size) {
102         this.size = size;
103     }
104
105
106     /**
107      * The CSS style class to be applied to the entire rendered output
108      * of this "instant actions" object.
109      */

110     protected String JavaDoc style = null;
111
112     public String JavaDoc getStyle() {
113         return (this.style);
114     }
115
116     public void setStyle(String JavaDoc style) {
117         this.style = style;
118     }
119
120
121     /**
122      * HTML Label tag text.
123      */

124     protected String JavaDoc label = null;
125
126     public String JavaDoc getLabel() {
127         return (this.label);
128     }
129
130     public void setLabel(String JavaDoc label) {
131         this.label = label;
132     }
133
134
135     // --------------------------------------------------------- Public Methods
136

137     public int doStartTag() throws JspException JavaDoc {
138
139         this.labels.clear();
140         this.selecteds.clear();
141         this.urls.clear();
142
143         return (EVAL_BODY_TAG);
144        
145     }
146     
147     
148     /**
149      * Render this instant actions control.
150      *
151      * @exception JspException if a processing error occurs
152      */

153     public int doEndTag() throws JspException JavaDoc {
154
155         JspWriter JavaDoc out = pageContext.getOut();
156
157         try {
158
159             // Render (once only) the JavaScript function we need
160
if (pageContext.getAttribute(FUNCTION_TAG) == null) {
161                 out.println();
162                 out.println("<script language=\"JavaScript\">");
163                 out.println("<!--");
164                 out.println("function IA_jumpMenu(targ,selObj) {");
165                 out.println(" dest = selObj.options[selObj.selectedIndex].value;");
166                 out.println(" if (dest.length > 0) {");
167                 out.println(" eval(targ+\".location='\"+dest+\"'\");");
168                 out.println(" }");
169                 out.println("}");
170                 out.println("//-->");
171                 out.println("</script>");
172                 out.println();
173                 pageContext.setAttribute(FUNCTION_TAG, Boolean.TRUE);
174             }
175
176             // Render LABEL element for section 508 accessibility
177

178             if (label != null) {
179                 out.print("<label for=\"labelId\">");
180                 out.print(label);
181                 out.println("</label>");
182             }
183             // Render the beginning of this element
184
out.println();
185             out.print("<select");
186             if (size > 1) {
187                 out.print(" size=\"");
188                 out.print(size);
189                 out.print("\"");
190             }
191             if (style != null) {
192                 out.print(" class=\"");
193                 out.print(style);
194                 out.print("\"");
195             }
196             if (label != null) {
197                 out.print(" id=\"");
198                 out.print("labelId");
199                 out.print("\"");
200             }
201             
202             out.print(" onchange=\"IA_jumpMenu('self',this)\"");
203             out.println(">");
204
205             // Render each defined action
206
int n = labels.size();
207             for (int i = 0; i < n; i++) {
208                 String JavaDoc label = (String JavaDoc) labels.get(i);
209                 boolean selected = ((Boolean JavaDoc) selecteds.get(i)).booleanValue();
210                 boolean disabled = ((Boolean JavaDoc) disableds.get(i)).booleanValue();
211                 String JavaDoc url = (String JavaDoc) urls.get(i);
212                 out.print("<option");
213                 if (selected)
214                     out.print(" selected=\"selected\"");
215                 if (disabled)
216                     out.print(" disabled=\"true\"");
217                 out.print(" value=\"");
218                 if (url != null)
219                     out.print(url);
220                 out.print("\"");
221                 out.print(">");
222                 if (label != null)
223                     out.print(label);
224                 out.println("</option>");
225             }
226
227             // Render the end of this element
228
out.println("</select>");
229             out.println();
230
231         } catch (IOException JavaDoc e) {
232             throw new JspException JavaDoc(e);
233         }
234
235         return (EVAL_PAGE);
236
237     }
238
239
240     /**
241      * Release all state information set by this tag.
242      */

243     public void release() {
244
245         this.labels.clear();
246         this.selecteds.clear();
247         this.urls.clear();
248
249         this.size = 1;
250         this.style = null;
251
252     }
253
254
255     // -------------------------------------------------------- Package Methods
256

257
258     /**
259      * Add a new Action to the set that will be rendered by this control.
260      *
261      * @param label Localized label visible to the user
262      * @param selected Initial selected state of this option
263      * @param disabled Ability to be selected state of this option
264      * @param url URL to which control should be transferred if selected
265      */

266     void addAction(String JavaDoc label, boolean selected, boolean disabled, String JavaDoc url) {
267
268         labels.add(label);
269         selecteds.add(new Boolean JavaDoc(selected));
270         disableds.add(new Boolean JavaDoc(disabled));
271         urls.add(url);
272
273     }
274
275
276     // ------------------------------------------------------ Protected Methods
277

278
279 }
280
Popular Tags