KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.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.forms.datatype.Datatype;
22 import org.apache.cocoon.forms.datatype.DynamicSelectionList;
23 import org.apache.cocoon.forms.datatype.FlowJXPathSelectionList;
24 import org.apache.cocoon.forms.datatype.SelectionList;
25 import org.apache.cocoon.forms.event.ValueChangedEvent;
26 import org.apache.cocoon.forms.event.ValueChangedListener;
27 import org.apache.cocoon.forms.event.WidgetEventMulticaster;
28
29 /**
30  * Base class for WidgetDefinitions that use a Datatype and SelectionList.
31  *
32  * @version $Id: AbstractDatatypeWidgetDefinition.java 289538 2005-09-16 13:46:22Z sylvain $
33  */

34 public abstract class AbstractDatatypeWidgetDefinition extends AbstractWidgetDefinition implements Serviceable {
35     private Datatype datatype;
36     private Object JavaDoc initialValue;
37     private SelectionList selectionList;
38     private ValueChangedListener listener;
39     private ServiceManager manager;
40
41     public void service(ServiceManager manager) throws ServiceException {
42         checkMutable();
43         this.manager = manager;
44     }
45     
46     /**
47      * checks
48      * @return
49      */

50     public void checkCompleteness() throws IncompletenessException {
51         super.checkCompleteness();
52         
53         if(this.datatype==null)
54             throw new IncompletenessException("A datatype element is required!",this);
55         
56     }
57     
58     /**
59      * initialize this definition with the other, sort of like a copy constructor
60      */

61     public void initializeFrom(WidgetDefinition definition) throws Exception JavaDoc {
62         super.initializeFrom(definition);
63         
64         if(definition instanceof AbstractDatatypeWidgetDefinition) {
65             AbstractDatatypeWidgetDefinition other = (AbstractDatatypeWidgetDefinition)definition;
66             
67             this.datatype = other.datatype;
68             this.initialValue = other.initialValue;
69             this.selectionList = other.selectionList;
70             this.listener = other.listener;
71             
72         } else {
73             throw new Exception JavaDoc("Definition to inherit from is not of the right type! (at "+getLocation()+")");
74         }
75     }
76
77     public Datatype getDatatype() {
78         return datatype;
79     }
80     
81     public Object JavaDoc getInitialValue() {
82         return this.initialValue;
83     }
84
85     public void setDatatype(Datatype datatype, Object JavaDoc initialValue) {
86         checkMutable();
87         this.datatype = datatype;
88         this.initialValue = initialValue;
89     }
90
91     public void setSelectionList(SelectionList selectionList) {
92         checkMutable();
93         if (selectionList != null && selectionList.getDatatype() != getDatatype())
94             throw new RuntimeException JavaDoc("Tried to assign a SelectionList that is not associated with this widget's datatype.");
95         this.selectionList = selectionList;
96     }
97
98     public SelectionList getSelectionList() {
99         return selectionList;
100     }
101     
102     /**
103      * Builds a dynamic selection list from a source. This is a helper method for widget instances whose selection
104      * list source has to be changed dynamically, and it does not modify this definition's selection list,
105      * if any.
106      * @param uri The URI of the source.
107      */

108     public SelectionList buildSelectionList(String JavaDoc uri) {
109         return new DynamicSelectionList(datatype, uri, this.manager);
110     }
111     
112     /**
113      * Builds a dynamic selection list from an in-memory collection.
114      * This is a helper method for widget instances whose selection
115      * list has to be changed dynamically, and it does not modify this definition's selection list,
116      * if any.
117      * @see org.apache.cocoon.forms.formmodel.Field#setSelectionList(Object model, String valuePath, String labelPath)
118      * @param model The collection used as a model for the selection list.
119      * @param valuePath An XPath expression referring to the attribute used
120      * to populate the values of the list's items.
121      * @param labelPath An XPath expression referring to the attribute used
122      * to populate the labels of the list's items.
123      */

124     public SelectionList buildSelectionListFromModel(Object JavaDoc model, String JavaDoc valuePath, String JavaDoc labelPath) {
125         return new FlowJXPathSelectionList(model, valuePath, labelPath, datatype);
126     }
127     
128     public void addValueChangedListener(ValueChangedListener listener) {
129         checkMutable();
130         this.listener = WidgetEventMulticaster.add(this.listener, listener);
131     }
132     
133     public void fireValueChangedEvent(ValueChangedEvent event) {
134         if (this.listener != null) {
135             this.listener.valueChanged(event);
136         }
137     }
138     
139     public boolean hasValueChangedListeners() {
140         return this.listener != null;
141     }
142
143     public ValueChangedListener getValueChangedListener() {
144         return this.listener;
145     }
146
147 }
148
Popular Tags