001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.beans;
027
028 import sun.beans.editors.*;
029
030 /**
031 * The PropertyEditorManager can be used to locate a property editor for
032 * any given type name. This property editor must support the
033 * java.beans.PropertyEditor interface for editing a given object.
034 * <P>
035 * The PropertyEditorManager uses three techniques for locating an editor
036 * for a given type. First, it provides a registerEditor method to allow
037 * an editor to be specifically registered for a given type. Second it
038 * tries to locate a suitable class by adding "Editor" to the full
039 * qualified classname of the given type (e.g. "foo.bah.FozEditor").
040 * Finally it takes the simple classname (without the package name) adds
041 * "Editor" to it and looks in a search-path of packages for a matching
042 * class.
043 * <P>
044 * So for an input class foo.bah.Fred, the PropertyEditorManager would
045 * first look in its tables to see if an editor had been registered for
046 * foo.bah.Fred and if so use that. Then it will look for a
047 * foo.bah.FredEditor class. Then it will look for (say)
048 * standardEditorsPackage.FredEditor class.
049 * <p>
050 * Default PropertyEditors will be provided for the Java primitive types
051 * "boolean", "byte", "short", "int", "long", "float", and "double"; and
052 * for the classes java.lang.String. java.awt.Color, and java.awt.Font.
053 */
054
055 public class PropertyEditorManager {
056
057 /**
058 * Register an editor class to be used to edit values of
059 * a given target class.
060 *
061 * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
062 * method is called. This could result in a SecurityException.
063 *
064 * @param targetType the Class object of the type to be edited
065 * @param editorClass the Class object of the editor class. If
066 * this is null, then any existing definition will be removed.
067 * @exception SecurityException if a security manager exists and its
068 * <code>checkPropertiesAccess</code> method doesn't allow setting
069 * of system properties.
070 * @see SecurityManager#checkPropertiesAccess
071 */
072
073 public static void registerEditor(Class<?> targetType,
074 Class<?> editorClass) {
075 SecurityManager sm = System.getSecurityManager();
076 if (sm != null) {
077 sm.checkPropertiesAccess();
078 }
079 initialize();
080 if (editorClass == null) {
081 registry.remove(targetType);
082 } else {
083 registry.put(targetType, editorClass);
084 }
085 }
086
087 /**
088 * Locate a value editor for a given target type.
089 *
090 * @param targetType The Class object for the type to be edited
091 * @return An editor object for the given target class.
092 * The result is null if no suitable editor can be found.
093 */
094
095 public static synchronized PropertyEditor findEditor(
096 Class<?> targetType) {
097 initialize();
098 Class editorClass = (Class) registry.get(targetType);
099 if (editorClass != null) {
100 try {
101 Object o = editorClass.newInstance();
102 return (PropertyEditor) o;
103 } catch (Exception ex) {
104 System.err
105 .println("Couldn't instantiate type editor \""
106 + editorClass.getName() + "\" : " + ex);
107 }
108 }
109
110 // Now try adding "Editor" to the class name.
111
112 String editorName = targetType.getName() + "Editor";
113 try {
114 return (PropertyEditor) Introspector.instantiate(
115 targetType, editorName);
116 } catch (Exception ex) {
117 // Silently ignore any errors.
118 }
119
120 // Now try looking for <searchPath>.fooEditor
121 int index = editorName.lastIndexOf('.') + 1;
122 if (index > 0) {
123 editorName = editorName.substring(index);
124 }
125 for (String path : searchPath) {
126 String name = path + '.' + editorName;
127 try {
128 return (PropertyEditor) Introspector.instantiate(
129 targetType, name);
130 } catch (Exception ex) {
131 // Silently ignore any errors.
132 }
133 }
134
135 if (null != targetType.getEnumConstants()) {
136 return new EnumEditor(targetType);
137 }
138 // We couldn't find a suitable Editor.
139 return null;
140 }
141
142 /**
143 * Gets the package names that will be searched for property editors.
144 *
145 * @return The array of package names that will be searched in
146 * order to find property editors.
147 * <p> The default value for this array is implementation-dependent,
148 * e.g. Sun implementation initially sets to {"sun.beans.editors"}.
149 */
150 public static synchronized String[] getEditorSearchPath() {
151 // Return a copy of the searchPath.
152 String result[] = new String[searchPath.length];
153 System.arraycopy(searchPath, 0, result, 0, searchPath.length);
154 return result;
155 }
156
157 /**
158 * Change the list of package names that will be used for
159 * finding property editors.
160 *
161 * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
162 * method is called. This could result in a SecurityException.
163 *
164 * @param path Array of package names.
165 * @exception SecurityException if a security manager exists and its
166 * <code>checkPropertiesAccess</code> method doesn't allow setting
167 * of system properties.
168 * @see SecurityManager#checkPropertiesAccess
169 */
170
171 public static synchronized void setEditorSearchPath(String path[]) {
172 SecurityManager sm = System.getSecurityManager();
173 if (sm != null) {
174 sm.checkPropertiesAccess();
175 }
176 if (path == null) {
177 path = new String[0];
178 }
179 searchPath = path;
180 }
181
182 private static synchronized void initialize() {
183 if (registry != null) {
184 return;
185 }
186 registry = new java.util.Hashtable();
187 registry.put(Byte.TYPE, ByteEditor.class);
188 registry.put(Short.TYPE, ShortEditor.class);
189 registry.put(Integer.TYPE, IntegerEditor.class);
190 registry.put(Long.TYPE, LongEditor.class);
191 registry.put(Boolean.TYPE, BooleanEditor.class);
192 registry.put(Float.TYPE, FloatEditor.class);
193 registry.put(Double.TYPE, DoubleEditor.class);
194 }
195
196 private static String[] searchPath = { "sun.beans.editors" };
197 private static java.util.Hashtable registry;
198 }
|