001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.formmodel;
018:
019: import java.util.Locale;
020:
021: import org.apache.cocoon.forms.FormsConstants;
022: import org.apache.cocoon.forms.FormContext;
023: import org.apache.cocoon.forms.datatype.Datatype;
024: import org.apache.cocoon.forms.datatype.SelectionList;
025: import org.apache.cocoon.forms.event.ValueChangedEvent;
026: import org.apache.cocoon.forms.event.ValueChangedListener;
027: import org.apache.cocoon.forms.event.ValueChangedListenerEnabled;
028: import org.apache.cocoon.forms.event.WidgetEvent;
029: import org.apache.cocoon.forms.event.WidgetEventMulticaster;
030: import org.apache.cocoon.xml.XMLUtils;
031: import org.apache.commons.lang.ObjectUtils;
032: import org.xml.sax.ContentHandler;
033: import org.xml.sax.SAXException;
034:
035: /**
036: * An Output widget can be used to show a non-editable value to the user.
037: * An Output widget is associated with a certain
038: * {@link org.apache.cocoon.forms.datatype.Datatype Datatype}.
039: *
040: * <p>An Output widget is always valid and never required.
041: *
042: * @version $Id: Output.java 462520 2006-10-10 19:39:14Z vgritsenko $
043: */
044: public class Output extends AbstractWidget implements DataWidget,
045: ValueChangedListenerEnabled {
046:
047: private final OutputDefinition definition;
048: private Object value;
049: private ValueChangedListener listener;
050:
051: public Output(OutputDefinition definition) {
052: super (definition);
053: this .definition = definition;
054: this .listener = definition.getValueChangedListener();
055: }
056:
057: public Datatype getDatatype() {
058: return definition.getDatatype();
059: }
060:
061: public WidgetDefinition getDefinition() {
062: return definition;
063: }
064:
065: public OutputDefinition getOutputDefinition() {
066: return definition;
067: }
068:
069: public void initialize() {
070: Object value = this .definition.getInitialValue();
071: if (value != null) {
072: setValue(value);
073: }
074: super .initialize();
075: }
076:
077: public void readFromRequest(FormContext formContext) {
078: // do nothing
079: }
080:
081: /**
082: * @see org.apache.cocoon.forms.formmodel.Widget#validate()
083: */
084: public boolean validate() {
085: return true;
086: }
087:
088: /**
089: * @see org.apache.cocoon.forms.formmodel.Widget#isValid()
090: */
091: public boolean isValid() {
092: return true;
093: }
094:
095: private static final String OUTPUT_EL = "output";
096: private static final String VALUE_EL = "value";
097:
098: /**
099: * @return "output"
100: */
101: public String getXMLElementName() {
102: return OUTPUT_EL;
103: }
104:
105: protected void generateItemSaxFragment(
106: ContentHandler contentHandler, Locale locale)
107: throws SAXException {
108: // the value
109: if (value != null) {
110: contentHandler.startElement(FormsConstants.INSTANCE_NS,
111: VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON
112: + VALUE_EL, XMLUtils.EMPTY_ATTRIBUTES);
113: String stringValue;
114: stringValue = definition.getDatatype().convertToString(
115: value, locale);
116: contentHandler.characters(stringValue.toCharArray(), 0,
117: stringValue.length());
118: contentHandler.endElement(FormsConstants.INSTANCE_NS,
119: VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON
120: + VALUE_EL);
121: }
122:
123: // generate selection list, if any
124: SelectionList selectionList = definition.getSelectionList();
125: if (selectionList != null) {
126: selectionList.generateSaxFragment(contentHandler, locale);
127: }
128:
129: // include some info about the datatype
130: definition.getDatatype().generateSaxFragment(contentHandler,
131: locale);
132: }
133:
134: public Object getValue() {
135: return value;
136: }
137:
138: public void setValue(Object object) {
139: if (object != null
140: && !definition.getDatatype().getTypeClass()
141: .isAssignableFrom(object.getClass())) {
142: throw new RuntimeException(
143: "Tried to set value of output widget \""
144: + getRequestParameterName()
145: + "\" with an object of an incorrect type: "
146: + "expected "
147: + definition.getDatatype().getTypeClass()
148: + ", received " + object.getClass() + ".");
149: }
150: if (!ObjectUtils.equals(value, object)) {
151: Object oldValue = value;
152: value = object;
153: if (hasValueChangedListeners()
154: || getForm().hasFormHandler()) {
155: getForm().addWidgetEvent(
156: new ValueChangedEvent(this , oldValue, value));
157: }
158: getForm().addWidgetUpdate(this );
159: }
160: }
161:
162: public void addValueChangedListener(ValueChangedListener listener) {
163: this .listener = WidgetEventMulticaster.add(this .listener,
164: listener);
165: }
166:
167: public void removeValueChangedListener(ValueChangedListener listener) {
168: this .listener = WidgetEventMulticaster.remove(this .listener,
169: listener);
170: }
171:
172: public boolean hasValueChangedListeners() {
173: return this .listener != null;
174: }
175:
176: public void broadcastEvent(WidgetEvent event) {
177: if (event instanceof ValueChangedEvent) {
178: if (this .listener != null) {
179: this .listener.valueChanged((ValueChangedEvent) event);
180: }
181: } else {
182: // Other kinds of events
183: super.broadcastEvent(event);
184: }
185: }
186: }
|