KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > beans > PropertyEditorManager


1 /*
2  * @(#)PropertyEditorManager.java 1.45 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 /**
11  * The PropertyEditorManager can be used to locate a property editor for
12  * any given type name. This property editor must support the
13  * java.beans.PropertyEditor interface for editing a given object.
14  * <P>
15  * The PropertyEditorManager uses three techniques for locating an editor
16  * for a given type. First, it provides a registerEditor method to allow
17  * an editor to be specifically registered for a given type. Second it
18  * tries to locate a suitable class by adding "Editor" to the full
19  * qualified classname of the given type (e.g. "foo.bah.FozEditor").
20  * Finally it takes the simple classname (without the package name) adds
21  * "Editor" to it and looks in a search-path of packages for a matching
22  * class.
23  * <P>
24  * So for an input class foo.bah.Fred, the PropertyEditorManager would
25  * first look in its tables to see if an editor had been registered for
26  * foo.bah.Fred and if so use that. Then it will look for a
27  * foo.bah.FredEditor class. Then it will look for (say)
28  * standardEditorsPackage.FredEditor class.
29  * <p>
30  * Default PropertyEditors will be provided for the Java primitive types
31  * "boolean", "byte", "short", "int", "long", "float", and "double"; and
32  * for the classes java.lang.String. java.awt.Color, and java.awt.Font.
33  */

34
35 public class PropertyEditorManager {
36
37     /**
38      * Register an editor class to be used to edit values of
39      * a given target class.
40      *
41      * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
42      * method is called. This could result in a SecurityException.
43      *
44      * @param targetType the Class object of the type to be edited
45      * @param editorClass the Class object of the editor class. If
46      * this is null, then any existing definition will be removed.
47      * @exception SecurityException if a security manager exists and its
48      * <code>checkPropertiesAccess</code> method doesn't allow setting
49      * of system properties.
50      * @see SecurityManager#checkPropertiesAccess
51      */

52
53     public static void registerEditor(Class JavaDoc<?> targetType, Class JavaDoc<?> editorClass) {
54     SecurityManager JavaDoc sm = System.getSecurityManager();
55     if (sm != null) {
56         sm.checkPropertiesAccess();
57     }
58     initialize();
59     if (editorClass == null) {
60         registry.remove(targetType);
61     } else {
62         registry.put(targetType, editorClass);
63     }
64     }
65
66     /**
67      * Locate a value editor for a given target type.
68      *
69      * @param targetType The Class object for the type to be edited
70      * @return An editor object for the given target class.
71      * The result is null if no suitable editor can be found.
72      */

73
74     public static synchronized PropertyEditor JavaDoc findEditor(Class JavaDoc<?> targetType) {
75     initialize();
76     Class JavaDoc editorClass = (Class JavaDoc)registry.get(targetType);
77     if (editorClass != null) {
78         try {
79         Object JavaDoc o = editorClass.newInstance();
80             return (PropertyEditor JavaDoc)o;
81         } catch (Exception JavaDoc ex) {
82         System.err.println("Couldn't instantiate type editor \"" +
83             editorClass.getName() + "\" : " + ex);
84         }
85     }
86
87     // Now try adding "Editor" to the class name.
88

89     String JavaDoc editorName = targetType.getName() + "Editor";
90     try {
91         return (PropertyEditor JavaDoc) Introspector.instantiate(targetType, editorName);
92     } catch (Exception JavaDoc ex) {
93        // Silently ignore any errors.
94
}
95
96     // Now try looking for <searchPath>.fooEditor
97
editorName = targetType.getName();
98     while (editorName.indexOf('.') > 0) {
99         editorName = editorName.substring(editorName.indexOf('.')+1);
100     }
101     for (int i = 0; i < searchPath.length; i++) {
102         String JavaDoc name = searchPath[i] + "." + editorName + "Editor";
103         try {
104             return (PropertyEditor JavaDoc) Introspector.instantiate(targetType, name);
105         } catch (Exception JavaDoc ex) {
106            // Silently ignore any errors.
107
}
108     }
109
110     // We couldn't find a suitable Editor.
111
return null;
112     }
113
114     /**
115      * Gets the package names that will be searched for property editors.
116      *
117      * @return The array of package names that will be searched in
118      * order to find property editors.
119      * <p> The default value for this array is implementation-dependent,
120      * e.g. Sun implementation initially sets to {"sun.beans.editors"}.
121      */

122     public static synchronized String JavaDoc[] getEditorSearchPath() {
123     // Return a copy of the searchPath.
124
String JavaDoc result[] = new String JavaDoc[searchPath.length];
125     for (int i = 0; i < searchPath.length; i++) {
126         result[i] = searchPath[i];
127     }
128     return result;
129     }
130
131     /**
132      * Change the list of package names that will be used for
133      * finding property editors.
134      *
135      * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
136      * method is called. This could result in a SecurityException.
137      *
138      * @param path Array of package names.
139      * @exception SecurityException if a security manager exists and its
140      * <code>checkPropertiesAccess</code> method doesn't allow setting
141      * of system properties.
142      * @see SecurityManager#checkPropertiesAccess
143      */

144
145     public static synchronized void setEditorSearchPath(String JavaDoc path[]) {
146     SecurityManager JavaDoc sm = System.getSecurityManager();
147     if (sm != null) {
148         sm.checkPropertiesAccess();
149     }
150     initialize();
151     if (path == null) {
152         path = new String JavaDoc[0];
153     }
154     searchPath = path;
155     }
156
157     private static synchronized void load(Class JavaDoc targetType, String JavaDoc name) {
158     String JavaDoc editorName = name;
159     for (int i = 0; i < searchPath.length; i++) {
160         try {
161             editorName = searchPath[i] + "." + name;
162             Class JavaDoc cls = Class.forName(editorName);
163             registry.put(targetType, cls);
164         return;
165         } catch (Exception JavaDoc ex) {
166         // Drop through and try next package.
167
}
168     }
169     // This shouldn't happen.
170
System.err.println("load of " + editorName + " failed");
171     }
172
173
174     private static synchronized void initialize() {
175     if (registry != null) {
176         return;
177     }
178     registry = new java.util.Hashtable JavaDoc();
179     load(Byte.TYPE, "ByteEditor");
180     load(Short.TYPE, "ShortEditor");
181     load(Integer.TYPE, "IntEditor");
182     load(Long.TYPE ,"LongEditor");
183     load(Boolean.TYPE, "BoolEditor");
184     load(Float.TYPE, "FloatEditor");
185     load(Double.TYPE, "DoubleEditor");
186     }
187
188     private static String JavaDoc[] searchPath = { "sun.beans.editors" };
189     private static java.util.Hashtable JavaDoc registry;
190 }
191
Popular Tags