jdk/src/share/classes/java/beans/PropertyEditorManager.java
changeset 1308 db0929bb4cd4
parent 2 90ce3da70b43
child 3239 f675984c2349
equal deleted inserted replaced
1307:7603aea0ec0e 1308:db0929bb4cd4
     1 /*
     1 /*
     2  * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 1996-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
    23  * have any questions.
    23  * have any questions.
    24  */
    24  */
    25 
    25 
    26 package java.beans;
    26 package java.beans;
    27 
    27 
       
    28 import com.sun.beans.WeakCache;
    28 import sun.beans.editors.*;
    29 import sun.beans.editors.*;
    29 
    30 
    30 /**
    31 /**
    31  * The PropertyEditorManager can be used to locate a property editor for
    32  * The PropertyEditorManager can be used to locate a property editor for
    32  * any given type name.  This property editor must support the
    33  * any given type name.  This property editor must support the
    53  */
    54  */
    54 
    55 
    55 public class PropertyEditorManager {
    56 public class PropertyEditorManager {
    56 
    57 
    57     /**
    58     /**
    58      * Register an editor class to be used to edit values of
    59      * Registers an editor class to edit values of the given target class.
    59      * a given target class.
    60      * If the editor class is {@code null},
       
    61      * then any existing definition will be removed.
       
    62      * Thus this method can be used to cancel the registration.
       
    63      * The registration is canceled automatically
       
    64      * if either the target or editor class is unloaded.
       
    65      * <p>
       
    66      * If there is a security manager, its {@code checkPropertiesAccess}
       
    67      * method is called. This could result in a {@linkplain SecurityException}.
    60      *
    68      *
    61      * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
    69      * @param targetType   the class object of the type to be edited
    62      * method is called. This could result in a SecurityException.
    70      * @param editorClass  the class object of the editor class
       
    71      * @throws SecurityException  if a security manager exists and
       
    72      *                            its {@code checkPropertiesAccess} method
       
    73      *                            doesn't allow setting of system properties
    63      *
    74      *
    64      * @param targetType the Class object of the type to be edited
       
    65      * @param editorClass the Class object of the editor class.  If
       
    66      *     this is null, then any existing definition will be removed.
       
    67      * @exception  SecurityException  if a security manager exists and its
       
    68      *             <code>checkPropertiesAccess</code> method doesn't allow setting
       
    69      *              of system properties.
       
    70      * @see SecurityManager#checkPropertiesAccess
    75      * @see SecurityManager#checkPropertiesAccess
    71      */
    76      */
    72 
    77     public static synchronized void registerEditor(Class<?> targetType, Class<?> editorClass) {
    73     public static void registerEditor(Class<?> targetType, Class<?> editorClass) {
       
    74         SecurityManager sm = System.getSecurityManager();
    78         SecurityManager sm = System.getSecurityManager();
    75         if (sm != null) {
    79         if (sm != null) {
    76             sm.checkPropertiesAccess();
    80             sm.checkPropertiesAccess();
    77         }
    81         }
    78         initialize();
    82         registry.put(targetType, editorClass);
    79         if (editorClass == null) {
       
    80             registry.remove(targetType);
       
    81         } else {
       
    82             registry.put(targetType, editorClass);
       
    83         }
       
    84     }
    83     }
    85 
    84 
    86     /**
    85     /**
    87      * Locate a value editor for a given target type.
    86      * Locate a value editor for a given target type.
    88      *
    87      *
    89      * @param targetType  The Class object for the type to be edited
    88      * @param targetType  The Class object for the type to be edited
    90      * @return An editor object for the given target class.
    89      * @return An editor object for the given target class.
    91      * The result is null if no suitable editor can be found.
    90      * The result is null if no suitable editor can be found.
    92      */
    91      */
    93 
       
    94     public static synchronized PropertyEditor findEditor(Class<?> targetType) {
    92     public static synchronized PropertyEditor findEditor(Class<?> targetType) {
    95         initialize();
    93         Class editorClass = registry.get(targetType);
    96         Class editorClass = (Class)registry.get(targetType);
       
    97         if (editorClass != null) {
    94         if (editorClass != null) {
    98             try {
    95             try {
    99                 Object o = editorClass.newInstance();
    96                 Object o = editorClass.newInstance();
   100                 return (PropertyEditor)o;
    97                 return (PropertyEditor)o;
   101             } catch (Exception ex) {
    98             } catch (Exception ex) {
   141      *          order to find property editors.
   138      *          order to find property editors.
   142      * <p>     The default value for this array is implementation-dependent,
   139      * <p>     The default value for this array is implementation-dependent,
   143      *         e.g. Sun implementation initially sets to  {"sun.beans.editors"}.
   140      *         e.g. Sun implementation initially sets to  {"sun.beans.editors"}.
   144      */
   141      */
   145     public static synchronized String[] getEditorSearchPath() {
   142     public static synchronized String[] getEditorSearchPath() {
   146         // Return a copy of the searchPath.
   143         return searchPath.clone();
   147         String result[] = new String[searchPath.length];
       
   148         System.arraycopy(searchPath, 0, result, 0, searchPath.length);
       
   149         return result;
       
   150     }
   144     }
   151 
   145 
   152     /**
   146     /**
   153      * Change the list of package names that will be used for
   147      * Change the list of package names that will be used for
   154      *          finding property editors.
   148      *          finding property editors.
   160      * @exception  SecurityException  if a security manager exists and its
   154      * @exception  SecurityException  if a security manager exists and its
   161      *             <code>checkPropertiesAccess</code> method doesn't allow setting
   155      *             <code>checkPropertiesAccess</code> method doesn't allow setting
   162      *              of system properties.
   156      *              of system properties.
   163      * @see SecurityManager#checkPropertiesAccess
   157      * @see SecurityManager#checkPropertiesAccess
   164      */
   158      */
   165 
   159     public static synchronized void setEditorSearchPath(String[] path) {
   166     public static synchronized void setEditorSearchPath(String path[]) {
       
   167         SecurityManager sm = System.getSecurityManager();
   160         SecurityManager sm = System.getSecurityManager();
   168         if (sm != null) {
   161         if (sm != null) {
   169             sm.checkPropertiesAccess();
   162             sm.checkPropertiesAccess();
   170         }
   163         }
   171         if (path == null) {
   164         searchPath = (path != null)
   172             path = new String[0];
   165                 ? path.clone()
   173         }
   166                 : EMPTY;
   174         searchPath = path;
       
   175     }
   167     }
   176 
   168 
   177     private static synchronized void initialize() {
   169     private static String[] searchPath = { "sun.beans.editors" };
   178         if (registry != null) {
   170     private static final String[] EMPTY = {};
   179             return;
   171     private static final WeakCache<Class<?>, Class<?>> registry;
   180         }
   172 
   181         registry = new java.util.Hashtable();
   173     static {
       
   174         registry = new WeakCache<Class<?>, Class<?>>();
   182         registry.put(Byte.TYPE, ByteEditor.class);
   175         registry.put(Byte.TYPE, ByteEditor.class);
   183         registry.put(Short.TYPE, ShortEditor.class);
   176         registry.put(Short.TYPE, ShortEditor.class);
   184         registry.put(Integer.TYPE, IntegerEditor.class);
   177         registry.put(Integer.TYPE, IntegerEditor.class);
   185         registry.put(Long.TYPE, LongEditor.class);
   178         registry.put(Long.TYPE, LongEditor.class);
   186         registry.put(Boolean.TYPE, BooleanEditor.class);
   179         registry.put(Boolean.TYPE, BooleanEditor.class);
   187         registry.put(Float.TYPE, FloatEditor.class);
   180         registry.put(Float.TYPE, FloatEditor.class);
   188         registry.put(Double.TYPE, DoubleEditor.class);
   181         registry.put(Double.TYPE, DoubleEditor.class);
   189     }
   182     }
   190 
       
   191     private static String[] searchPath = { "sun.beans.editors" };
       
   192     private static java.util.Hashtable registry;
       
   193 }
   183 }