KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors > EnumEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.editors;
21
22 import java.beans.*;
23
24 /**
25  * A property editor class handling enumeration values provided for some
26  * properties of Swing components.
27  *
28  * @author Tran Duc Trung, Tomas Pavek
29  */

30
31 public class EnumEditor extends PropertyEditorSupport
32                         implements org.netbeans.modules.form.NamedPropertyEditor
33 {
34     /** array of object triplets describing the enumeration
35      * 0 - displayed label
36      * 1 - value
37      * 2 - code string
38      */

39     private Object JavaDoc[] enumerationValues;
40
41     public EnumEditor(Object JavaDoc[] enumerationValues) {
42         translateEnumLabels(enumerationValues);
43         this.enumerationValues = enumerationValues;
44     }
45
46     // --------
47

48     public String JavaDoc[] getTags() {
49         int n = enumerationValues.length / 3;
50         String JavaDoc[] tags = new String JavaDoc[n];
51         for (int i=0; i < n; i++)
52             tags[i] = (String JavaDoc) enumerationValues[i*3];
53
54         return tags;
55     }
56
57     public void setAsText(String JavaDoc str) {
58         int n = enumerationValues.length / 3;
59         for (int i=0; i < n; i++)
60             if (enumerationValues[i*3].toString().equals(str)) {
61                 setValue(enumerationValues[i*3 + 1]);
62                 break;
63             }
64     }
65
66     public String JavaDoc getAsText() {
67         Object JavaDoc value = getValue();
68         int n = enumerationValues.length / 3;
69         for (int i=0; i < n; i++) {
70             Object JavaDoc eVal = enumerationValues[i*3 + 1];
71             if ((eVal == null && value == null) || (eVal != null && eVal.equals(value)))
72                 return enumerationValues[i*3].toString();
73         }
74
75         return enumerationValues.length > 0 ?
76                  enumerationValues[0].toString() : null;
77     }
78
79     public String JavaDoc getJavaInitializationString() {
80         String JavaDoc initString = null;
81
82         Object JavaDoc value = getValue();
83         int n = enumerationValues.length / 3;
84         for (int i=0; i < n; i++) {
85             Object JavaDoc eVal = enumerationValues[i*3 + 1];
86             if ((eVal == null && value == null) || (eVal != null && eVal.equals(value))) {
87                 initString = (String JavaDoc) enumerationValues[i*3 + 2];
88                 break;
89             }
90         }
91
92         if (initString == null)
93             initString = enumerationValues.length > 2 ?
94                          (String JavaDoc) enumerationValues[2] : null;
95         if (initString == null)
96             return null;
97
98         for (int i=0; i < swingClassNames.length; i++)
99             if (initString.startsWith(swingClassNames[i])) {
100                 initString = "javax.swing." + initString; // NOI18N
101
break;
102             }
103
104         return initString;
105     }
106
107     // -------
108
// NamedPropertyEditor
109

110     public String JavaDoc getDisplayName() {
111         return org.openide.util.NbBundle.getBundle(EnumEditor.class)
112                                             .getString("CTL_EnumEditorName"); // NOI18N
113
}
114
115     // -------
116

117     /* We arrange some constants description to better fit in narrow space
118      * in property sheet, e.g. instead of HORIZONTAL_SCROLLBAR_AS_NEEDED we
119      * show only AS_NEEDED. We keep the uppercase letters to preserve the
120      * feeling that the value is a constant.
121      */

122     private static void translateEnumLabels(Object JavaDoc[] enumerationValues) {
123         int n1 = enumerationValues.length / 3;
124         int n2 = arrangedEnumLabels.length / 2;
125
126         for (int i=0; i < n1; i++) {
127             String JavaDoc code = (String JavaDoc) enumerationValues[i*3 + 2];
128             for (int j=0; j < n2; j++)
129                 if (code.endsWith(arrangedEnumLabels[j*2])) {
130                     enumerationValues[i*3] = arrangedEnumLabels[j*2 + 1];
131                     break;
132                 }
133         }
134     }
135
136     private static String JavaDoc[] arrangedEnumLabels = {
137         "WindowConstants.DISPOSE_ON_CLOSE", "DISPOSE", // NOI18N
138
"WindowConstants.DO_NOTHING_ON_CLOSE", "DO_NOTHING", // NOI18N
139
"WindowConstants.HIDE_ON_CLOSE", "HIDE", // NOI18N
140
"JFrame.EXIT_ON_CLOSE", "EXIT", // NOI18N
141
"ListSelectionModel.MULTIPLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL", // NOI18N
142
"ListSelectionModel.SINGLE_INTERVAL_SELECTION", "SINGLE_INTERVAL", // NOI18N
143
"ListSelectionModel.SINGLE_SELECTION", "SINGLE", // NOI18N
144
"JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED", "AS_NEEDED", // NOI18N
145
"JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS", "ALWAYS", // NOI18N
146
"JScrollPane.HORIZONTAL_SCROLLBAR_NEVER", "NEVER", // NOI18N
147
"JScrollPane.VERTICAL_SCROLLBAR_ALWAYS", "ALWAYS", // NOI18N
148
"JScrollPane.VERTICAL_SCROLLBAR_NEVER", "NEVER", // NOI18N
149
"JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED", "AS_NEEDED", // NOI18N
150
"ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED", "AS_NEEDED", // NOI18N
151
"ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS", "ALWAYS", // NOI18N
152
"ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER", "NEVER", // NOI18N
153
"ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS", "ALWAYS", // NOI18N
154
"ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER", "NEVER", // NOI18N
155
"ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED", "AS_NEEDED", // NOI18N
156
"JTable.AUTO_RESIZE_NEXT_COLUMN", "NEXT_COLUMN", // NOI18N
157
"JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS", "SUBSEQUENT_COLUMNS", // NOI18N
158
"JTable.AUTO_RESIZE_OFF", "OFF", // NOI18N
159
"JTable.AUTO_RESIZE_ALL_COLUMNS", "ALL_COLUMNS", // NOI18N
160
"JTable.AUTO_RESIZE_LAST_COLUMN", "LAST_COLUMN" // NOI18N
161
};
162
163     private static String JavaDoc[] swingClassNames = {
164         "SwingConstants.", // NOI18N
165
"DebugGraphics.", // NOI18N
166
"JDesktopPane.", // NOI18N
167
"JFileChooser.", // NOI18N
168
"WindowConstants.", // NOI18N
169
"ListSelectionModel.", // NOI18N
170
"JScrollBar.", // NOI18N
171
"JScrollPane.", // NOI18N
172
"ScrollPaneConstants.", // NOI18N
173
"JSlider.", // NOI18N
174
"JSplitPane.", // NOI18N
175
"JTabbedPane.", // NOI18N
176
"JTable.", // NOI18N
177
"JTextField.", // NOI18N
178
"JViewport.", // NOI18N
179
"JFrame.", // NOI18N
180
"JList.", // NOI18N
181
"JFormattedTextField." // NOI18N
182
};
183 }
184
Popular Tags