001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.event;
025:
026: import java.io.PrintWriter;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import org.riotfamily.common.markup.Html;
032: import org.riotfamily.common.markup.TagWriter;
033: import org.riotfamily.common.util.FormatUtils;
034: import org.riotfamily.forms.AbstractEditorBase;
035: import org.riotfamily.forms.MessageUtils;
036: import org.riotfamily.forms.request.FormRequest;
037:
038: /**
039: * A button widget.
040: */
041: public class Button extends AbstractEditorBase implements
042: JavaScriptEventAdapter {
043:
044: private List listeners;
045:
046: private String labelKey;
047:
048: private String label;
049:
050: private String cssClass;
051:
052: private boolean submit = false;
053:
054: private String partitialSubmit;
055:
056: private boolean clicked;
057:
058: private int tabIndex;
059:
060: public String getLabelKey() {
061: return labelKey;
062: }
063:
064: public void setLabelKey(String labelKey) {
065: this .labelKey = labelKey;
066: }
067:
068: public String getLabel() {
069: if (label == null) {
070: if (labelKey != null) {
071: label = MessageUtils.getMessage(this , labelKey);
072: } else {
073: label = "Submit";
074: }
075: }
076: return label;
077: }
078:
079: public void setLabel(String label) {
080: this .label = label;
081: }
082:
083: public String getCssClass() {
084: if (cssClass == null) {
085: if (labelKey != null) {
086: int i = labelKey.lastIndexOf('.');
087: String s = i != -1 ? labelKey.substring(i + 1)
088: : labelKey;
089: cssClass = "button button-" + FormatUtils.toCssClass(s);
090: } else {
091: cssClass = "button button-"
092: + FormatUtils.toCssClass(getLabel());
093: }
094: }
095: return cssClass;
096: }
097:
098: public void setCssClass(String cssClass) {
099: this .cssClass = cssClass;
100: }
101:
102: public void setSubmit(boolean submit) {
103: this .submit = submit;
104: }
105:
106: public void setPartitialSubmit(String partitialSubmit) {
107: this .partitialSubmit = partitialSubmit;
108: this .submit = partitialSubmit != null;
109: }
110:
111: public void setTabIndex(int tabIndex) {
112: this .tabIndex = tabIndex;
113: }
114:
115: public void addClickListener(ClickListener listener) {
116: if (listeners == null) {
117: listeners = new ArrayList();
118: }
119: listeners.add(listener);
120: }
121:
122: public void processRequest(FormRequest request) {
123: String value = request.getParameter(getParamName());
124: clicked = value != null;
125: if (clicked) {
126: onClick();
127: }
128: }
129:
130: public boolean isClicked() {
131: return this .clicked;
132: }
133:
134: public void renderInternal(PrintWriter writer) {
135: TagWriter tag = new TagWriter(writer);
136: tag.startEmpty(Html.INPUT).attribute(Html.INPUT_TYPE, "submit")
137: .attribute(Html.COMMON_CLASS, getCssClass()).attribute(
138: Html.COMMON_ID, getId()).attribute(
139: Html.COMMON_TABINDEX, tabIndex).attribute(
140: Html.INPUT_NAME, getParamName()).attribute(
141: Html.INPUT_VALUE, getLabel());
142:
143: if (partitialSubmit != null) {
144: tag.attribute(Html.COMMON_ONCLICK, "submitElement('"
145: + partitialSubmit + "', this); return false;");
146: }
147: tag.end();
148: }
149:
150: protected void onClick() {
151: log.debug("Button " + getId() + " clicked.");
152: fireClickEvent();
153: }
154:
155: protected void fireClickEvent() {
156: if (listeners != null) {
157: ClickEvent event = new ClickEvent(this );
158: Iterator it = listeners.iterator();
159: while (it.hasNext()) {
160: ClickListener listener = (ClickListener) it.next();
161: listener.clicked(event);
162: }
163: }
164: }
165:
166: public int getEventTypes() {
167: if (!submit && listeners != null) {
168: return JavaScriptEvent.ON_CLICK;
169: }
170: return 0;
171: }
172:
173: public void handleJavaScriptEvent(JavaScriptEvent event) {
174: if (event.getType() == JavaScriptEvent.ON_CLICK) {
175: onClick();
176: }
177: }
178:
179: }
|