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.woody.formmodel;
018:
019: import org.apache.avalon.framework.service.ServiceException;
020: import org.apache.avalon.framework.service.ServiceManager;
021: import org.apache.avalon.framework.service.Serviceable;
022: import org.apache.cocoon.woody.datatype.Datatype;
023: import org.apache.cocoon.woody.datatype.DynamicSelectionList;
024: import org.apache.cocoon.woody.datatype.FlowJXPathSelectionList;
025: import org.apache.cocoon.woody.datatype.SelectionList;
026: import org.apache.cocoon.woody.event.ValueChangedEvent;
027: import org.apache.cocoon.woody.event.ValueChangedListener;
028: import org.apache.cocoon.woody.event.WidgetEventMulticaster;
029:
030: /**
031: * Base class for WidgetDefinitions that use a Datatype and SelectionList.
032: *
033: * @version $Id: AbstractDatatypeWidgetDefinition.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public abstract class AbstractDatatypeWidgetDefinition extends
036: AbstractWidgetDefinition implements Serviceable {
037: private Datatype datatype;
038: private SelectionList selectionList;
039: private ValueChangedListener listener;
040: private ServiceManager manager;
041:
042: public void service(ServiceManager manager) throws ServiceException {
043: this .manager = manager;
044: }
045:
046: public Datatype getDatatype() {
047: return datatype;
048: }
049:
050: public void setDatatype(Datatype datatype) {
051: this .datatype = datatype;
052: }
053:
054: public void setSelectionList(SelectionList selectionList) {
055: if (selectionList.getDatatype() != getDatatype())
056: throw new RuntimeException(
057: "Tried to assign a SelectionList that is not associated with this widget's datatype.");
058: this .selectionList = selectionList;
059: }
060:
061: public SelectionList getSelectionList() {
062: return selectionList;
063: }
064:
065: /**
066: * Builds a dynamic selection list from a source. This is a helper method for widget instances whose selection
067: * list source has to be changed dynamically, and it does not modify this definition's selection list,
068: * if any.
069: * @param uri The URI of the source.
070: */
071: public SelectionList buildSelectionList(String uri) {
072: return new DynamicSelectionList(datatype, uri, this .manager);
073: }
074:
075: /**
076: * Builds a dynamic selection list from an in-memory collection.
077: * This is a helper method for widget instances whose selection
078: * list has to be changed dynamically, and it does not modify this definition's selection list,
079: * if any.
080: * @see org.apache.cocoon.woody.formmodel.Field#setSelectionList(Object model, String valuePath, String labelPath)
081: * @param model The collection used as a model for the selection list.
082: * @param valuePath An XPath expression referring to the attribute used
083: * to populate the values of the list's items.
084: * @param labelPath An XPath expression referring to the attribute used
085: * to populate the labels of the list's items.
086: */
087: public SelectionList buildSelectionListFromModel(Object model,
088: String valuePath, String labelPath) {
089: return new FlowJXPathSelectionList(model, valuePath, labelPath,
090: datatype);
091: }
092:
093: public void addValueChangedListener(ValueChangedListener listener) {
094: this .listener = WidgetEventMulticaster.add(this .listener,
095: listener);
096: }
097:
098: public void fireValueChangedEvent(ValueChangedEvent event) {
099: if (this .listener != null) {
100: this .listener.valueChanged(event);
101: }
102: }
103:
104: public boolean hasValueChangedListeners() {
105: return this.listener != null;
106: }
107:
108: }
|