KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale JavaDoc;
19
20 import org.apache.cocoon.woody.FormContext;
21 import org.xml.sax.ContentHandler JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 /**
25  * A discriminated union that references a discriminant value in another
26  * widget and contains one of several cases (widgets). To have a case
27  * hold more than one widget or to use a different id for the case than
28  * for the widget id, just wrap the widget(s) in a container widget named
29  * with the desired case id.
30  *
31  * @author Timothy Larson
32  * @version $Id: Union.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class Union extends AbstractContainerWidget {
35     private static final String JavaDoc ELEMENT = "field";
36     private Widget caseWidget;
37
38     public Union(UnionDefinition definition) {
39         super(definition);
40         setLocation(definition.getLocation());
41         // TODO: Remove after moving logic to Field.
42
//item.enteredValue = (String)definition.getDefaultValue();
43
}
44
45     // TODO: This whole union mess is too dependent on undefined sequences of execution.
46
// These need to be ordered into a contract of sequences.
47

48     public void setParent(Widget widget) {
49         super.setParent(widget);
50         resolve();
51     }
52
53     // TODO: The "resolve" step currently expands each "New" into the list of widgets in the corresponding "Class".
54
// "resolve" should be changed to "expand", and a new step, "resolve" should be introduced which patches up any
55
// *widget* (not definition) references after the expansion has put all of the widgets in place.
56
public void resolve() {
57         String JavaDoc caseWidgetId = ((UnionDefinition)definition).getCaseWidgetId();
58         caseWidget = getParent().getWidget(caseWidgetId);
59     }
60
61     public String JavaDoc getElementName() {
62         return ELEMENT;
63     }
64
65     public Object JavaDoc getValue() {
66         return caseWidget.getValue();
67     }
68
69     public void readFromRequest(FormContext formContext) {
70         // Ensure the case widgets got its value
71
caseWidget.readFromRequest(formContext);
72         Widget widget;
73         // Read current case from request
74
String JavaDoc value = (String JavaDoc)getValue();
75         if (value != null && !value.equals(""))
76             if ((widget = getWidget(value)) != null)
77                 widget.readFromRequest(formContext);
78
79         // Read union discriminant value from request
80
//item.readFromRequest(formContext);
81
}
82
83     // TODO: Simplify this logic.
84
public boolean validate(FormContext formContext) {
85         Widget widget;
86         boolean valid = true;
87         // Read current case from request
88
String JavaDoc value = (String JavaDoc)getValue();
89         if (value != null && !value.equals(""))
90             if ((widget = getWidget(value)) != null)
91                 valid = valid & widget.validate(formContext);
92         return valid;
93     }
94
95     public Widget getWidget(String JavaDoc id) {
96         if (!widgets.hasWidget(id) && ((ContainerDefinition)definition).hasWidget(id))
97             ((ContainerDefinition)definition).createWidget(this, id);
98         return super.getWidget(id);
99     }
100
101     // This method is overridden to suppress output of sub-widget sax fragments.
102
public void generateItemsSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
103         // Do nothing
104
}
105
106     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
107         generateSaxFragment(contentHandler, locale, ELEMENT);
108     }
109 }
110
Popular Tags