KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > FormPropertyEditorManager


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
21 package org.netbeans.modules.form;
22
23 import java.beans.*;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 /** A class that manages bean property editors for the Form Editor.
28  *
29  * @author Ian Formanek
30  */

31 final public class FormPropertyEditorManager extends Object JavaDoc
32 {
33     private static HashMap JavaDoc editorsCache = new HashMap JavaDoc(30);
34
35     private static HashMap JavaDoc expliciteEditors = new HashMap JavaDoc(10);
36
37     // -------
38

39     public static synchronized PropertyEditor findEditor(FormProperty property) {
40         Class JavaDoc propType = property.getValueType();
41         FormModel form = property.getPropertyContext().getFormModel();
42         Class JavaDoc[] edClasses = findEditorClasses(propType, form);
43         if (edClasses.length > 0) {
44             PropertyEditor[] editors =
45                 createEditorInstances(new Class JavaDoc[] { edClasses[0] }, propType);
46             if (editors.length > 0)
47                 return editors[0];
48         }
49         return null;
50     }
51
52     public static synchronized PropertyEditor[] getAllEditors(FormProperty property) {
53         Class JavaDoc propType = property.getValueType();
54         FormModel form = property.getPropertyContext().getFormModel();
55         return createEditorInstances(findEditorClasses(propType, form), propType);
56     }
57
58     public static synchronized void registerEditor(Class JavaDoc propertyType,
59                                                    Class JavaDoc editorClass)
60     {
61         Class JavaDoc[] currentEditors = (Class JavaDoc[])expliciteEditors.get(getTypeName(propertyType));
62         Class JavaDoc[] newEditors;
63         if (currentEditors == null) {
64             newEditors = new Class JavaDoc[1];
65             newEditors[0] = editorClass;
66         }
67         else {
68             // check whether the editor is not already registered
69
for (int i = 0; i < currentEditors.length; i++) {
70                 if (currentEditors[i].equals(editorClass)) {
71                     return; // do nothing in such case
72
}
73             }
74             newEditors = new Class JavaDoc[currentEditors.length + 1];
75             System.arraycopy(currentEditors, 0, newEditors, 0, currentEditors.length);
76             newEditors[newEditors.length - 1] = editorClass;
77         }
78         expliciteEditors.put(getTypeName(propertyType), newEditors);
79     }
80
81     synchronized static void clearEditorsCache() {
82         editorsCache.clear();
83     }
84
85     // -------
86

87     private static String JavaDoc getTypeName(Class JavaDoc type) {
88         String JavaDoc typeName = type.getName();
89         if (type.isPrimitive()) {
90             if (Byte.TYPE.equals(type)) typeName = "byte"; // NOI18N
91
else if (Short.TYPE.equals(type)) typeName = "short"; // NOI18N
92
else if (Integer.TYPE.equals(type)) typeName = "integer"; // NOI18N
93
else if (Long.TYPE.equals(type)) typeName = "long"; // NOI18N
94
else if (Boolean.TYPE.equals(type)) typeName = "boolean"; // NOI18N
95
else if (Float.TYPE.equals(type)) typeName = "float"; // NOI18N
96
else if (Double.TYPE.equals(type)) typeName = "double"; // NOI18N
97
else if (Character.TYPE.equals(type)) typeName = "char"; // NOI18N
98
}
99         return typeName;
100     }
101
102     private static Class JavaDoc[] findEditorClasses(Class JavaDoc type, FormModel form) {
103         // try the editors cache
104
Class JavaDoc[] edClasses = (Class JavaDoc[]) editorsCache.get(type);
105         if (edClasses != null)
106             return edClasses;
107
108         FormLoaderSettings formSettings = FormLoaderSettings.getInstance();
109
110         ArrayList JavaDoc editorsList = new ArrayList JavaDoc(5);
111
112         // 1st - try standard way through PropertyEditorManager
113
PropertyEditor stdPropEd = (type == Object JavaDoc.class || type == java.awt.Font JavaDoc.class) ? null :
114                                      PropertyEditorManager.findEditor(type);
115         if (stdPropEd != null) {
116             editorsList.add(stdPropEd.getClass());
117         }
118         else {
119             // 2nd - try the form editor's specific search path
120
String JavaDoc editorName = type.getName();
121             if (!editorName.startsWith("[")) { // not an array type // NOI18N
122
int dot = editorName.lastIndexOf('.');
123                 if (dot > 0)
124                     editorName = editorName.substring(dot+1);
125
126                 String JavaDoc[] searchPath = formSettings.getEditorSearchPath();
127                 for (int i = 0; i < searchPath.length; i++) {
128                     String JavaDoc name = searchPath[i] + "." + editorName + "Editor"; // NOI18N
129
try {
130                         Class JavaDoc edClass = FormUtils.loadClass(name, form);
131                         editorsList.add(edClass);
132                         break; // stop on first found editor
133
}
134                     catch (Exception JavaDoc e) {} // silently ignore
135
catch (LinkageError JavaDoc e) {} // silently ignore
136
}
137             }
138         }
139
140         // 3rd - search in explicitly registered editors (in Options)
141
String JavaDoc typeName = getTypeName(type);
142         String JavaDoc[][] registered = formSettings.getRegisteredEditors();
143         for (int i = 0; i < registered.length; i++) {
144             String JavaDoc[] typereg = registered[i];
145             if ((typereg != null) && (typereg.length > 0)) {
146                 if (typereg[0].equals(typeName)) {
147                     for (int j = 1; j < typereg.length; j++) {
148                         try {
149                             Class JavaDoc edClass = FormUtils.loadClass(typereg[j], form);
150                             if (!editorsList.contains(edClass))
151                                 editorsList.add(edClass);
152                         }
153                         catch (Exception JavaDoc e) { // ignore
154
org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
155                         }
156                     }
157                 }
158             }
159         }
160
161         // 4th - search in editors registered using registerEditor(...)
162
Class JavaDoc[] explicite = (Class JavaDoc[]) expliciteEditors.get(typeName);
163         if (explicite != null) {
164             for (int i = 0; i < explicite.length; i++) {
165                 Class JavaDoc edClass = explicite[i];
166                 if (!editorsList.contains(edClass))
167                     editorsList.add(edClass);
168             }
169         }
170
171         // 5th - experimental: add ComponentChooserEditor for Components
172
if (java.awt.Component JavaDoc.class.isAssignableFrom(type))
173             editorsList.add(ComponentChooserEditor.class);
174
175         // 6th - add the RADConnectionPropertyEditor for all values
176
editorsList.add(RADConnectionPropertyEditor.class);
177
178         edClasses = new Class JavaDoc[editorsList.size()];
179         editorsList.toArray(edClasses);
180         editorsCache.put(type, edClasses);
181
182         return edClasses;
183     }
184
185     private static PropertyEditor[] createEditorInstances(Class JavaDoc[] edClasses,
186                                                           Class JavaDoc propertyType) {
187         ArrayList JavaDoc instancesList = new ArrayList JavaDoc(edClasses.length);
188
189         for (int i = 0; i < edClasses.length; i++) {
190             Class JavaDoc edType = edClasses[i];
191             if (RADConnectionPropertyEditor.class.isAssignableFrom(edType)) {
192                 instancesList.add(new RADConnectionPropertyEditor(propertyType));
193             }
194             else if (ComponentChooserEditor.class.isAssignableFrom(edType)) {
195                 instancesList.add(new ComponentChooserEditor(
196                                         new Class JavaDoc[] { propertyType }));
197             }
198             else if (PropertyEditor.class.isAssignableFrom(edType)) {
199                 try {
200                     instancesList.add(edType.newInstance());
201                 }
202                 catch (Exception JavaDoc e) { // ignore
203
org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
204                 }
205             }
206             // ignore classes which do not implement java.beans.PropertyEditor
207
}
208
209         PropertyEditor[] editors = new PropertyEditor[instancesList.size()];
210         instancesList.toArray(editors);
211         return editors;
212     }
213 }
214
Popular Tags