KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.forms.request.FormRequest;
34
35
36 /**
37  * Abstract superclass for elements that consist of several child elements.
38  * Calls to <code>processRequest()</code> and <code>render()</code> are
39  * automatically delegated to the components. Addionally the components are
40  * initialized, i.e. <code>setParent()</code>, <code>setForm()</code> and
41  * <code>Form.registerElement()</code> are called.
42  */

43 public abstract class CompositeElement extends AbstractEditorBase {
44
45     private List JavaDoc components = new ArrayList JavaDoc();
46     
47     private boolean surroundBySpan;
48     
49     /**
50      * Empty default constructor.
51      */

52     public CompositeElement() {
53     }
54
55     
56     protected List JavaDoc getComponents() {
57         return components;
58     }
59
60     /**
61      * Adds the given element to the list of components. If a form as already
62      * been set {@link #initComponent(Element)} is
63      * invoked, otherwise initialization is deferred until
64      * {@link #setForm(Form)} is called.
65      *
66      * @param element the element to add
67      */

68     protected void addComponent(Element element) {
69         components.add(element);
70         element.setParent(this);
71         if (getForm() != null) {
72             initComponent(element);
73         }
74     }
75
76     /**
77      * Removes the given component. Note that the element is
78      * not unregistered from the form.
79      */

80     protected void removeComponent(Element element) {
81         components.remove(element);
82     }
83
84     public boolean isEmpty() {
85         return components.isEmpty();
86     }
87     
88     /**
89      * Invokes <code>initComponent(Element)</code> on all components and
90      * finally calls <code>initCompositeElement()</code>.
91      */

92     protected final void afterFormSet() {
93         Iterator JavaDoc it = components.iterator();
94         while (it.hasNext()) {
95             Element element = (Element) it.next();
96             initComponent(element);
97         }
98         initCompositeElement();
99     }
100     
101     /**
102      * Subclasses may override this method to perform initialization tasks.
103      * A reference to the form will be set at this point and all components
104      * will be initialized.
105      *
106      *The default implementation does nothing.
107      */

108     protected void initCompositeElement() {
109     }
110
111     /**
112      * Calls <code>processRequestInternal()</code> and afterwards
113      * <code>processRequestComponents()</code> to process the components.
114      */

115     public void processRequest(FormRequest request) {
116         processRequestInternal(request);
117         processRequestCompontents(request);
118         revalidate();
119     }
120
121     protected final void revalidate() {
122         ErrorUtils.removeErrors(this);
123         validate();
124     }
125     
126     protected void validate() {
127     }
128     
129     /**
130      * Processes the request for all the components
131      */

132     protected final void processRequestCompontents(FormRequest request) {
133         // Temporary list to allow concurrent modification
134
List JavaDoc tempList = new ArrayList JavaDoc(components);
135         Iterator JavaDoc it = tempList.iterator();
136         while (it.hasNext()) {
137             Element element = (Element) it.next();
138             log.debug("Processing component: " + element);
139             element.processRequest(request);
140         }
141     }
142     
143     /**
144      * Sets a reference to the form and registers the element by calling
145      * {@link Form#registerElement(Element)}.
146      *
147      * @throws IllegalStateException if form is null
148      */

149     protected void initComponent(Element element) {
150         if (getForm() == null) {
151             throw new IllegalStateException JavaDoc("Form not set");
152         }
153         getForm().registerElement(element);
154     }
155
156     /**
157      * Called before processRequest() is invoked on the contained elements.
158      * Subclasses can override this method to perform custom processing. The
159      * default implementation does nothing.
160      */

161     protected void processRequestInternal(FormRequest request) {
162     }
163
164     protected boolean isSurroundBySpan() {
165         return this.surroundBySpan;
166     }
167
168     protected void setSurroundBySpan(boolean surroundBySpan) {
169         this.surroundBySpan = surroundBySpan;
170     }
171
172     protected void renderInternal(PrintWriter JavaDoc writer) {
173         if (surroundBySpan) {
174             TagWriter spanTag = new TagWriter(writer);
175             spanTag.start(Html.SPAN).attribute(Html.COMMON_ID, getId()).body();
176             renderComponents(writer);
177             spanTag.end();
178         }
179         else {
180             renderComponents(writer);
181         }
182     }
183     
184     protected void renderComponents(PrintWriter JavaDoc writer) {
185         Iterator JavaDoc it = components.iterator();
186         while (it.hasNext()) {
187             Element element = (Element) it.next();
188             log.debug("Rendering component " + element);
189             element.render(writer);
190         }
191     }
192     
193     /**
194      * Delegates the call to the first component.
195      */

196     public void focus() {
197         if (!components.isEmpty()) {
198             ((Element) components.get(0)).focus();
199         }
200     }
201     
202     /**
203      * Helper method to check for composite elements in templates.
204      * Always returns <code>true</code>
205      */

206     public boolean isCompositeElement() {
207         return true;
208     }
209 }
Popular Tags