KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > SubmitTag


1 /*
2  * $Id: SubmitTag.java 164530 2005-04-25 03:11:07Z niallp $
3  *
4  * Copyright 1999-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.html;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22
23 import org.apache.struts.taglib.TagUtils;
24 import org.apache.struts.util.MessageResources;
25
26 /**
27  * Tag for input fields of type "submit".
28  *
29  * @version $Rev: 164530 $ $Date: 2005-04-25 04:11:07 +0100 (Mon, 25 Apr 2005) $
30  */

31 public class SubmitTag extends BaseHandlerTag {
32
33
34     // ----------------------------------------------------- Instance Variables
35

36
37     /**
38      * The message resources for this package.
39      */

40     protected static MessageResources messages =
41      MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
42
43
44     /**
45      * The name of the generated input field.
46      */

47     protected String JavaDoc property = null;
48
49
50     /**
51      * The body content of this tag (if any).
52      */

53     protected String JavaDoc text = null;
54
55
56     /**
57      * The value of the button label.
58      */

59     protected String JavaDoc value = null;
60
61
62     // ------------------------------------------------------------- Properties
63

64
65     /**
66      * Return the property.
67      */

68     public String JavaDoc getProperty() {
69
70         return (this.property);
71
72     }
73
74
75     /**
76      * Set the property name.
77      *
78      * @param property The property name
79      */

80     public void setProperty(String JavaDoc property) {
81
82         this.property = property;
83
84     }
85
86
87     /**
88      * Return the label value.
89      */

90     public String JavaDoc getValue() {
91
92         return (this.value);
93
94     }
95
96
97     /**
98      * Set the label value.
99      *
100      * @param value The label value
101      */

102     public void setValue(String JavaDoc value) {
103
104         this.value = value;
105
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         // Do nothing until doEndTag() is called
120
this.text = null;
121         return (EVAL_BODY_TAG);
122
123     }
124
125
126
127     /**
128      * Save the associated label from the body content.
129      *
130      * @exception JspException if a JSP exception has occurred
131      */

132     public int doAfterBody() throws JspException JavaDoc {
133
134         if (bodyContent != null) {
135             String JavaDoc value = bodyContent.getString().trim();
136             if (value.length() > 0) {
137                 text = value;
138             }
139         }
140         return (SKIP_BODY);
141
142     }
143
144
145     /**
146      * Process the end of this tag.
147      * <p>
148      * Support for Indexed property since Struts 1.1
149      *
150      * @exception JspException if a JSP exception has occurred
151      */

152     public int doEndTag() throws JspException JavaDoc {
153
154         // Generate an HTML element
155
StringBuffer JavaDoc results = new StringBuffer JavaDoc();
156         results.append(getElementOpen());
157         prepareAttribute(results, "name", prepareName());
158         prepareButtonAttributes(results);
159         results.append(prepareEventHandlers());
160         results.append(prepareStyles());
161         prepareOtherAttributes(results);
162         results.append(getElementClose());
163
164         TagUtils.getInstance().write(pageContext, results.toString());
165
166         return (EVAL_PAGE);
167
168     }
169
170     /**
171      * Render the opening element.
172      *
173      * @return The opening part of the element.
174      */

175     protected String JavaDoc getElementOpen() {
176         return "<input type=\"submit\"";
177     }
178
179
180     /**
181      * Prepare the name element
182      * @return The element name.
183      */

184     protected String JavaDoc prepareName() throws JspException JavaDoc {
185
186         if (property == null) {
187             return null;
188         }
189
190         // * @since Struts 1.1
191
if(indexed) {
192             StringBuffer JavaDoc results = new StringBuffer JavaDoc();
193             results.append(property);
194             prepareIndex(results, null);
195             return results.toString();
196         }
197
198         return property;
199
200     }
201
202     /**
203      * Render the button attributes
204      * @param results The StringBuffer that output will be appended to.
205      */

206     protected void prepareButtonAttributes(StringBuffer JavaDoc results)
207                       throws JspException JavaDoc {
208         prepareAttribute(results, "accesskey", getAccesskey());
209         prepareAttribute(results, "tabindex", getTabindex());
210         prepareValue(results);
211     }
212
213     /**
214      * Render the value element
215      * @param results The StringBuffer that output will be appended to.
216      */

217     protected void prepareValue(StringBuffer JavaDoc results) {
218
219         // Acquire the label value we will be generating
220
String JavaDoc label = value;
221         if ((label == null) && (text != null))
222             label = text;
223         if ((label == null) || (label.length() < 1))
224             label = getDefaultValue();
225
226         prepareAttribute(results, "value", label);
227
228     }
229
230     /**
231      * Return the default value.
232      *
233      * @return The default value if none supplied.
234      */

235     protected String JavaDoc getDefaultValue() {
236         return "Submit";
237     }
238
239     /**
240      * Release any acquired resources.
241      */

242     public void release() {
243
244         super.release();
245         property = null;
246         text = null;
247         value = null;
248
249     }
250
251
252 }
253
Popular Tags