KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > AbstractWidgetDefinition


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.cocoon.forms.FormsConstants;
25 import org.apache.cocoon.forms.event.CreateEvent;
26 import org.apache.cocoon.forms.event.CreateListener;
27 import org.apache.cocoon.forms.event.WidgetEventMulticaster;
28 import org.apache.cocoon.forms.validation.WidgetValidator;
29 import org.apache.cocoon.util.location.Location;
30 import org.apache.cocoon.xml.XMLUtils;
31 import org.apache.excalibur.xml.sax.XMLizable;
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * Provides functionality that is common across many WidgetDefinition implementations.
37  *
38  * @version $Id: AbstractWidgetDefinition.java 326838 2005-10-20 06:26:53Z sylvain $
39  */

40 public abstract class AbstractWidgetDefinition implements WidgetDefinition {
41     private FormDefinition formDefinition;
42     protected WidgetDefinition parent;
43
44     //TODO consider final on these
45
private Location location = Location.UNKNOWN;
46     private String JavaDoc id;
47     /** the definition is mutable when being built */
48     private boolean mutable = true;
49     /** The initial map of attributes (can be null) */
50     private Map JavaDoc attributes;
51     private Map JavaDoc displayData;
52     private List JavaDoc validators;
53     private WidgetState state = WidgetState.ACTIVE;
54
55     protected CreateListener createListener;
56
57     public FormDefinition getFormDefinition() {
58         if (this.formDefinition == null) {
59             if (this instanceof FormDefinition) {
60                 this.formDefinition = (FormDefinition)this;
61             } else {
62                 this.formDefinition = this.parent.getFormDefinition();
63             }
64         }
65         return this.formDefinition;
66     }
67     
68     /**
69      * initialize this definition with the other, sort of like a copy constructor
70      */

71     public void initializeFrom(WidgetDefinition definition) throws Exception JavaDoc {
72         if(definition instanceof AbstractWidgetDefinition) {
73             AbstractWidgetDefinition other = (AbstractWidgetDefinition)definition;
74             
75             this.state = other.state;
76             this.createListener = other.createListener; // this works, we don't really remove listeners, right?
77

78             this.validators = new ArrayList JavaDoc();
79             if(other.validators!=null) {
80                 for(int i=0; i<other.validators.size(); i++)
81                     this.validators.add(other.validators.get(i));
82             }
83             
84             if(other.attributes!=null) {
85                 if(attributes==null)
86                     attributes = new HashMap JavaDoc();
87                 copyMap(attributes,other.attributes);
88             }
89             if(other.displayData!=null) {
90                 if(displayData==null)
91                     displayData = new HashMap JavaDoc();
92                 copyMap(displayData,other.displayData);
93             }
94         } else {
95             throw new Exception JavaDoc("Definition to inherit from is not of the right type! (at "+getLocation()+")");
96         }
97     }
98     
99     /**
100      * helper to copy a map
101      *
102      * @param dest destination map
103      * @param src source map
104      */

105     protected void copyMap(Map JavaDoc dest, Map JavaDoc src) {
106         dest.clear();
107         Iterator JavaDoc it = src.entrySet().iterator();
108         while(it.hasNext()) {
109             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
110             dest.put(entry.getKey(),entry.getValue());
111         }
112     }
113     
114     /**
115      * Checks if this definition is complete or not.
116      */

117     public void checkCompleteness() throws IncompletenessException
118     {
119         // FormDefinition is the only one allowed not to have an ID
120
if( (id==null || "".equals(id) && !(this instanceof FormDefinition) ))
121             throw new IncompletenessException("Widget found without an ID! "+this,this);
122         
123         
124         // TODO: don't know what else to check now
125
}
126     
127     
128     /**
129      * Locks this definition so that it becomes immutable.
130      */

131     public void makeImmutable() {
132         this.mutable = false;
133     }
134     
135     /**
136      * Check that this definition is mutable, i.e. is in setup phase. If not, throw an exception.
137      */

138     protected void checkMutable() {
139         if (!this.mutable) {
140             throw new IllegalStateException JavaDoc("Attempt to modify an immutable WidgetDefinition");
141         }
142     }
143
144     /**
145      * Sets the parent of this definition
146      */

147     public void setParent(WidgetDefinition definition) {
148         //FIXME(SW) calling checkMutable() here is not possible as NewDefinition.resolve() does some weird
149
//reorganization of the definition tree
150
this.parent = definition;
151     }
152
153     /**
154      * Gets the parent of this definition.
155      * This method returns null for the root definition.
156      */

157     public WidgetDefinition getParent() {
158         return this.parent;
159     }
160
161     public WidgetState getState() {
162         return this.state;
163     }
164
165     public void setState(WidgetState state) {
166         checkMutable();
167         this.state = state;
168     }
169
170     public void setLocation(Location location) {
171         checkMutable();
172         this.location = location;
173     }
174
175     public Location getLocation() {
176         return location;
177     }
178
179     public String JavaDoc getId() {
180         return id;
181     }
182
183     protected void setId(String JavaDoc id) {
184         checkMutable();
185         this.id = id;
186     }
187
188     protected void setAttributes(Map JavaDoc attributes) {
189         checkMutable();
190         //this.attributes = attributes;
191
if(this.attributes==null) {
192             this.attributes = attributes;
193             return;
194         }
195         if(attributes==null)
196             return;
197         
198         // merge attribute lists
199
Iterator JavaDoc entries = attributes.entrySet().iterator();
200         while(entries.hasNext()) {
201             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
202             Object JavaDoc key = entry.getKey();
203             Object JavaDoc value = entry.getValue();
204             this.attributes.put(key,value);
205         }
206     }
207     
208     public Object JavaDoc getAttribute(String JavaDoc name) {
209         if (this.attributes != null) {
210             return this.attributes.get(name);
211         }
212         return null;
213     }
214
215     protected void addCreateListener(CreateListener listener) {
216         checkMutable();
217         // Event listener daisy-chain
218
this.createListener = WidgetEventMulticaster.add(this.createListener, listener);
219     }
220     
221     public void widgetCreated(Widget widget) {
222         if (this.createListener != null) {
223             widget.getForm().addWidgetEvent(new CreateEvent(widget));
224         }
225     }
226
227     public void fireCreateEvent(CreateEvent event) {
228         // Check that this widget was created by the current definition
229
if (event.getSourceWidget().getDefinition() != this) {
230             throw new IllegalArgumentException JavaDoc("Widget was not created by this definition");
231         }
232         if (this.createListener != null) {
233             this.createListener.widgetCreated(event);
234         }
235     }
236
237     public void generateLabel(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
238         generateDisplayData("label", contentHandler);
239     }
240
241     /**
242      * Sets the various display data for this widget. This includes the label, hint and help.
243      * They must all be objects implementing the XMLizable interface. This approach
244      * allows to have mixed content in these data.
245      *
246      * @param displayData an association of {name, sax fragment}
247      */

248     public void setDisplayData(Map JavaDoc displayData) {
249         checkMutable();
250         //this.displayData = displayData;
251

252         if(this.displayData==null) {
253             this.displayData = displayData;
254             return;
255         }
256         if(displayData==null)
257             return;
258         
259         // merge displayData lists
260
Iterator JavaDoc entries = displayData.entrySet().iterator();
261         while(entries.hasNext()) {
262             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
263             Object JavaDoc key = entry.getKey();
264             Object JavaDoc value = entry.getValue();
265             if(value!=null || !this.displayData.containsKey(key))
266                 this.displayData.put(key,value);
267         }
268     }
269
270     public void addValidator(WidgetValidator validator) {
271         checkMutable();
272         if (this.validators == null) {
273             this.validators = new ArrayList JavaDoc();
274         }
275
276         this.validators.add(validator);
277     }
278
279     public void generateDisplayData(String JavaDoc name, ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
280         Object JavaDoc data = this.displayData.get(name);
281         if (data != null) {
282             ((XMLizable)data).toSAX(contentHandler);
283         } else if (!this.displayData.containsKey(name)) {
284             throw new IllegalArgumentException JavaDoc("Unknown display data name '" + name + "'");
285         }
286     }
287
288     public void generateDisplayData(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
289         // Output all non-null display data
290
Iterator JavaDoc iter = this.displayData.entrySet().iterator();
291         while (iter.hasNext()) {
292             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
293             if (entry.getValue() != null) {
294                 String JavaDoc name = (String JavaDoc)entry.getKey();
295
296                 // Enclose the data into a "wi:{name}" element
297
contentHandler.startElement(FormsConstants.INSTANCE_NS, name, FormsConstants.INSTANCE_PREFIX_COLON + name, XMLUtils.EMPTY_ATTRIBUTES);
298
299                 ((XMLizable)entry.getValue()).toSAX(contentHandler);
300
301                 contentHandler.endElement(FormsConstants.INSTANCE_NS, name, FormsConstants.INSTANCE_PREFIX_COLON + name);
302             }
303         }
304     }
305
306     public boolean validate(Widget widget) {
307         if (this.validators == null) {
308             // No validators
309
return true;
310
311         }
312         Iterator JavaDoc iter = this.validators.iterator();
313         while(iter.hasNext()) {
314             WidgetValidator validator = (WidgetValidator)iter.next();
315             if (! validator.validate(widget)) {
316                 // Stop at the first validator that fails
317
return false;
318             }
319         }
320         // All validators were sucessful
321
return true;
322     }
323 }
324
Popular Tags