KickJava   Java API By Example, From Geeks To Geeks.

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


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.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
30 import javax.servlet.jsp.tagext.Tag JavaDoc;
31
32
33 /**
34  * <p>Nested tag that represents an individual "instant action". This tag
35  * is valid <strong>only</strong> when nested within an ActoinsTag tag.
36  * This tag has the following user-settable attributes:</p>
37  * <ul>
38  * <li><strong>selected</strong> - Set to <code>true</code> if this action
39  * should be selected when the control is initially displayed.</li>
40  * <li><strong>url</strong> - URL to which control should be transferred
41  * (in the current frame or window) if this action is selected.</li>
42  * </ul>
43  *
44  * <p>In addition, the body content of this tag is used as the user-visible
45  * label for the action, so that it may be conveniently localized.</p>
46  *
47  * <strong>FIXME</strong> - Internationalize the exception messages!
48  *
49  * @author Craig R. McClanahan
50  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:59:01 $
51  */

52
53 public class ActionTag extends BodyTagSupport JavaDoc {
54
55
56     // ----------------------------------------------------- Instance Variables
57

58
59     /**
60      * The label that will be rendered for this action.
61      */

62     protected String JavaDoc label = null;
63
64
65     // ------------------------------------------------------------- Properties
66

67
68     /**
69      * Should this action be selected when the control is initially displayed?
70      */

71     protected boolean selected = false;
72
73     public boolean getSelected() {
74         return (this.selected);
75     }
76
77     public void setSelected(boolean selected) {
78         this.selected = selected;
79     }
80
81     /**
82      * Should this action selection be disabled?
83      * e.g. Action separators should be disabled.
84      */

85     protected boolean disabled = false;
86
87     public boolean getDisabled() {
88         return (this.disabled);
89     }
90
91     public void setDisabled(boolean disabled) {
92         this.disabled = disabled;
93     }
94
95     /**
96      * The URL to which control is transferred if this action is selected.
97      */

98     protected String JavaDoc url = null;
99
100     public String JavaDoc getUrl() {
101         return (this.url);
102     }
103
104     public void setUrl(String JavaDoc url) {
105         this.url = url;
106     }
107
108
109     // --------------------------------------------------------- Public Methods
110

111
112     /**
113      * Process the start of this tag.
114      *
115      * @exception JspException if a JSP exception has occurred
116      */

117     public int doStartTag() throws JspException JavaDoc {
118
119         // Initialize the holder for our label text
120
this.label = null;
121
122         // Do no further processing for now
123
return (EVAL_BODY_TAG);
124
125     }
126
127
128     /**
129      * Process the body text of this tag (if any).
130      *
131      * @exception JspException if a JSP exception has occurred
132      */

133     public int doAfterBody() throws JspException JavaDoc {
134
135         String JavaDoc label = bodyContent.getString();
136         if (label != null) {
137             label = label.trim();
138             if (label.length() > 0)
139                 this.label = label;
140         }
141         return (SKIP_BODY);
142
143     }
144
145
146     /**
147      * Record this action with our surrounding ActionsTag instance.
148      *
149      * @exception JspException if a processing error occurs
150      */

151     public int doEndTag() throws JspException JavaDoc {
152
153         // Find our parent ActionsTag instance
154
Tag JavaDoc parent = getParent();
155         while ((parent != null) && !(parent instanceof ActionsTag)) {
156             parent = parent.getParent();
157         }
158         if ((parent == null) || !(parent instanceof ActionsTag))
159             throw new JspException JavaDoc("Must be nested in an ActionsTag isntance");
160         ActionsTag actions = (ActionsTag) parent;
161
162         // Register the information for the action represented by
163
// this action
164
HttpServletRequest JavaDoc request =
165             (HttpServletRequest JavaDoc) pageContext.getRequest();
166         HttpServletResponse JavaDoc response =
167             (HttpServletResponse JavaDoc) pageContext.getResponse();
168         String JavaDoc path = null;
169         if ((url != null) && (url.startsWith("/"))) {
170             path = request.getContextPath() + url;
171         } else {
172             path = url;
173         }
174         actions.addAction(label, selected, disabled,
175                           response.encodeURL(path));
176
177         return (EVAL_PAGE);
178
179     }
180
181
182     /**
183      * Release all state information set by this tag.
184      */

185     public void release() {
186
187         this.label = null;
188         this.selected = false;
189         this.disabled = false;
190         this.url = null;
191
192     }
193
194
195 }
196
Popular Tags