View Javadoc

1   /*
2    * $Id: FormButton.java 497654 2007-01-19 00:21:57Z rgielen $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.components;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts2.views.annotations.StrutsTagAttribute;
27  
28  import com.opensymphony.xwork2.util.ValueStack;
29  
30  /***
31   * FormButton.
32   */
33  public abstract class FormButton extends UIBean {
34  
35      static final String BUTTONTYPE_INPUT = "input";
36      static final String BUTTONTYPE_BUTTON = "button";
37      static final String BUTTONTYPE_IMAGE = "image";
38  
39      protected String action;
40      protected String method;
41      protected String align;
42      protected String type;
43  
44      public FormButton(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
45          super(stack, request, response);
46      }
47  
48      //public void evaluateParams() {
49      public void evaluateExtraParams() {
50          super.evaluateExtraParams();
51          if (align == null) {
52              align = "right";
53          }
54  
55          String submitType = BUTTONTYPE_INPUT;
56          if (type != null && (BUTTONTYPE_BUTTON.equalsIgnoreCase(type) || (supportsImageType() && BUTTONTYPE_IMAGE.equalsIgnoreCase(type))))
57          {
58              submitType = type;
59          }
60  
61          //super.evaluateParams();
62  
63          addParameter("type", submitType);
64  
65          if (!BUTTONTYPE_INPUT.equals(submitType) && (label == null)) {
66              addParameter("label", getParameters().get("nameValue"));
67          }
68  
69          if (action != null || method != null) {
70              String name;
71  
72              if (action != null) {
73                  name = "action:" + findString(action);
74  
75                  if (method != null) {
76                      name += "!" + findString(method);
77                  }
78              } else {
79                  name = "method:" + findString(method);
80              }
81  
82              addParameter("name", name);
83          }
84  
85          addParameter("align", findString(align));
86  
87      }
88  
89      /***
90       * Override UIBean's implementation, such that component Html id is determined
91       * in the following order :-
92       * <ol>
93       *   <li>This component id attribute</li>
94       *   <li>[containing_form_id]_[this_component_name]</li>
95       *   <li>[containing_form_id]_[this_component_action]_[this_component_method]</li>
96       *   <li>[containing_form_id]_[this_component_method]</li>
97       *   <li>[this_component_name]</li>
98       *   <li>[this_component_action]_[this_component_method]</li>
99       *   <li>[this_component_method]</li>
100      *   <li>[an increasing sequential number unique to the form starting with 0]</li>
101      * </ol>
102      */
103     protected void populateComponentHtmlId(Form form) {
104         String _tmp_id = "";
105         if (id != null) {
106             // this check is needed for backwards compatibility with 2.1.x
107             if (altSyntax()) {
108                 _tmp_id = findString(id);
109             } else {
110                 _tmp_id = id;
111             }
112         }
113         else {
114             if (form != null && form.getParameters().get("id") != null) {
115                 _tmp_id = _tmp_id + form.getParameters().get("id").toString() + "_";
116             }
117             if (name != null) {
118                 _tmp_id = _tmp_id + escape(name);
119             } else if (action != null || method != null){
120                 if (action != null) {
121                     _tmp_id = _tmp_id + escape(action);
122                 }
123                 if (method != null) {
124                     _tmp_id = _tmp_id + "_" + escape(method);
125                 }
126             } else {
127                 // if form is null, this component is used, without a form, i guess
128                 // there's not much we could do then.
129                 if (form != null) {
130                     _tmp_id = _tmp_id + form.getSequence();
131                 }
132             }
133         }
134         addParameter("id", _tmp_id);
135     }
136 
137     /***
138      * Indicate whether the concrete button supports the type "image".
139      *
140      * @return <tt>true</tt> if type image is supported.
141      */
142     protected abstract boolean supportsImageType();
143 
144     @StrutsTagAttribute(description="Set action attribute.")
145     public void setAction(String action) {
146         this.action = action;
147     }
148 
149     @StrutsTagAttribute(description="Set method attribute.")
150     public void setMethod(String method) {
151         this.method = method;
152     }
153 
154     @StrutsTagAttribute(description="HTML align attribute.")
155     public void setAlign(String align) {
156         this.align = align;
157     }
158 
159     @StrutsTagAttribute(description="The type of submit to use. Valid values are <i>input</i>, " +
160                 "<i>button</i> and <i>image</i>.", defaultValue="input")
161     public void setType(String type) {
162         this.type = type;
163     }
164 }