KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > AbstractElement


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;
25
26 import java.io.PrintWriter JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.riotfamily.forms.request.FormRequest;
31
32
33 /**
34  * Convinient superclass for element implementations.
35  */

36 public abstract class AbstractElement implements Element {
37
38     protected Log log = LogFactory.getLog(getClass());
39     
40     private Form form;
41
42     private FormContext formContext;
43     
44     private Element parent;
45
46     private String JavaDoc label;
47     
48     private String JavaDoc hint;
49     
50     private String JavaDoc id;
51     
52     private String JavaDoc styleClass;
53     
54     private boolean required;
55     
56     private boolean enabled = true;
57     
58     public String JavaDoc getId() {
59         return id;
60     }
61
62     public void setId(String JavaDoc id) {
63         this.id = id;
64     }
65     
66     public String JavaDoc getStyleClass() {
67         return styleClass;
68     }
69     
70     public void setStyleClass(String JavaDoc styleClass) {
71         this.styleClass = styleClass;
72     }
73         
74     public Form getForm() {
75         return form;
76     }
77     
78     public final void setForm(Form form) {
79         this.form = form;
80         afterFormSet();
81     }
82
83     protected void afterFormSet() {
84     }
85
86     public FormContext getFormContext() {
87         return formContext;
88     }
89     
90     public final void setFormContext(FormContext formContext) {
91         this.formContext = formContext;
92         afterFormContextSet();
93     }
94     
95     protected void afterFormContextSet() {
96     }
97     
98     public Element getParent() {
99         return parent;
100     }
101
102     public void setParent(Element parent) {
103         this.parent = parent;
104     }
105     
106     public String JavaDoc getLabel() {
107         return label;
108     }
109
110     public void setLabel(String JavaDoc label) {
111         this.label = label;
112     }
113     
114     public String JavaDoc getHint() {
115         return hint;
116     }
117     
118     public void setHint(String JavaDoc hint) {
119         this.hint = hint;
120     }
121         
122     public void focus() {
123         if (form == null) {
124             throw new IllegalStateException JavaDoc(
125                     "Element must be registered before calling focus()");
126         }
127         form.requestFocus(this);
128     }
129
130     public final void render() {
131         if (form == null) {
132             throw new IllegalStateException JavaDoc("A form must be set!");
133         }
134         PrintWriter JavaDoc writer = getFormContext().getWriter();
135         if (writer == null) {
136             throw new IllegalStateException JavaDoc("A writer must be set!");
137         }
138         render(writer);
139     }
140
141     public void render(PrintWriter JavaDoc writer) {
142         renderInternal(writer);
143         form.elementRendered(this);
144     }
145     
146     protected abstract void renderInternal(PrintWriter JavaDoc writer);
147
148     /**
149      * Subclasses may override this method to change their internal state
150      * according the given request.
151      */

152     public void processRequest(FormRequest request) {
153     }
154     
155     /**
156      * Returns <code>true</code>, if the element as well as its parent is
157      * enabled.
158      */

159     public final boolean isEnabled() {
160         if (enabled && (parent != null)) {
161             return parent.isEnabled();
162         }
163         return enabled;
164     }
165     
166     /**
167      * Enables (or disables) the element. The state of nested elements
168      * will be implicitly affected, since {@link #isEnabled()} takes the state
169      * of its parent element into account.
170      */

171     public final void setEnabled(boolean enabled) {
172         this.enabled = enabled;
173         if (getFormListener() != null) {
174             getFormListener().elementEnabled(this);
175         }
176     }
177     
178     /**
179      * Returns whether the element is mandatory and must be filled out by
180      * the user.
181      */

182     public boolean isRequired() {
183         return required;
184     }
185     
186     /**
187      * Sets whether the element is required.
188      */

189     public void setRequired(boolean required) {
190         this.required = required;
191     }
192     
193     public boolean isCompositeElement() {
194         return false;
195     }
196         
197     protected FormListener getFormListener() {
198         if (form != null) {
199             return form.getFormListener();
200         }
201         return null;
202     }
203 }
204
Popular Tags