KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > api > properties > BaseMap


1 package org.apache.beehive.controls.api.properties;
2 /*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * $Header:$
18  */

19
20 import java.lang.annotation.Annotation JavaDoc;
21
22 import org.apache.beehive.controls.api.bean.ControlBean;
23 import org.apache.beehive.controls.api.bean.ControlExtension;
24 import org.apache.beehive.controls.api.bean.ControlInterface;
25 import org.apache.beehive.controls.api.bean.ExternalPropertySets;
26
27 /**
28  * The BaseMap class provide an abstract base PropertyMap class from which other
29  * concrete PropertyMap implementation can derive. It contains some common code
30  * (such as property key validation and the implementation of the base delegation model)
31  * that is generically useful.
32  */

33 abstract public class BaseMap implements PropertyMap, java.io.Serializable JavaDoc
34 {
35     /**
36      * Sets the PropertySet or Control interface associated with this map. Only properties
37      * declared by the PropertySet or one of the PropertySets on the Control interface may
38      * be used with this map.
39      */

40     protected void setMapClass(Class JavaDoc mapClass)
41     {
42         //
43
// If the provided map class is a ControlBean type, then locate associated control
44
// interface or extension that defines properties.
45
//
46
if (ControlBean.class.isAssignableFrom(mapClass))
47         {
48             Class JavaDoc [] intfs = mapClass.getInterfaces();
49             for (int i = 0; i < intfs.length; i++)
50             {
51                 if (intfs[i].isAnnotationPresent(ControlInterface.class) ||
52                     intfs[i].isAnnotationPresent(ControlExtension.class))
53                 {
54                     mapClass = intfs[i];
55                     break;
56                 }
57             }
58         }
59         else
60         {
61             if (!mapClass.isAnnotation() &&
62                 !mapClass.isAnnotationPresent(ControlInterface.class) &&
63                 !mapClass.isAnnotationPresent(ControlExtension.class))
64                 throw new IllegalArgumentException JavaDoc(mapClass+" must be Control or annotation type");
65         }
66
67         _mapClass = mapClass;
68     }
69
70     /**
71      * Returns the PropertySet or Control interface class associated with the PropertyMap.
72      */

73     public Class JavaDoc getMapClass() { return _mapClass; }
74
75     /**
76      * Checks to see if the provided class is a control or property set interface that is
77      * compatible with the local PropertyMap.
78      */

79     private boolean isCompatibleClass(Class JavaDoc checkClass)
80     {
81         //
82
// If the check class is equal to or a super-interface of the map class, then
83
// they are compatible.
84
//
85
if (_mapClass.isAssignableFrom(checkClass))
86             return true;
87
88         //
89
// If the check class is a property set declared by the map class or a super interface
90
// of the map class, then they are compatible.
91
//
92
if (checkClass.isAnnotationPresent(PropertySet.class))
93         {
94             Class JavaDoc declaringClass = checkClass.getDeclaringClass();
95
96             // External property sets are always compatible.
97
// TODO: Could do a more extensive check..
98
if (declaringClass == null)
99                 return true;
100
101             if (declaringClass.isAssignableFrom(_mapClass))
102                 return true;
103         }
104
105         //
106
// If the map class is a property set declared by the check class or a super interface
107
// of the check class, then they are compatible. This is the inverse of the last check,
108
// and happens e.g. when a programatically instantiated control w/ an initial property
109
// map needs to delegate to the control interface's property map.
110
//
111
if (_mapClass.isAnnotationPresent(PropertySet.class))
112         {
113             Class JavaDoc declaringClass = _mapClass.getDeclaringClass();
114             if (declaringClass != null &&
115                 declaringClass.isAssignableFrom(checkClass))
116                 return true;
117
118             // External property sets have no declaring class
119
if (declaringClass == null)
120             {
121                 ExternalPropertySets eps = (ExternalPropertySets) checkClass.getAnnotation(ExternalPropertySets.class);
122                 if (eps != null)
123                 {
124                     Class JavaDoc[] propSets = eps.value();
125                     if (propSets != null)
126                     {
127                         for (Class JavaDoc ps : propSets)
128                         {
129                             if (_mapClass.equals(ps))
130                                 return true;
131                         }
132                     }
133                 }
134             }
135         }
136
137         return false;
138     }
139
140     /**
141      * Checks to ensure that the provided key is a valid key for this PropertyMap
142      */

143     protected boolean isValidKey(PropertyKey key)
144     {
145         return isCompatibleClass(key.getPropertySet());
146     }
147
148     /**
149      * Sets a delegate base property map from which values will be derived if not found within
150      * the local property map.
151      */

152     public synchronized void setDelegateMap(PropertyMap delegateMap)
153     {
154         if (!isCompatibleClass(delegateMap.getMapClass()))
155             throw new IllegalArgumentException JavaDoc("The delegate map type (" + delegateMap.getMapClass() + " is an incompatible type with " + _mapClass);
156
157         _delegateMap = delegateMap;
158     }
159
160     /**
161      * Returns a delegate base property map from which values will be derived if not found within
162      * the local property map.
163      */

164     public PropertyMap getDelegateMap()
165     {
166         return _delegateMap;
167     }
168
169     /**
170      * Returns the property value specified by 'key' within this map.
171      */

172     public Object JavaDoc getProperty(PropertyKey key)
173     {
174         //
175
// Delegate up to any parent map
176
//
177
if (_delegateMap != null)
178             return _delegateMap.getProperty(key);
179
180         //
181
// If neither found a value, return the default value
182
//
183
return key.getDefaultValue();
184     }
185
186     /**
187      * Returns true if the PropertyMap contains one or more values for the specified
188      * PropertySet, false otherwise.
189      */

190     public boolean containsPropertySet(Class JavaDoc<? extends Annotation JavaDoc> propertySet)
191     {
192         //
193
// Defer to any delegate map
194
//
195
if (_delegateMap != null)
196             return _delegateMap.containsPropertySet(propertySet);
197
198         return false;
199     }
200
201     /**
202      * Returns a PropertySet proxy instance that derives its data from the contents of
203      * the property map. Will return null if the PropertyMap does not contain any properties
204      * associated with the specified PropertySet.
205      */

206     public <T extends Annotation JavaDoc> T getPropertySet(Class JavaDoc<T> propertySet)
207     {
208         if (!containsPropertySet(propertySet))
209             return null;
210
211         return PropertySetProxy.getProxy(propertySet, this);
212     }
213
214     Class JavaDoc _mapClass; // associated Control or PropertySet class
215
PropertyMap _delegateMap; // wrapped PropertyMap (or null)
216
}
217
Popular Tags