001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.components;
006:
007: import com.opensymphony.xwork.util.OgnlValueStack;
008:
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: /**
013: * FormButton.
014: *
015: * @author <a href="mailto:gielen@it-neering.net">Rene Gielen</a>
016: * @author tm_jee
017: */
018:
019: public abstract class FormButton extends UIBean {
020:
021: static final String BUTTONTYPE_INPUT = "input";
022: static final String BUTTONTYPE_BUTTON = "button";
023: static final String BUTTONTYPE_IMAGE = "image";
024:
025: protected String action;
026: protected String method;
027: protected String align;
028: protected String type;
029:
030: public FormButton(OgnlValueStack stack, HttpServletRequest request,
031: HttpServletResponse response) {
032: super (stack, request, response);
033: }
034:
035: //public void evaluateParams() {
036: public void evaluateExtraParams() {
037: super .evaluateExtraParams();
038: if (align == null) {
039: align = "right";
040: }
041:
042: String submitType = BUTTONTYPE_INPUT;
043: if (type != null
044: && (BUTTONTYPE_BUTTON.equalsIgnoreCase(type) || (supportsImageType() && BUTTONTYPE_IMAGE
045: .equalsIgnoreCase(type)))) {
046: submitType = type;
047: }
048:
049: //super.evaluateParams();
050:
051: addParameter("type", submitType);
052:
053: if (!BUTTONTYPE_INPUT.equals(submitType) && (label == null)) {
054: addParameter("label", getParameters().get("nameValue"));
055: }
056:
057: if (action != null || method != null) {
058: String name;
059:
060: if (action != null) {
061: name = "action:" + findString(action);
062:
063: if (method != null) {
064: name += "!" + findString(method);
065: }
066: } else {
067: name = "method:" + findString(method);
068: }
069:
070: addParameter("name", name);
071: }
072:
073: addParameter("align", findString(align));
074: }
075:
076: /**
077: * Override UIBean's implementation, such that component Html id is determined
078: * in the following order :-
079: * <ol>
080: * <li>This component id attribute</li>
081: * <li>[containing_form_id]_[this_component_name]</li>
082: * <li>[containing_form_id]_[this_component_action]_[this_component_method]</li>
083: * <li>[containing_form_id]_[this_component_method]</li>
084: * <li>[this_component_name]</li>
085: * <li>[this_component_action]_[this_component_method]</li>
086: * <li>[this_component_method]</li>
087: * <li>[an increasing sequential number unique to the form starting with 0]</li>
088: * </ol>
089: */
090: protected void populateComponentHtmlId(Form form) {
091: String _tmp_id = "";
092: if (id != null) {
093: // this check is needed for backwards compatibility with 2.1.x
094: if (altSyntax()) {
095: _tmp_id = findString(id);
096: } else {
097: _tmp_id = id;
098: }
099: } else {
100: if (form != null && form.getParameters().get("id") != null) {
101: _tmp_id = _tmp_id
102: + form.getParameters().get("id").toString()
103: + "_";
104: }
105: if (name != null) {
106: _tmp_id = _tmp_id + escape(name);
107: } else if (action != null || method != null) {
108: if (action != null) {
109: _tmp_id = _tmp_id + escape(action);
110: }
111: if (method != null) {
112: _tmp_id = _tmp_id + "_" + escape(method);
113: }
114: } else {
115: // if form is null, this component is used, without a form, i guess
116: // there's not much we could do then.
117: if (form != null) {
118: _tmp_id = _tmp_id + form.getSequence();
119: }
120: }
121: }
122: addParameter("id", _tmp_id);
123: }
124:
125: /**
126: * Indicate whether the concrete button supports the type "image".
127: *
128: * @return <tt>true</tt> if type image is supported.
129: */
130: protected abstract boolean supportsImageType();
131:
132: /**
133: * Set action attribute.
134: *
135: * @ww.tagattribute required="false" type="String"
136: */
137: public void setAction(String action) {
138: this .action = action;
139: }
140:
141: /**
142: * Set method attribute.
143: *
144: * @ww.tagattribute required="false" type="String"
145: */
146: public void setMethod(String method) {
147: this .method = method;
148: }
149:
150: /**
151: * HTML align attribute.
152: *
153: * @ww.tagattribute required="false" type="String"
154: */
155: public void setAlign(String align) {
156: this .align = align;
157: }
158:
159: /**
160: * The type of submit to use. Valid values are <i>input</i>, <i>button</i> and <i>image</i>.
161: *
162: * @ww.tagattribute required="false" type="String" default="input"
163: */
164: public void setType(String type) {
165: this.type = type;
166: }
167: }
|