KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.service.ServiceException;
19 import org.apache.avalon.framework.service.ServiceManager;
20 import org.apache.avalon.framework.service.Serviceable;
21 import org.apache.cocoon.woody.datatype.Datatype;
22 import org.apache.cocoon.woody.datatype.DynamicSelectionList;
23 import org.apache.cocoon.woody.datatype.FlowJXPathSelectionList;
24 import org.apache.cocoon.woody.datatype.SelectionList;
25 import org.apache.cocoon.woody.event.ValueChangedEvent;
26 import org.apache.cocoon.woody.event.ValueChangedListener;
27 import org.apache.cocoon.woody.event.WidgetEventMulticaster;
28
29 /**
30  * Base class for WidgetDefinitions that use a Datatype and SelectionList.
31  *
32  * @version $Id: AbstractDatatypeWidgetDefinition.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public abstract class AbstractDatatypeWidgetDefinition extends AbstractWidgetDefinition implements Serviceable {
35     private Datatype datatype;
36     private SelectionList selectionList;
37     private ValueChangedListener listener;
38     private ServiceManager manager;
39
40     public void service(ServiceManager manager) throws ServiceException {
41         this.manager = manager;
42     }
43
44     public Datatype getDatatype() {
45         return datatype;
46     }
47
48     public void setDatatype(Datatype datatype) {
49         this.datatype = datatype;
50     }
51
52     public void setSelectionList(SelectionList selectionList) {
53         if (selectionList.getDatatype() != getDatatype())
54             throw new RuntimeException JavaDoc("Tried to assign a SelectionList that is not associated with this widget's datatype.");
55         this.selectionList = selectionList;
56     }
57
58     public SelectionList getSelectionList() {
59         return selectionList;
60     }
61     
62     /**
63      * Builds a dynamic selection list from a source. This is a helper method for widget instances whose selection
64      * list source has to be changed dynamically, and it does not modify this definition's selection list,
65      * if any.
66      * @param uri The URI of the source.
67      */

68     public SelectionList buildSelectionList(String JavaDoc uri) {
69         return new DynamicSelectionList(datatype, uri, this.manager);
70     }
71     
72     /**
73      * Builds a dynamic selection list from an in-memory collection.
74      * This is a helper method for widget instances whose selection
75      * list has to be changed dynamically, and it does not modify this definition's selection list,
76      * if any.
77      * @see org.apache.cocoon.woody.formmodel.Field#setSelectionList(Object model, String valuePath, String labelPath)
78      * @param model The collection used as a model for the selection list.
79      * @param valuePath An XPath expression referring to the attribute used
80      * to populate the values of the list's items.
81      * @param labelPath An XPath expression referring to the attribute used
82      * to populate the labels of the list's items.
83      */

84     public SelectionList buildSelectionListFromModel(Object JavaDoc model, String JavaDoc valuePath, String JavaDoc labelPath) {
85         return new FlowJXPathSelectionList(model, valuePath, labelPath, datatype);
86     }
87     
88     public void addValueChangedListener(ValueChangedListener listener) {
89         this.listener = WidgetEventMulticaster.add(this.listener, listener);
90     }
91     
92     public void fireValueChangedEvent(ValueChangedEvent event) {
93         if (this.listener != null) {
94             this.listener.valueChanged(event);
95         }
96     }
97     
98     public boolean hasValueChangedListeners() {
99         return this.listener != null;
100     }
101
102 }
103
Popular Tags