KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.apache.cocoon.woody.Constants;
24 import org.apache.cocoon.woody.FormContext;
25 import org.apache.cocoon.woody.event.WidgetEvent;
26 import org.apache.cocoon.woody.validation.WidgetValidator;
27 import org.apache.cocoon.xml.AttributesImpl;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * Abstract base class for Widget implementations. Provides functionality
33  * common to many widgets.
34  *
35  * @version $Id: AbstractWidget.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public abstract class AbstractWidget implements Widget {
38     private String JavaDoc location;
39     private Widget parent;
40     private Form form;
41     protected AbstractWidgetDefinition definition;
42     
43     private List JavaDoc validators;
44
45     /**
46      * Sets the definition of this widget.
47      */

48     protected void setDefinition(AbstractWidgetDefinition definition) {
49         this.definition = definition;
50     }
51
52     /**
53      * Gets the id of this widget.
54      */

55     public String JavaDoc getId() {
56         return definition.getId();
57     }
58
59     /**
60      * Sets the source location of this widget.
61      */

62     protected void setLocation(String JavaDoc location) {
63         this.location = location;
64     }
65
66     /**
67      * Gets the source location of this widget.
68      */

69     public String JavaDoc getLocation() {
70         return this.location;
71     }
72
73     public Widget getParent() {
74         return parent;
75     }
76
77     public void setParent(Widget widget) {
78         this.parent = widget;
79     }
80
81     public Form getForm() {
82         if (this.form == null) {
83             if (parent == null) {
84                 this.form = (Form)this;
85             } else {
86                 this.form = parent.getForm();
87             }
88         }
89         return this.form;
90     }
91
92     public String JavaDoc getNamespace() {
93         if (getParent() != null && getParent().getNamespace().length() > 0) {
94             return getParent().getNamespace() + "." + getId();
95         } else {
96             return getId();
97         }
98     }
99
100     public String JavaDoc getFullyQualifiedId() {
101         if (parent != null) {
102             String JavaDoc namespace = parent.getNamespace();
103             if (namespace.length() > 0) {
104                 return namespace + "." + getId();
105             }
106         }
107         return getId();
108     }
109
110     public Object JavaDoc getValue() {
111         return null;
112     }
113
114     public void setValue(Object JavaDoc object) {
115         throw new RuntimeException JavaDoc("Cannot set the value of widget " + getFullyQualifiedId());
116     }
117
118     public boolean isRequired() {
119         return false;
120     }
121
122     public Widget getWidget(String JavaDoc id) {
123         return null;
124     }
125     
126     public void broadcastEvent(WidgetEvent event) {
127         throw new UnsupportedOperationException JavaDoc("Widget " + this.getFullyQualifiedId() + " doesn't handle events.");
128     }
129     
130     /**
131      * Add a validator to this widget instance.
132      *
133      * @param validator
134      */

135     public void addValidator(WidgetValidator validator) {
136         if (this.validators == null) {
137             this.validators = new ArrayList JavaDoc();
138         }
139         
140         this.validators.add(validator);
141     }
142     
143     /**
144      * Remove a validator from this widget instance
145      *
146      * @param validator
147      * @return <code>true</code> if the validator was found.
148      */

149     public boolean removeValidator(WidgetValidator validator) {
150         return (this.validators == null)? false : this.validators.remove(validator);
151     }
152     
153     public boolean validate(FormContext context) {
154         // Test validators from the widget definition
155
if (!this.definition.validate(this, context)) {
156             // Failed
157
return false;
158         } else {
159             // Definition sussessful, test local validators
160
if (this.validators == null) {
161                 // No local validators
162
return true;
163             } else {
164                 // Iterate on local validators
165
Iterator JavaDoc iter = this.validators.iterator();
166                 while(iter.hasNext()) {
167                     WidgetValidator validator = (WidgetValidator)iter.next();
168                     if (!validator.validate(this, context)) {
169                         return false;
170                     }
171                 }
172                 // All local iterators successful
173
return true;
174             }
175         }
176     }
177     
178     public void generateLabel(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
179         if (definition != null) {
180             definition.generateDisplayData("label", contentHandler);
181         }
182     }
183
184     public void generateItemSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
185         // Do nothing
186
}
187
188     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale, String JavaDoc element, WidgetDefinition definition)
189     throws SAXException JavaDoc {
190         AttributesImpl attrs = new AttributesImpl();
191         attrs.addCDATAAttribute("id", getFullyQualifiedId());
192         contentHandler.startElement(Constants.WI_NS, element, Constants.WI_PREFIX_COLON + element, attrs);
193         generateItemSaxFragment(contentHandler, locale);
194         contentHandler.endElement(Constants.WI_NS, element, Constants.WI_PREFIX_COLON + element);
195     }
196 }
197
Popular Tags