001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.formmodel;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import org.apache.cocoon.forms.FormsConstants;
028: import org.apache.cocoon.forms.FormContext;
029: import org.apache.cocoon.xml.XMLUtils;
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * Helper class for the implementation of widgets containing other widgets.
035: * This implements a type-aware List of Widgets that automatically can distribute
036: * the common Widget operations over the contained Widgets.
037: *
038: * @version $Id: WidgetList.java 449149 2006-09-23 03:58:05Z crossley $
039: */
040: public class WidgetList {
041:
042: private static final String WIDGETS_EL = "widgets";
043:
044: /**
045: * List of the contained widgets.
046: * This maintains the original order of the widgets to garantee order of
047: * validation and generation of SAXFragments
048: */
049: private List widgets;
050:
051: /**
052: * Map of the contained widgets using its id as the lookup key.
053: */
054: private Map widgetsById;
055:
056: /**
057: * Constructs ContainerDelegate to store and jointly manage a list of
058: * contained widgets.
059: */
060: public WidgetList() {
061: widgets = new ArrayList();
062: widgetsById = new HashMap();
063: }
064:
065: /**
066: * Get the (unmodifiable) list of widgets
067: *
068: * @return the widget list
069: */
070: public List getWidgetList() {
071: return Collections.unmodifiableList(this .widgets);
072: }
073:
074: /**
075: * Get the (unmodifiable) map of widgets
076: *
077: * @return the widget map
078: */
079: public Map getWidgetMap() {
080: return Collections.unmodifiableMap(this .widgetsById);
081: }
082:
083: /**
084: * Adds a widget to the list of contained {@link Widget}'s
085: *
086: * @param widget
087: */
088: public void addWidget(Widget widget) {
089: widgets.add(widget);
090: widgetsById.put(widget.getId(), widget);
091: }
092:
093: /**
094: * Performs the {@link Widget#readFromRequest(FormContext)} on all the
095: * contained widgets.
096: *
097: * @param formContext to pass to the {@link Widget#readFromRequest(FormContext)}
098: *
099: * @see Widget#readFromRequest(FormContext)
100: */
101: public void readFromRequest(FormContext formContext) {
102: Iterator widgetIt = iterator();
103: while (widgetIt.hasNext()) {
104: Widget widget = (Widget) widgetIt.next();
105: widget.readFromRequest(formContext);
106: }
107: }
108:
109: /**
110: * Validates all contained widgets and returns the combined result.
111: *
112: * @return <code>false</code> if at least one of the contained widgets is not valid.
113: *
114: * @see Widget#validate()
115: */
116: public boolean validate() {
117: boolean valid = true;
118: Iterator widgetIt = iterator();
119: while (widgetIt.hasNext()) {
120: Widget widget = (Widget) widgetIt.next();
121: valid = valid & widget.validate();
122: }
123: return valid;
124: }
125:
126: /**
127: * Checks if a widget with the provided id is contained in the list.
128: *
129: * @param id of the widget to look for.
130: * @return true if the widget was found
131: */
132: public boolean hasWidget(String id) {
133: return widgetsById.containsKey(id);
134: }
135:
136: /**
137: * Looks for a widget in this list by using the provided id as a lookup key.
138: *
139: * @param id of the widget to look for
140: * @return the found widget or <code>null</code> if it could not be found.
141: */
142: public Widget getWidget(String id) {
143: return (Widget) widgetsById.get(id);
144: }
145:
146: /**
147: * @return an iterator over the contained {@link Widget}'s
148: */
149: public Iterator iterator() {
150: return widgets.iterator();
151: }
152:
153: /**
154: * @return <code>false</code> if at least one of the contained widgets has no value.
155: */
156: public boolean widgetsHaveValues() {
157: Iterator widgetsIt = iterator();
158: while (widgetsIt.hasNext()) {
159: Widget widget = (Widget) widgetsIt.next();
160: if (widget.getValue() == null)
161: return false;
162: }
163: return true;
164: }
165:
166: /**
167: * Generates the SAXfragments of the contained widgets
168: *
169: * @param contentHandler
170: * @param locale
171: * @throws SAXException
172: *
173: * @see Widget#generateSaxFragment(ContentHandler, Locale)
174: */
175: public void generateSaxFragment(ContentHandler contentHandler,
176: Locale locale) throws SAXException {
177: contentHandler.startElement(FormsConstants.INSTANCE_NS,
178: WIDGETS_EL, FormsConstants.INSTANCE_PREFIX_COLON
179: + WIDGETS_EL, XMLUtils.EMPTY_ATTRIBUTES);
180: Iterator widgetIt = widgets.iterator();
181: while (widgetIt.hasNext()) {
182: Widget widget = (Widget) widgetIt.next();
183: widget.generateSaxFragment(contentHandler, locale);
184: }
185: contentHandler.endElement(FormsConstants.INSTANCE_NS,
186: WIDGETS_EL, FormsConstants.INSTANCE_PREFIX_COLON
187: + WIDGETS_EL);
188: }
189: }
|