001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.jsp.tag.uilib.form.element;
016:
017: import java.io.IOException;
018: import java.io.Writer;
019: import javax.servlet.jsp.JspException;
020: import org.apache.commons.lang.StringEscapeUtils;
021: import org.araneaframework.jsp.UiUpdateEvent;
022: import org.araneaframework.jsp.exception.AraneaJspException;
023: import org.araneaframework.jsp.util.JspStringUtil;
024: import org.araneaframework.jsp.util.JspUtil;
025: import org.araneaframework.jsp.util.JspWidgetCallUtil;
026: import org.araneaframework.uilib.event.OnClickEventListener;
027:
028: /**
029: * Button form element tag, represents {@link org.araneaframework.uilib.form.control.ButtonControl}.
030: * Rendered with either <input type="button"> or just <button>
031: * depending on <code>renderMode</code> attribute.
032: *
033: * @author Oleg Mürk
034: *
035: * @jsp.tag
036: * name = "button"
037: * body-content = "JSP"
038: * description = "Form button, represents UiLib "ButtonControl"."
039: */
040: public class FormButtonHtmlTag extends BaseFormButtonTag {
041: public static final String RENDER_BUTTON = "button";
042: public static final String RENDER_INPUT = "input";
043: protected String renderMode = FormButtonHtmlTag.RENDER_BUTTON;
044:
045: {
046: baseStyleClass = "aranea-button";
047: }
048:
049: protected int doStartTag(Writer out) throws Exception {
050: super .doStartTag(out);
051:
052: // Prepare
053: String name = this .getFullFieldId();
054:
055: JspUtil
056: .writeOpenStartTag(
057: out,
058: renderMode
059: .equals(FormButtonHtmlTag.RENDER_BUTTON) ? FormButtonHtmlTag.RENDER_BUTTON
060: : FormButtonHtmlTag.RENDER_INPUT);
061:
062: if (renderMode.equals(FormButtonHtmlTag.RENDER_INPUT))
063: JspUtil.writeAttribute(out, "type", "button");
064: JspUtil.writeAttribute(out, "id", name);
065: JspUtil.writeAttribute(out, "name", name);
066: JspUtil.writeAttribute(out, "class", getStyleClass());
067: JspUtil.writeAttribute(out, "style", getStyle());
068: if (showLabel
069: && renderMode.equals(FormButtonHtmlTag.RENDER_INPUT)) {
070: if (accessKey != null) {
071: String escapedLabel = StringEscapeUtils
072: .escapeHtml(localizedLabel);
073: JspUtil.writeAttribute(out, "value", JspStringUtil
074: .underlineAccessKey(escapedLabel, accessKey));
075: } else {
076: JspUtil.writeAttribute(out, "value", localizedLabel);
077: }
078: }
079: if (renderMode.equals(FormButtonHtmlTag.RENDER_BUTTON))
080: JspUtil.writeAttribute(out, "label", localizedLabel);
081: JspUtil.writeAttribute(out, "tabindex", tabindex);
082: if (events && !viewModel.isDisabled()) {
083: writeEventAttribute(out);
084: } else if (viewModel.isDisabled()) {
085: JspUtil.writeAttribute(out, "disabled", "true");
086: }
087: JspUtil.writeAttributes(out, attributes);
088: if (accessKey != null)
089: JspUtil.writeAttribute(out, "accesskey", accessKey);
090: if (renderMode.equals(FormButtonHtmlTag.RENDER_BUTTON))
091: JspUtil.writeCloseStartTag_SS(out);
092: if (renderMode.equals(FormButtonHtmlTag.RENDER_INPUT))
093: JspUtil.writeCloseStartEndTag(out);
094:
095: // Continue
096: return EVAL_BODY_INCLUDE;
097: }
098:
099: protected int doEndTag(Writer out) throws Exception {
100:
101: if (renderMode.equals(FormButtonHtmlTag.RENDER_BUTTON)) {
102: if (showLabel) {
103: if (accessKey != null) {
104: String escapedLabel = StringEscapeUtils
105: .escapeHtml(localizedLabel);
106: out.write(JspStringUtil.underlineAccessKey(
107: escapedLabel, accessKey));
108: } else {
109: JspUtil.writeEscaped(out, localizedLabel);
110: }
111: }
112: JspUtil.writeEndTag(out, FormButtonHtmlTag.RENDER_BUTTON);
113: }
114:
115: // Continue
116: super .doEndTag(out);
117: return EVAL_PAGE;
118: }
119:
120: /**
121: * @jsp.attribute
122: * type = "java.lang.String"
123: * required = "false"
124: * description =
125: * "Allowed values are (button | input) - the corresponding HTML tag will be used for rendering. Default is button."
126: */
127: public void setRenderMode(String renderMode) throws JspException {
128: if (!(renderMode.equals(FormButtonHtmlTag.RENDER_BUTTON) || renderMode
129: .equals(FormButtonHtmlTag.RENDER_INPUT)))
130: throw new AraneaJspException(
131: "<ui:button> 'renderMode' attribute must be '"
132: + FormButtonHtmlTag.RENDER_BUTTON
133: + "' or '" + FormButtonHtmlTag.RENDER_INPUT
134: + "'");
135: this .renderMode = (String) evaluate("renderMode", renderMode,
136: String.class);
137: }
138:
139: protected boolean writeEventAttribute(Writer out)
140: throws IOException, JspException {
141: UiUpdateEvent event = new UiUpdateEvent();
142: event.setId(OnClickEventListener.ON_CLICK_EVENT);
143: event.setTarget(formFullId + "." + derivedId);
144: event.setUpdateRegionNames(updateRegionNames);
145: event.setEventPrecondition(onClickPrecondition);
146:
147: if (viewModel.isOnClickEventRegistered()) {
148: JspUtil.writeEventAttributes(out, event);
149: JspWidgetCallUtil.writeSubmitScriptForEvent(out, "onclick");
150: }
151:
152: return viewModel.isOnClickEventRegistered();
153: }
154: }
|