KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > beans > FeatureDescriptor


1 /*
2  * @(#)FeatureDescriptor.java 1.33 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.beans;
9
10 import java.lang.ref.Reference JavaDoc;
11 import java.lang.ref.WeakReference JavaDoc;
12 import java.lang.ref.SoftReference JavaDoc;
13
14 /**
15  * The FeatureDescriptor class is the common baseclass for PropertyDescriptor,
16  * EventSetDescriptor, and MethodDescriptor, etc.
17  * <p>
18  * It supports some common information that can be set and retrieved for
19  * any of the introspection descriptors.
20  * <p>
21  * In addition it provides an extension mechanism so that arbitrary
22  * attribute/value pairs can be associated with a design feature.
23  */

24
25 public class FeatureDescriptor {
26
27     private Reference JavaDoc classRef;
28
29     /**
30      * Constructs a <code>FeatureDescriptor</code>.
31      */

32     public FeatureDescriptor() {
33     }
34
35     /**
36      * Gets the programmatic name of this feature.
37      *
38      * @return The programmatic name of the property/method/event
39      */

40     public String JavaDoc getName() {
41     return name;
42     }
43
44     /**
45      * Sets the programmatic name of this feature.
46      *
47      * @param name The programmatic name of the property/method/event
48      */

49     public void setName(String JavaDoc name) {
50     this.name = name;
51     }
52
53     /**
54      * Gets the localized display name of this feature.
55      *
56      * @return The localized display name for the property/method/event.
57      * This defaults to the same as its programmatic name from getName.
58      */

59     public String JavaDoc getDisplayName() {
60     if (displayName == null) {
61         return getName();
62     }
63     return displayName;
64     }
65
66     /**
67      * Sets the localized display name of this feature.
68      *
69      * @param displayName The localized display name for the
70      * property/method/event.
71      */

72     public void setDisplayName(String JavaDoc displayName) {
73     this.displayName = displayName;
74     }
75
76     /**
77      * The "expert" flag is used to distinguish between those features that are
78      * intended for expert users from those that are intended for normal users.
79      *
80      * @return True if this feature is intended for use by experts only.
81      */

82     public boolean isExpert() {
83     return expert;
84     }
85
86     /**
87      * The "expert" flag is used to distinguish between features that are
88      * intended for expert users from those that are intended for normal users.
89      *
90      * @param expert True if this feature is intended for use by experts only.
91      */

92     public void setExpert(boolean expert) {
93     this.expert = expert;
94     }
95
96     /**
97      * The "hidden" flag is used to identify features that are intended only
98      * for tool use, and which should not be exposed to humans.
99      *
100      * @return True if this feature should be hidden from human users.
101      */

102     public boolean isHidden() {
103     return hidden;
104     }
105
106     /**
107      * The "hidden" flag is used to identify features that are intended only
108      * for tool use, and which should not be exposed to humans.
109      *
110      * @param hidden True if this feature should be hidden from human users.
111      */

112     public void setHidden(boolean hidden) {
113     this.hidden = hidden;
114     }
115
116     /**
117      * The "preferred" flag is used to identify features that are particularly
118      * important for presenting to humans.
119      *
120      * @return True if this feature should be preferentially shown to human users.
121      */

122     public boolean isPreferred() {
123     return preferred;
124     }
125
126     /**
127      * The "preferred" flag is used to identify features that are particularly
128      * important for presenting to humans.
129      *
130      * @param preferred True if this feature should be preferentially shown
131      * to human users.
132      */

133     public void setPreferred(boolean preferred) {
134     this.preferred = preferred;
135     }
136
137     /**
138      * Gets the short description of this feature.
139      *
140      * @return A localized short description associated with this
141      * property/method/event. This defaults to be the display name.
142      */

143     public String JavaDoc getShortDescription() {
144     if (shortDescription == null) {
145         return getDisplayName();
146     }
147     return shortDescription;
148     }
149
150     /**
151      * You can associate a short descriptive string with a feature. Normally
152      * these descriptive strings should be less than about 40 characters.
153      * @param text A (localized) short description to be associated with
154      * this property/method/event.
155      */

156     public void setShortDescription(String JavaDoc text) {
157     shortDescription = text;
158     }
159
160     /**
161      * Associate a named attribute with this feature.
162      *
163      * @param attributeName The locale-independent name of the attribute
164      * @param value The value.
165      */

166     public void setValue(String JavaDoc attributeName, Object JavaDoc value) {
167     if (table == null) {
168         table = new java.util.Hashtable JavaDoc();
169     }
170     table.put(attributeName, value);
171     }
172
173     /**
174      * Retrieve a named attribute with this feature.
175      *
176      * @param attributeName The locale-independent name of the attribute
177      * @return The value of the attribute. May be null if
178      * the attribute is unknown.
179      */

180     public Object JavaDoc getValue(String JavaDoc attributeName) {
181     if (table == null) {
182        return null;
183     }
184     return table.get(attributeName);
185     }
186
187     /**
188      * Gets an enumeration of the locale-independent names of this
189      * feature.
190      *
191      * @return An enumeration of the locale-independent names of any
192      * attributes that have been registered with setValue.
193      */

194     public java.util.Enumeration JavaDoc<String JavaDoc> attributeNames() {
195     if (table == null) {
196         table = new java.util.Hashtable JavaDoc();
197     }
198     return table.keys();
199     }
200
201     /**
202      * Package-private constructor,
203      * Merge information from two FeatureDescriptors.
204      * The merged hidden and expert flags are formed by or-ing the values.
205      * In the event of other conflicts, the second argument (y) is
206      * given priority over the first argument (x).
207      *
208      * @param x The first (lower priority) MethodDescriptor
209      * @param y The second (higher priority) MethodDescriptor
210      */

211     FeatureDescriptor(FeatureDescriptor JavaDoc x, FeatureDescriptor JavaDoc y) {
212     expert = x.expert | y.expert;
213     hidden = x.hidden | y.hidden;
214     preferred = x.preferred | y.preferred;
215     name = y.name;
216     shortDescription = x.shortDescription;
217     if (y.shortDescription != null) {
218         shortDescription = y.shortDescription;
219     }
220     displayName = x.displayName;
221     if (y.displayName != null) {
222         displayName = y.displayName;
223     }
224     classRef = x.classRef;
225     if (y.classRef != null) {
226         classRef = y.classRef;
227     }
228     addTable(x.table);
229     addTable(y.table);
230     }
231
232     /*
233      * Package-private dup constructor
234      * This must isolate the new object from any changes to the old object.
235      */

236     FeatureDescriptor(FeatureDescriptor JavaDoc old) {
237     expert = old.expert;
238     hidden = old.hidden;
239     preferred = old.preferred;
240     name = old.name;
241     shortDescription = old.shortDescription;
242     displayName = old.displayName;
243     classRef = old.classRef;
244
245     addTable(old.table);
246     }
247
248     private void addTable(java.util.Hashtable JavaDoc t) {
249     if (t == null) {
250         return;
251     }
252     java.util.Enumeration JavaDoc keys = t.keys();
253     while (keys.hasMoreElements()) {
254         String JavaDoc key = (String JavaDoc)keys.nextElement();
255         Object JavaDoc value = t.get(key);
256         setValue(key, value);
257     }
258     }
259
260     // Package private methods for recreating the weak/soft referent
261

262     void setClass0(Class JavaDoc cls) {
263     classRef = createReference(cls);
264     }
265
266     Class JavaDoc getClass0() {
267     return (Class JavaDoc)getObject(classRef);
268     }
269
270     /**
271      * Create a Reference wrapper for the object.
272      *
273      * @param obj object that will be wrapped
274      * @param soft true if a SoftReference should be created; otherwise Soft
275      * @return a Reference or null if obj is null.
276      */

277     static Reference JavaDoc createReference(Object JavaDoc obj, boolean soft) {
278     Reference JavaDoc ref = null;
279     if (obj != null) {
280         if (soft) {
281         ref = new SoftReference JavaDoc(obj);
282         } else {
283         ref = new WeakReference JavaDoc(obj);
284         }
285     }
286     return ref;
287     }
288
289     // Convenience method which creates a WeakReference.
290
static Reference JavaDoc createReference(Object JavaDoc obj) {
291     return createReference(obj, false);
292     }
293
294     /**
295      * Returns an object from a Reference wrapper.
296      *
297      * @return the Object in a wrapper or null.
298      */

299     static Object JavaDoc getObject(Reference JavaDoc ref) {
300     return (ref == null) ? null : (Object JavaDoc)ref.get();
301     }
302
303     static String JavaDoc capitalize(String JavaDoc s) {
304     return NameGenerator.capitalize(s);
305     }
306
307     private boolean expert;
308     private boolean hidden;
309     private boolean preferred;
310     private String JavaDoc shortDescription;
311     private String JavaDoc name;
312     private String JavaDoc displayName;
313     private java.util.Hashtable JavaDoc table;
314 }
315
Popular Tags