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.woody.formmodel;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Locale;
024: import java.util.Map;
025:
026: import org.apache.cocoon.woody.Constants;
027: import org.apache.cocoon.woody.FormContext;
028: import org.xml.sax.ContentHandler;
029: import org.xml.sax.SAXException;
030:
031: /**
032: * Helper class for the implementation of widgets containing other widgets.
033: *
034: * @author Timothy Larson
035: * @version $Id: ContainerDelegate.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class ContainerDelegate {
038: // private WidgetDefinition definition;
039: private List widgets;
040: private Map widgetsById;
041:
042: private static final String WIDGETS_EL = "widgets";
043:
044: public ContainerDelegate(WidgetDefinition definition) {
045: widgets = new ArrayList();
046: widgetsById = new HashMap();
047: // this.definition = definition;
048: }
049:
050: public void addWidget(Widget widget) {
051: widgets.add(widget);
052: widgetsById.put(widget.getId(), widget);
053: }
054:
055: public void readFromRequest(FormContext formContext) {
056: Iterator widgetIt = widgets.iterator();
057: while (widgetIt.hasNext()) {
058: Widget widget = (Widget) widgetIt.next();
059: widget.readFromRequest(formContext);
060: }
061: }
062:
063: public boolean validate(FormContext formContext) {
064: boolean valid = true;
065: Iterator widgetIt = widgets.iterator();
066: while (widgetIt.hasNext()) {
067: Widget widget = (Widget) widgetIt.next();
068: valid = valid & widget.validate(formContext);
069: }
070: return valid;
071: }
072:
073: public boolean hasWidget(String id) {
074: return widgetsById.containsKey(id);
075: }
076:
077: public Widget getWidget(String id) {
078: return (Widget) widgetsById.get(id);
079: }
080:
081: public Iterator iterator() {
082: return widgets.iterator();
083: }
084:
085: /**
086: * Returns false if there is at least one field which has no value.
087: */
088: public boolean widgetsHaveValues() {
089: Iterator widgetsIt = widgets.iterator();
090: while (widgetsIt.hasNext()) {
091: Widget widget = (Widget) widgetsIt.next();
092: if (widget.getValue() == null)
093: return false;
094: }
095: return true;
096: }
097:
098: public void generateSaxFragment(ContentHandler contentHandler,
099: Locale locale) throws SAXException {
100: contentHandler.startElement(Constants.WI_NS, WIDGETS_EL,
101: Constants.WI_PREFIX_COLON + WIDGETS_EL,
102: Constants.EMPTY_ATTRS);
103: Iterator widgetIt = widgets.iterator();
104: while (widgetIt.hasNext()) {
105: Widget widget = (Widget) widgetIt.next();
106: widget.generateSaxFragment(contentHandler, locale);
107: }
108: contentHandler.endElement(Constants.WI_NS, WIDGETS_EL,
109: Constants.WI_PREFIX_COLON + WIDGETS_EL);
110: }
111: }
|