KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.cocoon.forms.FormsConstants;
27 import org.apache.cocoon.forms.FormContext;
28 import org.apache.cocoon.xml.XMLUtils;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * Helper class for the implementation of widgets containing other widgets.
34  * This implements a type-aware List of Widgets that automatically can distribute
35  * the common Widget operations over the contained Widgets.
36  *
37  * @version $Id: WidgetList.java 326838 2005-10-20 06:26:53Z sylvain $
38  */

39 public class WidgetList {
40
41     private static final String JavaDoc WIDGETS_EL = "widgets";
42     
43     /**
44      * List of the contained widgets.
45      * This maintains the original order of the widgets to garantee order of
46      * validation and generation of SAXFragments
47      */

48     private List JavaDoc widgets;
49     
50     /**
51      * Map of the contained widgets using its id as the lookup key.
52      */

53     private Map JavaDoc widgetsById;
54
55
56     /**
57      * Constructs ContainerDelegate to store and jointly manage a list of
58      * contained widgets.
59      */

60     public WidgetList() {
61         widgets = new ArrayList JavaDoc();
62         widgetsById = new HashMap JavaDoc();
63     }
64
65     /**
66      * Get the (unmodifiable) list of widgets
67      *
68      * @return the widget list
69      */

70     public List JavaDoc getWidgetList() {
71         return Collections.unmodifiableList(this.widgets);
72     }
73     
74     /**
75      * Get the (unmodifiable) map of widgets
76      *
77      * @return the widget map
78      */

79     public Map JavaDoc getWidgetMap() {
80         return Collections.unmodifiableMap(this.widgetsById);
81     }
82
83     /**
84      * Adds a widget to the list of contained {@link Widget}'s
85      *
86      * @param widget
87      */

88     public void addWidget(Widget widget) {
89         widgets.add(widget);
90         widgetsById.put(widget.getId(), widget);
91     }
92     
93
94     /**
95      * Performs the {@link Widget#readFromRequest(FormContext)} on all the
96      * contained widgets.
97      *
98      * @param formContext to pass to the {@link Widget#readFromRequest(FormContext)}
99      *
100      * @see Widget#readFromRequest(FormContext)
101      */

102     public void readFromRequest(FormContext formContext) {
103         Iterator JavaDoc widgetIt = iterator();
104         while (widgetIt.hasNext()) {
105             Widget widget = (Widget)widgetIt.next();
106             widget.readFromRequest(formContext);
107         }
108     }
109
110     /**
111      * Validates all contained widgets and returns the combined result.
112      *
113      * @return <code>false</code> if at least one of the contained widgets is not valid.
114      *
115      * @see Widget#validate()
116      */

117     public boolean validate() {
118         boolean valid = true;
119         Iterator JavaDoc widgetIt = iterator();
120         while (widgetIt.hasNext()) {
121             Widget widget = (Widget)widgetIt.next();
122             valid = valid & widget.validate();
123         }
124         return valid;
125     }
126
127     /**
128      * Checks if a widget with the provided id is contained in the list.
129      *
130      * @param id of the widget to look for.
131      * @return true if the widget was found
132      */

133     public boolean hasWidget(String JavaDoc id) {
134         return widgetsById.containsKey(id);
135     }
136
137     /**
138      * Looks for a widget in this list by using the provided id as a lookup key.
139      *
140      * @param id of the widget to look for
141      * @return the found widget or <code>null</code> if it could not be found.
142      */

143     public Widget getWidget(String JavaDoc id) {
144         return (Widget)widgetsById.get(id);
145     }
146
147     /**
148      * @return an iterator over the contained {@link Widget}'s
149      */

150     public Iterator JavaDoc iterator() {
151         return widgets.iterator();
152     }
153
154     /**
155      * @return <code>false</code> if at least one of the contained widgets has no value.
156      */

157     public boolean widgetsHaveValues() {
158         Iterator JavaDoc widgetsIt = iterator();
159         while(widgetsIt.hasNext()) {
160             Widget widget = (Widget)widgetsIt.next();
161             if (widget.getValue() == null)
162                 return false;
163         }
164         return true;
165     }
166
167     /**
168      * Generates the SAXfragments of the contained widgets
169      *
170      * @param contentHandler
171      * @param locale
172      * @throws SAXException
173      *
174      * @see Widget#generateSaxFragment(ContentHandler, Locale)
175      */

176     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
177         contentHandler.startElement(FormsConstants.INSTANCE_NS, WIDGETS_EL, FormsConstants.INSTANCE_PREFIX_COLON + WIDGETS_EL, XMLUtils.EMPTY_ATTRIBUTES);
178         Iterator JavaDoc widgetIt = widgets.iterator();
179         while (widgetIt.hasNext()) {
180             Widget widget = (Widget)widgetIt.next();
181             widget.generateSaxFragment(contentHandler, locale);
182         }
183         contentHandler.endElement(FormsConstants.INSTANCE_NS, WIDGETS_EL, FormsConstants.INSTANCE_PREFIX_COLON + WIDGETS_EL);
184     }
185 }
186
187
Popular Tags