KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > formmodel > ContainerDelegate


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.woody.formmodel;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.cocoon.woody.Constants;
26 import org.apache.cocoon.woody.FormContext;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * Helper class for the implementation of widgets containing other widgets.
32  *
33  * @author Timothy Larson
34  * @version $Id: ContainerDelegate.java 30932 2004-07-29 17:35:38Z vgritsenko $
35  */

36 public class ContainerDelegate {
37 // private WidgetDefinition definition;
38
private List JavaDoc widgets;
39     private Map JavaDoc widgetsById;
40
41     private static final String JavaDoc WIDGETS_EL = "widgets";
42
43     public ContainerDelegate(WidgetDefinition definition) {
44         widgets = new ArrayList JavaDoc();
45         widgetsById = new HashMap JavaDoc();
46 // this.definition = definition;
47
}
48
49     public void addWidget(Widget widget) {
50         widgets.add(widget);
51         widgetsById.put(widget.getId(), widget);
52     }
53
54     public void readFromRequest(FormContext formContext) {
55         Iterator JavaDoc widgetIt = widgets.iterator();
56         while (widgetIt.hasNext()) {
57             Widget widget = (Widget)widgetIt.next();
58             widget.readFromRequest(formContext);
59         }
60     }
61
62     public boolean validate(FormContext formContext) {
63         boolean valid = true;
64         Iterator JavaDoc widgetIt = widgets.iterator();
65         while (widgetIt.hasNext()) {
66             Widget widget = (Widget)widgetIt.next();
67             valid = valid & widget.validate(formContext);
68         }
69         return valid;
70     }
71
72     public boolean hasWidget(String JavaDoc id) {
73         return widgetsById.containsKey(id);
74     }
75
76     public Widget getWidget(String JavaDoc id) {
77         return (Widget)widgetsById.get(id);
78     }
79
80     public Iterator JavaDoc iterator() {
81         return widgets.iterator();
82     }
83
84     /**
85      * Returns false if there is at least one field which has no value.
86      */

87     public boolean widgetsHaveValues() {
88         Iterator JavaDoc widgetsIt = widgets.iterator();
89         while(widgetsIt.hasNext()) {
90             Widget widget = (Widget)widgetsIt.next();
91             if (widget.getValue() == null)
92                 return false;
93         }
94         return true;
95     }
96
97     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
98         contentHandler.startElement(Constants.WI_NS, WIDGETS_EL, Constants.WI_PREFIX_COLON + WIDGETS_EL, Constants.EMPTY_ATTRS);
99         Iterator JavaDoc widgetIt = widgets.iterator();
100         while (widgetIt.hasNext()) {
101             Widget widget = (Widget)widgetIt.next();
102             widget.generateSaxFragment(contentHandler, locale);
103         }
104         contentHandler.endElement(Constants.WI_NS, WIDGETS_EL, Constants.WI_PREFIX_COLON + WIDGETS_EL);
105     }
106 }
107
108
Popular Tags