KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale JavaDoc;
19
20 import org.apache.cocoon.forms.FormsConstants;
21 import org.apache.cocoon.forms.FormContext;
22 import org.apache.cocoon.forms.datatype.Datatype;
23 import org.apache.cocoon.forms.event.ValueChangedEvent;
24 import org.apache.cocoon.forms.event.ValueChangedListener;
25 import org.apache.cocoon.forms.event.ValueChangedListenerEnabled;
26 import org.apache.cocoon.forms.event.WidgetEvent;
27 import org.apache.cocoon.forms.event.WidgetEventMulticaster;
28 import org.apache.cocoon.xml.XMLUtils;
29 import org.apache.commons.lang.ObjectUtils;
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * An Output widget can be used to show a non-editable value to the user.
35  * An Output widget is associated with a certain
36  * {@link org.apache.cocoon.forms.datatype.Datatype Datatype}.
37  *
38  * <p>An Output widget is always valid and never required.
39  *
40  * @version $Id: Output.java 328140 2005-10-24 19:22:55Z sylvain $
41  */

42 public class Output extends AbstractWidget implements DataWidget, ValueChangedListenerEnabled {
43     
44     private final OutputDefinition definition;
45     private Object JavaDoc value;
46     private ValueChangedListener listener;
47
48     public OutputDefinition getOutputDefinition() {
49         return definition;
50     }
51
52     public Datatype getDatatype() {
53         return definition.getDatatype();
54     }
55
56     protected Output(OutputDefinition definition) {
57         super(definition);
58         this.definition = definition;
59         this.listener = definition.getValueChangedListener();
60     }
61
62     public WidgetDefinition getDefinition() {
63         return definition;
64     }
65
66     public void readFromRequest(FormContext formContext) {
67         // do nothing
68
}
69
70     /**
71      * @see org.apache.cocoon.forms.formmodel.Widget#validate()
72      */

73     public boolean validate() {
74         return true;
75     }
76
77     /**
78      * @see org.apache.cocoon.forms.formmodel.Widget#isValid()
79      */

80     public boolean isValid() {
81         return true;
82     }
83
84     private static final String JavaDoc OUTPUT_EL = "output";
85     private static final String JavaDoc VALUE_EL = "value";
86
87
88     /**
89      * @return "output"
90      */

91     public String JavaDoc getXMLElementName() {
92         return OUTPUT_EL;
93     }
94
95     protected void generateItemSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
96         // the value
97
if (value != null) {
98             contentHandler.startElement(FormsConstants.INSTANCE_NS, VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL, XMLUtils.EMPTY_ATTRIBUTES);
99             String JavaDoc stringValue;
100             stringValue = definition.getDatatype().convertToString(value, locale);
101             contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
102             contentHandler.endElement(FormsConstants.INSTANCE_NS, VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL);
103         }
104     }
105
106     public Object JavaDoc getValue() {
107         return value;
108     }
109
110     public void setValue(Object JavaDoc object) {
111         if (object != null && !definition.getDatatype().getTypeClass().isAssignableFrom(object.getClass())) {
112             throw new RuntimeException JavaDoc("Tried to set value of output widget \""
113                                        + getRequestParameterName()
114                                        + "\" with an object of an incorrect type: "
115                                        + "expected " + definition.getDatatype().getTypeClass()
116                                        + ", received " + object.getClass() + ".");
117         }
118         if (!ObjectUtils.equals(value, object)) {
119             Object JavaDoc oldValue = value;
120             value = object;
121             if (this.hasValueChangedListeners() || this.getForm().hasFormHandler()) {
122                 getForm().addWidgetEvent(new ValueChangedEvent(this, oldValue, value));
123             }
124             getForm().addWidgetUpdate(this);
125         }
126     }
127
128     public void addValueChangedListener(ValueChangedListener listener) {
129         this.listener = WidgetEventMulticaster.add(this.listener, listener);
130     }
131
132     public void removeValueChangedListener(ValueChangedListener listener) {
133         this.listener = WidgetEventMulticaster.remove(this.listener, listener);
134     }
135
136     public boolean hasValueChangedListeners() {
137         return this.listener != null;
138     }
139
140     public void broadcastEvent(WidgetEvent event) {
141         if (event instanceof ValueChangedEvent) {
142             if (this.listener != null) {
143                 this.listener.valueChanged((ValueChangedEvent)event);
144             }
145         } else {
146             // Other kinds of events
147
super.broadcastEvent(event);
148         }
149     }
150 }
151
Popular Tags