KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import org.apache.cocoon.forms.FormContext;
22 import org.apache.cocoon.forms.validation.ValidationError;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 /**
27  * A general-purpose abstract Widget which can hold zero or more widgets.
28  *
29  * @version $Id: AbstractContainerWidget.java 293356 2005-10-03 14:10:45Z sylvain $
30  */

31 public abstract class AbstractContainerWidget extends AbstractWidget implements ContainerWidget {
32
33     /**
34      * List of contained widgets.
35      */

36     protected WidgetList widgets;
37     
38     /**
39      * validation errors on container widgets
40      */

41     protected ValidationError validationError;
42
43     /**
44      * Constructs AbstractContainerWidget
45      */

46     public AbstractContainerWidget(AbstractContainerDefinition definition) {
47         super(definition);
48         widgets = new WidgetList();
49     }
50
51     /**
52      * Called after widget's environment has been setup,
53      * to allow for any contextual initalization such as
54      * looking up case widgets for union widgets.
55      */

56     public void initialize() {
57         Iterator JavaDoc it = this.getChildren();
58         while(it.hasNext()) {
59           ((Widget)it.next()).initialize();
60         }
61         
62         super.initialize();
63     }
64
65     public void addChild(Widget widget) {
66         // order is important
67
widgets.addWidget(widget);
68         widget.setParent(this);
69     }
70
71     public boolean hasChild(String JavaDoc id) {
72         return widgets.hasWidget(id);
73     }
74
75     public Widget getChild(String JavaDoc id) {
76         return widgets.getWidget(id);
77     }
78
79     public Iterator JavaDoc getChildren() {
80         return widgets.iterator();
81     }
82     
83     public int getSize() {
84         return widgets.getWidgetList().size();
85     }
86     
87     /**
88      * Delegates the readFromRequest() down to the contained child-widgets.
89      *
90      * When overriding one should call <code>super.readFromRequest()</code>
91      * to allow child-widgets to process the request.
92      *
93      * Overide only to add possible request-reading statements on the containment level.
94      *
95      * @param formContext to be passed to the {@link Widget#readFromRequest(FormContext)}
96      * of the contained widgets.
97      */

98     public void readFromRequest(FormContext formContext) {
99         if (getCombinedState().isAcceptingInputs()) {
100             widgets.readFromRequest(formContext);
101         }
102     }
103
104     /**
105      * Delegates the <code>validate()</code> down to the contained child-widgets,
106      * and validates the extra rules on this containment level regardless of
107      * children widget's validities.
108      *
109      * <p>When overriding one should call <code>super.validate()</code> as the first
110      * statement to keep in sync with this behaviour.</p>
111      *
112      * @return <code>true</code> only if all contained widgets are valid and the
113      * extra validation rules on this containment level are ok.
114      */

115     public boolean validate() {
116         if (!getCombinedState().isValidatingValues()) {
117             this.wasValid = true;
118             return true;
119         }
120         // Validate children first, then always validate self. Return combined result.
121
final boolean valid = widgets.validate();
122         this.wasValid = super.validate() && valid;
123         return this.wasValid;
124     }
125
126     /**
127      * Subclass container widgets can call this super.generateItemSaxFragment(..)
128      * to just insert the child-widget content wrapped in a @lt;fi:widgets@gt;
129      *
130      * @param contentHandler where the SAX is sent to via {@link Widget#generateSaxFragment(ContentHandler, Locale)}
131      * @param locale
132      * @throws SAXException
133      */

134     public void generateItemSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
135         if (getCombinedState().isDisplayingValues()) {
136             widgets.generateSaxFragment(contentHandler, locale);
137         }
138     }
139 }
140
Popular Tags