View Javadoc

1   /*
2    * $Id: FormButton.java 726715 2008-12-15 15:39:15Z musachy $
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  
22  package org.apache.struts2.components;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts2.views.annotations.StrutsTagAttribute;
28  import org.apache.struts2.dispatcher.mapper.ActionMapper;
29  import org.apache.struts2.dispatcher.mapper.ActionMapping;
30  
31  import com.opensymphony.xwork2.util.ValueStack;
32  import com.opensymphony.xwork2.inject.Inject;
33  
34  /***
35   * FormButton.
36   */
37  public abstract class FormButton extends ClosingUIBean {
38  
39      static final String BUTTONTYPE_INPUT = "input";
40      static final String BUTTONTYPE_BUTTON = "button";
41      static final String BUTTONTYPE_IMAGE = "image";
42  
43      protected String action;
44      protected String method;
45      protected String align;
46      protected String type;
47      protected ActionMapper actionMapper;
48  
49      public FormButton(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
50          super(stack, request, response);
51      }
52  
53      //public void evaluateParams() {
54      public void evaluateExtraParams() {
55          super.evaluateExtraParams();
56          if (align == null) {
57              align = "right";
58          }
59  
60          String submitType = BUTTONTYPE_INPUT;
61          if (type != null && (BUTTONTYPE_BUTTON.equalsIgnoreCase(type) || (supportsImageType() && BUTTONTYPE_IMAGE.equalsIgnoreCase(type))))
62          {
63              submitType = type;
64          }
65  
66          //super.evaluateParams();
67  
68          addParameter("type", submitType);
69  
70          if (!BUTTONTYPE_INPUT.equals(submitType) && (label == null)) {
71              addParameter("label", getParameters().get("nameValue"));
72          }
73  
74          if (action != null || method != null) {
75              String name;
76  
77              if (action != null) {
78                  ActionMapping mapping = new ActionMapping();
79                  mapping.setName(findString(action));
80                  if (method != null) {
81                      mapping.setMethod(findString(method));
82                  }
83                  mapping.setExtension("");
84                  name = "action:" + actionMapper.getUriFromActionMapping(mapping);
85              } else {
86                  name = "method:" + findString(method);
87              }
88  
89              addParameter("name", name);
90          }
91  
92          addParameter("align", findString(align));
93  
94      }
95  
96      /***
97       * Override UIBean's implementation, such that component Html id is determined
98       * in the following order :-
99       * <ol>
100      *   <li>This component id attribute</li>
101      *   <li>[containing_form_id]_[this_component_name]</li>
102      *   <li>[containing_form_id]_[this_component_action]_[this_component_method]</li>
103      *   <li>[containing_form_id]_[this_component_method]</li>
104      *   <li>[this_component_name]</li>
105      *   <li>[this_component_action]_[this_component_method]</li>
106      *   <li>[this_component_method]</li>
107      *   <li>[an increasing sequential number unique to the form starting with 0]</li>
108      * </ol>
109      */
110     protected void populateComponentHtmlId(Form form) {
111         String _tmp_id = "";
112         if (id != null) {
113             // this check is needed for backwards compatibility with 2.1.x
114         	_tmp_id = findStringIfAltSyntax(id);
115         }
116         else {
117             if (form != null && form.getParameters().get("id") != null) {
118                 _tmp_id = _tmp_id + form.getParameters().get("id").toString() + "_";
119             }
120             if (name != null) {
121                 _tmp_id = _tmp_id + escape(name);
122             } else if (action != null || method != null){
123                 if (action != null) {
124                     _tmp_id = _tmp_id + escape(action);
125                 }
126                 if (method != null) {
127                     _tmp_id = _tmp_id + "_" + escape(method);
128                 }
129             } else {
130                 // if form is null, this component is used, without a form, i guess
131                 // there's not much we could do then.
132                 if (form != null) {
133                     _tmp_id = _tmp_id + form.getSequence();
134                 }
135             }
136         }
137         addParameter("id", _tmp_id);
138     }
139 
140     /***
141      * Indicate whether the concrete button supports the type "image".
142      *
143      * @return <tt>true</tt> if type image is supported.
144      */
145     protected abstract boolean supportsImageType();
146 
147     @Inject
148     public void setActionMapper(ActionMapper mapper) {
149         this.actionMapper = mapper;
150     }
151 
152     @StrutsTagAttribute(description="Set action attribute.")
153     public void setAction(String action) {
154         this.action = action;
155     }
156 
157     @StrutsTagAttribute(description="Set method attribute.")
158     public void setMethod(String method) {
159         this.method = method;
160     }
161 
162     @StrutsTagAttribute(description="HTML align attribute.")
163     public void setAlign(String align) {
164         this.align = align;
165     }
166 
167     @StrutsTagAttribute(description="The type of submit to use. Valid values are <i>input</i>, " +
168                 "<i>button</i> and <i>image</i>.", defaultValue="input")
169     public void setType(String type) {
170         this.type = type;
171     }
172 }