KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > EnumeratedMetaData


1 /*
2  * $Id: EnumeratedMetaData.java,v 1.2 2004/09/08 23:36:49 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.data;
9
10 import java.util.List JavaDoc;
11 import java.util.Locale JavaDoc;
12
13 /**
14  * <p>
15  * Class for representing meta-data for data fields which have a finite
16  * set of possible values. The type of each value in the enumeration must
17  * match the type (&quot;elementClass&quot; property) of the meta-data object.
18  * Example usage:<br>
19  * <pre><code>
20  * String weekdays[] = {&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,
21  * &quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;};
22  * EnumeratedMetaData metaData = new EnumeratedMetaData(&quot;weekday&quot;, String.class,
23  * &quot;Day of Week&quot;);
24  * metaData.setEnumeration(weekdays);
25  * </code></pre>
26  * </p>
27  *
28  * @author Amy Fowler
29  * @version 1.0
30  */

31
32 public class EnumeratedMetaData extends MetaData {
33
34     protected Object JavaDoc[] enumeration;
35
36     /**
37       * Instantiates a meta-data object with a default name &quot;enumvalue&quot; and
38       * a default field class equal to <code>java.lang.String</code>.
39       * This provides the no-argument constructor required for JavaBeans.
40       * It is recommended that the program explicitly set a meaningful
41       * &quot;name&quot; property.
42       */

43      public EnumeratedMetaData() {
44          this("enumvalue");
45      }
46
47     /**
48      * Instantiates a meta-data object with the specified name and
49      * a default field class equal to <code>java.lang.String</code>.
50      * @param name String containing the name of the data field
51      */

52     public EnumeratedMetaData(String JavaDoc name) {
53         super(name);
54     }
55
56     /**
57      * Instantiates a meta-data object with the specified name and
58      * field class.
59      * @param name String containing the name of the data field
60      * @param klass Class indicating type of data field
61      */

62     public EnumeratedMetaData(String JavaDoc name, Class JavaDoc klass) {
63         super(name, klass);
64     }
65
66     /**
67      * Instantiates a meta-data object with the specified name,
68      * field class, and label.
69      * @param name String containing the name of the data field
70      * @param klass Class indicating type of data field
71      * @param label String containing the user-displayable label for the
72      * data field
73      */

74     public EnumeratedMetaData(String JavaDoc name, Class JavaDoc klass, String JavaDoc label) {
75         super(name, klass, label);
76     }
77
78     /**
79      * Gets the meta-data &quot;enumeration&quot; property which
80      * contains the set of possible values for the associated data field.
81      * The returned array is a copy, therefore modifications to that array
82      * will have no affect on the property.
83      * @see #setEnumeration
84      * @return array containing 0 or more enumerated values for the data field
85      */

86     public Object JavaDoc[] getEnumeration() {
87         Object JavaDoc[] evalues;
88         if (enumeration != null) {
89             evalues = new Object JavaDoc[enumeration.length];
90             System.arraycopy(enumeration, 0, evalues, 0,
91                              enumeration.length);
92         }
93         else {
94             evalues = new Object JavaDoc[0];
95         }
96         return evalues;
97     }
98
99     /**
100      * Sets the meta-data &quot;enumeration&quot; property by copying
101      * the values contained in the specified array to an internal representation.
102      * @see #getEnumeration
103      * @param enumeration array containing 0 or more enumerated values for the data field
104      */

105     public void setEnumeration(Object JavaDoc[] enumeration) {
106         Object JavaDoc oldEnumeration[] = this.enumeration;
107         this.enumeration = new Object JavaDoc[enumeration.length];
108         System.arraycopy(enumeration, 0, this.enumeration, 0,
109                          enumeration.length);
110         firePropertyChange("enumeration", oldEnumeration, enumeration);
111     }
112
113     /**
114      * Sets the meta-data &quot;enumeration&quot; property by copying
115      * the values contained in the specified list to an internal representation.
116      * @see #getEnumeration
117      * @param enumeration list containing 0 or more enumerated values for the data field
118      */

119     public void setEnumeration(List JavaDoc enumeration) {
120         setEnumeration(enumeration.toArray());
121     }
122
123
124 }
125
Popular Tags