KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.formmodel;
17
18 import org.apache.cocoon.forms.FormContext;
19 import org.apache.cocoon.forms.event.ValueChangedEvent;
20 import org.apache.cocoon.forms.event.ValueChangedListener;
21 import org.apache.cocoon.forms.event.ValueChangedListenerEnabled;
22 import org.apache.commons.lang.ObjectUtils;
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  * @version $Id: Union.java 210082 2005-07-11 08:06:48Z cziegeler $
32  */

33 public class Union extends AbstractContainerWidget {
34
35     //Note: union instances behave like simple "field" instance with respect to
36
// XSLT post-processing, the choice of element-name reflects this.
37
private static final String JavaDoc UNION_EL = "field";
38
39     private Widget caseWidget;
40     private String JavaDoc caseValue;
41
42     private final UnionDefinition definition;
43
44     public Union(UnionDefinition definition) {
45         super(definition);
46         this.definition = definition;
47         // TODO: Remove after moving logic to Field.
48
//item.enteredValue = (String)definition.getDefaultValue();
49
}
50
51     public WidgetDefinition getDefinition() {
52         return definition;
53     }
54
55     /**
56      * Called after widget's environment has been setup,
57      * to allow for any contextual initalization such as
58      * looking up case widgets for union widgets.
59      */

60     public void initialize() {
61         String JavaDoc caseWidgetId = definition.getCaseWidgetId();
62         this.caseWidget = getParent().lookupWidget(caseWidgetId);
63         if(this.caseWidget == null) {
64             throw new RuntimeException JavaDoc("Could not find case widget \""
65                 + caseWidgetId + "\" for union \"" + getId() + "\" at " + getLocation());
66         }
67         ((ValueChangedListenerEnabled)caseWidget).addValueChangedListener(
68             new ValueChangedListener() {
69                 public void valueChanged(ValueChangedEvent event) {
70                     String JavaDoc newValue = (String JavaDoc)event.getNewValue();
71                     if (!ObjectUtils.equals(Union.this.caseValue, newValue)) {
72                         Union.this.caseValue = newValue;
73                         getForm().addWidgetUpdate(Union.this);
74                     }
75                 }
76             }
77         );
78     }
79
80     /**
81      * @return "field"
82      */

83     public String JavaDoc getXMLElementName() {
84         return UNION_EL;
85     }
86
87     public Object JavaDoc getValue() {
88         return this.caseWidget.getValue();
89     }
90
91     public void readFromRequest(FormContext formContext) {
92         // Ensure the case widget has read its value
93
this.caseWidget.readFromRequest(formContext);
94
95         Widget widget;
96         // Read current case from request
97
String JavaDoc newValue = (String JavaDoc)getValue();
98         if (newValue != null && !newValue.equals("")) {
99
100             if (getForm().getSubmitWidget() == this.caseWidget && !newValue.equals(this.caseValue)) {
101                 // If submitted by the case widget and its value has changed, read the values
102
// for the previous case value. This allows to keep any already entered values
103
// despite the case change.
104
widget = getChild(this.caseValue);
105             } else {
106                 // Get the corresponding widget (will create it if needed)
107
widget = getChild(newValue);
108             }
109
110             if (widget != null && getCombinedState().isAcceptingInputs()) {
111                 widget.readFromRequest(formContext);
112             }
113         }
114         
115         if (!ObjectUtils.equals(this.caseValue, newValue)) {
116             this.caseValue = newValue;
117             getForm().addWidgetUpdate(this);
118         }
119     }
120
121     // TODO: Simplify this logic.
122
public boolean validate() {
123         if (!getCombinedState().isValidatingValues()) {
124             this.wasValid = true;
125             return true;
126         }
127         Widget widget;
128         boolean valid = true;
129         // Read current case from request
130
String JavaDoc value = (String JavaDoc)getValue();
131         if (value != null && !value.equals("")) {
132             if ((widget = getChild(value)) != null) {
133                 valid = valid & widget.validate();
134             }
135         }
136         this.wasValid = valid;
137         return valid;
138     }
139
140     public Widget getChild(String JavaDoc id) {
141         if (!widgets.hasWidget(id) && ((ContainerDefinition)definition).hasWidget(id)) {
142             ((ContainerDefinition)definition).createWidget(this, id);
143             Widget child = super.getChild(id);
144             child.initialize();
145             return child;
146         }
147         return super.getChild(id);
148     }
149
150     //TODO: check further: cause the claim in the accompanied comment doesn't seem
151
// to be completely correct
152

153     // This method is overridden to suppress output of sub-widget sax fragments.
154
// public void generateItemsSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
155
// // Do nothing
156
// }
157

158 }
159
Popular Tags