PropertyEditorManager.java revision 0
0N/A/*
0N/A * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage java.beans;
0N/A
0N/Aimport sun.beans.editors.*;
0N/A
0N/A/**
0N/A * The PropertyEditorManager can be used to locate a property editor for
0N/A * any given type name. This property editor must support the
0N/A * java.beans.PropertyEditor interface for editing a given object.
0N/A * <P>
0N/A * The PropertyEditorManager uses three techniques for locating an editor
0N/A * for a given type. First, it provides a registerEditor method to allow
0N/A * an editor to be specifically registered for a given type. Second it
0N/A * tries to locate a suitable class by adding "Editor" to the full
0N/A * qualified classname of the given type (e.g. "foo.bah.FozEditor").
0N/A * Finally it takes the simple classname (without the package name) adds
0N/A * "Editor" to it and looks in a search-path of packages for a matching
0N/A * class.
0N/A * <P>
0N/A * So for an input class foo.bah.Fred, the PropertyEditorManager would
0N/A * first look in its tables to see if an editor had been registered for
0N/A * foo.bah.Fred and if so use that. Then it will look for a
0N/A * foo.bah.FredEditor class. Then it will look for (say)
0N/A * standardEditorsPackage.FredEditor class.
0N/A * <p>
0N/A * Default PropertyEditors will be provided for the Java primitive types
0N/A * "boolean", "byte", "short", "int", "long", "float", and "double"; and
0N/A * for the classes java.lang.String. java.awt.Color, and java.awt.Font.
0N/A */
0N/A
0N/Apublic class PropertyEditorManager {
0N/A
0N/A /**
0N/A * Register an editor class to be used to edit values of
0N/A * a given target class.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
0N/A * method is called. This could result in a SecurityException.
0N/A *
0N/A * @param targetType the Class object of the type to be edited
0N/A * @param editorClass the Class object of the editor class. If
0N/A * this is null, then any existing definition will be removed.
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkPropertiesAccess</code> method doesn't allow setting
0N/A * of system properties.
0N/A * @see SecurityManager#checkPropertiesAccess
0N/A */
0N/A
0N/A public static void registerEditor(Class<?> targetType, Class<?> editorClass) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPropertiesAccess();
0N/A }
0N/A initialize();
0N/A if (editorClass == null) {
0N/A registry.remove(targetType);
0N/A } else {
0N/A registry.put(targetType, editorClass);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Locate a value editor for a given target type.
0N/A *
0N/A * @param targetType The Class object for the type to be edited
0N/A * @return An editor object for the given target class.
0N/A * The result is null if no suitable editor can be found.
0N/A */
0N/A
0N/A public static synchronized PropertyEditor findEditor(Class<?> targetType) {
0N/A initialize();
0N/A Class editorClass = (Class)registry.get(targetType);
0N/A if (editorClass != null) {
0N/A try {
0N/A Object o = editorClass.newInstance();
0N/A return (PropertyEditor)o;
0N/A } catch (Exception ex) {
0N/A System.err.println("Couldn't instantiate type editor \"" +
0N/A editorClass.getName() + "\" : " + ex);
0N/A }
0N/A }
0N/A
0N/A // Now try adding "Editor" to the class name.
0N/A
0N/A String editorName = targetType.getName() + "Editor";
0N/A try {
0N/A return (PropertyEditor) Introspector.instantiate(targetType, editorName);
0N/A } catch (Exception ex) {
0N/A // Silently ignore any errors.
0N/A }
0N/A
0N/A // Now try looking for <searchPath>.fooEditor
0N/A int index = editorName.lastIndexOf('.') + 1;
0N/A if (index > 0) {
0N/A editorName = editorName.substring(index);
0N/A }
0N/A for (String path : searchPath) {
0N/A String name = path + '.' + editorName;
0N/A try {
0N/A return (PropertyEditor) Introspector.instantiate(targetType, name);
0N/A } catch (Exception ex) {
0N/A // Silently ignore any errors.
0N/A }
0N/A }
0N/A
0N/A if (null != targetType.getEnumConstants()) {
0N/A return new EnumEditor(targetType);
0N/A }
0N/A // We couldn't find a suitable Editor.
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Gets the package names that will be searched for property editors.
0N/A *
0N/A * @return The array of package names that will be searched in
0N/A * order to find property editors.
0N/A * <p> The default value for this array is implementation-dependent,
0N/A * e.g. Sun implementation initially sets to {"sun.beans.editors"}.
0N/A */
0N/A public static synchronized String[] getEditorSearchPath() {
0N/A // Return a copy of the searchPath.
0N/A String result[] = new String[searchPath.length];
0N/A System.arraycopy(searchPath, 0, result, 0, searchPath.length);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Change the list of package names that will be used for
0N/A * finding property editors.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
0N/A * method is called. This could result in a SecurityException.
0N/A *
0N/A * @param path Array of package names.
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkPropertiesAccess</code> method doesn't allow setting
0N/A * of system properties.
0N/A * @see SecurityManager#checkPropertiesAccess
0N/A */
0N/A
0N/A public static synchronized void setEditorSearchPath(String path[]) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPropertiesAccess();
0N/A }
0N/A if (path == null) {
0N/A path = new String[0];
0N/A }
0N/A searchPath = path;
0N/A }
0N/A
0N/A private static synchronized void initialize() {
0N/A if (registry != null) {
0N/A return;
0N/A }
0N/A registry = new java.util.Hashtable();
0N/A registry.put(Byte.TYPE, ByteEditor.class);
0N/A registry.put(Short.TYPE, ShortEditor.class);
0N/A registry.put(Integer.TYPE, IntegerEditor.class);
0N/A registry.put(Long.TYPE, LongEditor.class);
0N/A registry.put(Boolean.TYPE, BooleanEditor.class);
0N/A registry.put(Float.TYPE, FloatEditor.class);
0N/A registry.put(Double.TYPE, DoubleEditor.class);
0N/A }
0N/A
0N/A private static String[] searchPath = { "sun.beans.editors" };
0N/A private static java.util.Hashtable registry;
0N/A}