View Javadoc

1   /*
2    * $Id: FormButton.java 651946 2008-04-27 13:41:38Z apetrelli $
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 UIBean {
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             if (altSyntax()) {
115                 _tmp_id = findString(id);
116             } else {
117                 _tmp_id = id;
118             }
119         }
120         else {
121             if (form != null && form.getParameters().get("id") != null) {
122                 _tmp_id = _tmp_id + form.getParameters().get("id").toString() + "_";
123             }
124             if (name != null) {
125                 _tmp_id = _tmp_id + escape(name);
126             } else if (action != null || method != null){
127                 if (action != null) {
128                     _tmp_id = _tmp_id + escape(action);
129                 }
130                 if (method != null) {
131                     _tmp_id = _tmp_id + "_" + escape(method);
132                 }
133             } else {
134                 // if form is null, this component is used, without a form, i guess
135                 // there's not much we could do then.
136                 if (form != null) {
137                     _tmp_id = _tmp_id + form.getSequence();
138                 }
139             }
140         }
141         addParameter("id", _tmp_id);
142     }
143 
144     /***
145      * Indicate whether the concrete button supports the type "image".
146      *
147      * @return <tt>true</tt> if type image is supported.
148      */
149     protected abstract boolean supportsImageType();
150 
151     @Inject
152     public void setActionMapper(ActionMapper mapper) {
153         this.actionMapper = mapper;
154     }
155 
156     @StrutsTagAttribute(description="Set action attribute.")
157     public void setAction(String action) {
158         this.action = action;
159     }
160 
161     @StrutsTagAttribute(description="Set method attribute.")
162     public void setMethod(String method) {
163         this.method = method;
164     }
165 
166     @StrutsTagAttribute(description="HTML align attribute.")
167     public void setAlign(String align) {
168         this.align = align;
169     }
170 
171     @StrutsTagAttribute(description="The type of submit to use. Valid values are <i>input</i>, " +
172                 "<i>button</i> and <i>image</i>.", defaultValue="input")
173     public void setType(String type) {
174         this.type = type;
175     }
176 }