KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > event > Button


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.event;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.riotfamily.common.markup.Html;
32 import org.riotfamily.common.markup.TagWriter;
33 import org.riotfamily.common.util.FormatUtils;
34 import org.riotfamily.forms.AbstractEditorBase;
35 import org.riotfamily.forms.MessageUtils;
36 import org.riotfamily.forms.request.FormRequest;
37
38
39 /**
40  * A button widget.
41  */

42 public class Button extends AbstractEditorBase
43         implements JavaScriptEventAdapter {
44
45     private List JavaDoc listeners;
46     
47     private String JavaDoc labelKey;
48     
49     private String JavaDoc label;
50     
51     private String JavaDoc cssClass;
52     
53     private boolean submit = false;
54     
55     private String JavaDoc partitialSubmit;
56     
57     private boolean clicked;
58     
59     private int tabIndex;
60
61     public String JavaDoc getLabelKey() {
62         return labelKey;
63     }
64     
65     public void setLabelKey(String JavaDoc labelKey) {
66         this.labelKey = labelKey;
67     }
68     
69     public String JavaDoc getLabel() {
70         if (label == null) {
71             if (labelKey != null) {
72                 label = MessageUtils.getMessage(this, labelKey);
73             }
74             else {
75                 label = "Submit";
76             }
77         }
78         return label;
79     }
80     
81     public void setLabel(String JavaDoc label) {
82         this.label = label;
83     }
84     
85     public String JavaDoc getCssClass() {
86         if (cssClass == null) {
87             if (labelKey != null) {
88                 int i = labelKey.lastIndexOf('.');
89                 String JavaDoc s = i != -1 ? labelKey.substring(i + 1) : labelKey;
90                 cssClass = "button button-" + FormatUtils.toCssClass(s);
91             }
92             else {
93                 cssClass = "button button-" + FormatUtils.toCssClass(getLabel());
94             }
95         }
96         return cssClass;
97     }
98
99     public void setCssClass(String JavaDoc cssClass) {
100         this.cssClass = cssClass;
101     }
102
103     public void setSubmit(boolean submit) {
104         this.submit = submit;
105     }
106     
107     public void setPartitialSubmit(String JavaDoc partitialSubmit) {
108         this.partitialSubmit = partitialSubmit;
109         this.submit = partitialSubmit != null;
110     }
111     
112     public void setTabIndex(int tabIndex) {
113         this.tabIndex = tabIndex;
114     }
115
116     public void addClickListener(ClickListener listener) {
117         if (listeners == null) {
118             listeners = new ArrayList JavaDoc();
119         }
120         listeners.add(listener);
121     }
122     
123     public void processRequest(FormRequest request) {
124         String JavaDoc value = request.getParameter(getParamName());
125         clicked = value != null;
126         if (clicked) {
127             onClick();
128         }
129     }
130     
131     public boolean isClicked() {
132         return this.clicked;
133     }
134
135     public void renderInternal(PrintWriter JavaDoc writer) {
136         TagWriter tag = new TagWriter(writer);
137         tag.startEmpty(Html.INPUT)
138                 .attribute(Html.INPUT_TYPE, "submit")
139                 .attribute(Html.COMMON_CLASS, getCssClass())
140                 .attribute(Html.COMMON_ID, getId())
141                 .attribute(Html.COMMON_TABINDEX, tabIndex)
142                 .attribute(Html.INPUT_NAME, getParamName())
143                 .attribute(Html.INPUT_VALUE, getLabel());
144         
145         if (partitialSubmit != null) {
146             tag.attribute(Html.COMMON_ONCLICK, "submitElement('" +
147                     partitialSubmit + "', this); return false;");
148         }
149         tag.end();
150     }
151     
152     protected void onClick() {
153         log.debug("Button " + getId() + " clicked.");
154         fireClickEvent();
155     }
156     
157     protected void fireClickEvent() {
158         if (listeners != null) {
159             ClickEvent event = new ClickEvent(this);
160             Iterator JavaDoc it = listeners.iterator();
161             while (it.hasNext()) {
162                 ClickListener listener = (ClickListener) it.next();
163                 listener.clicked(event);
164             }
165         }
166     }
167
168     public int getEventTypes() {
169         if (!submit && listeners != null) {
170             return JavaScriptEvent.ON_CLICK;
171         }
172         return 0;
173     }
174     
175     public void handleJavaScriptEvent(JavaScriptEvent event) {
176         if (event.getType() == JavaScriptEvent.ON_CLICK) {
177             onClick();
178         }
179     }
180
181 }
182
Popular Tags