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.Iterator;
020: import java.util.Locale;
021:
022: import org.apache.cocoon.forms.FormContext;
023: import org.apache.cocoon.forms.validation.ValidationError;
024: import org.xml.sax.ContentHandler;
025: import org.xml.sax.SAXException;
026:
027: /**
028: * A general-purpose abstract Widget which can hold zero or more widgets.
029: *
030: * @version $Id: AbstractContainerWidget.java 462520 2006-10-10 19:39:14Z vgritsenko $
031: */
032: public abstract class AbstractContainerWidget extends AbstractWidget
033: implements ContainerWidget {
034:
035: /**
036: * List of contained widgets.
037: */
038: protected WidgetList widgets;
039:
040: /**
041: * validation errors on container widgets
042: */
043: protected ValidationError validationError;
044:
045: /**
046: * Constructs AbstractContainerWidget
047: */
048: public AbstractContainerWidget(
049: AbstractContainerDefinition definition) {
050: super (definition);
051: widgets = new WidgetList();
052: }
053:
054: /**
055: * Called after widget's environment has been setup,
056: * to allow for any contextual initalization such as
057: * looking up case widgets for union widgets.
058: */
059: public void initialize() {
060: Iterator it = this .getChildren();
061: while (it.hasNext()) {
062: ((Widget) it.next()).initialize();
063: }
064:
065: super .initialize();
066: }
067:
068: public void addChild(Widget widget) {
069: // order is important
070: widgets.addWidget(widget);
071: widget.setParent(this );
072: }
073:
074: public boolean hasChild(String id) {
075: return widgets.hasWidget(id);
076: }
077:
078: public Widget getChild(String id) {
079: return widgets.getWidget(id);
080: }
081:
082: public Iterator getChildren() {
083: return widgets.iterator();
084: }
085:
086: public int getSize() {
087: return widgets.getWidgetList().size();
088: }
089:
090: /**
091: * Delegates the readFromRequest() down to the contained child-widgets.
092: *
093: * When overriding one should call <code>super.readFromRequest()</code>
094: * to allow child-widgets to process the request.
095: *
096: * Overide only to add possible request-reading statements on the containment level.
097: *
098: * @param formContext to be passed to the {@link Widget#readFromRequest(FormContext)}
099: * of the contained widgets.
100: */
101: public void readFromRequest(FormContext formContext) {
102: if (getCombinedState().isAcceptingInputs()) {
103: widgets.readFromRequest(formContext);
104: }
105: }
106:
107: /**
108: * Delegates the <code>validate()</code> down to the contained child-widgets,
109: * and validates the extra rules on this containment level regardless of
110: * children widget's validities.
111: *
112: * <p>When overriding one should call <code>super.validate()</code> as the first
113: * statement to keep in sync with this behaviour.</p>
114: *
115: * @return <code>true</code> only if all contained widgets are valid and the
116: * extra validation rules on this containment level are ok.
117: */
118: public boolean validate() {
119: if (!getCombinedState().isValidatingValues()) {
120: this .wasValid = true;
121: return true;
122: }
123: // Validate children first, then always validate self. Return combined result.
124: final boolean valid = widgets.validate();
125: this .wasValid = super .validate() && valid;
126: return this .wasValid;
127: }
128:
129: /**
130: * Subclass container widgets can call this super.generateItemSaxFragment(..)
131: * to just insert the child-widget content wrapped in a @lt;fi:widgets@gt;
132: *
133: * @param contentHandler where the SAX is sent to via {@link Widget#generateSaxFragment(ContentHandler, Locale)}
134: * @param locale
135: * @throws SAXException
136: */
137: public void generateItemSaxFragment(ContentHandler contentHandler,
138: Locale locale) throws SAXException {
139: if (getCombinedState().isDisplayingValues()) {
140: widgets.generateSaxFragment(contentHandler, locale);
141: }
142: }
143: }
|