jdk/src/share/classes/java/beans/PropertyEditorManager.java
author malenkov
Thu, 18 Feb 2010 17:46:40 +0300
changeset 4960 99ac74ca2f2f
parent 3239 f675984c2349
child 5506 202f599c92aa
permissions -rw-r--r--
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes Reviewed-by: peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
     2
 * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    28
import com.sun.beans.finder.PropertyEditorFinder;
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    29
import sun.awt.AppContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The PropertyEditorManager can be used to locate a property editor for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * any given type name.  This property editor must support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * java.beans.PropertyEditor interface for editing a given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The PropertyEditorManager uses three techniques for locating an editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * for a given type.  First, it provides a registerEditor method to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * an editor to be specifically registered for a given type.  Second it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * tries to locate a suitable class by adding "Editor" to the full
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * qualified classname of the given type (e.g. "foo.bah.FozEditor").
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Finally it takes the simple classname (without the package name) adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * "Editor" to it and looks in a search-path of packages for a matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * So for an input class foo.bah.Fred, the PropertyEditorManager would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * first look in its tables to see if an editor had been registered for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * foo.bah.Fred and if so use that.  Then it will look for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * foo.bah.FredEditor class.  Then it will look for (say)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * standardEditorsPackage.FredEditor class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Default PropertyEditors will be provided for the Java primitive types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * "boolean", "byte", "short", "int", "long", "float", and "double"; and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * for the classes java.lang.String. java.awt.Color, and java.awt.Font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class PropertyEditorManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    58
    private static final Object FINDER_KEY = new Object();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    61
     * Registers an editor class to edit values of the given target class.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    62
     * If the editor class is {@code null},
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    63
     * then any existing definition will be removed.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    64
     * Thus this method can be used to cancel the registration.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    65
     * The registration is canceled automatically
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    66
     * if either the target or editor class is unloaded.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    67
     * <p>
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    68
     * If there is a security manager, its {@code checkPropertiesAccess}
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    69
     * method is called. This could result in a {@linkplain SecurityException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    71
     * @param targetType   the class object of the type to be edited
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    72
     * @param editorClass  the class object of the editor class
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    73
     * @throws SecurityException  if a security manager exists and
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    74
     *                            its {@code checkPropertiesAccess} method
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    75
     *                            doesn't allow setting of system properties
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    76
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @see SecurityManager#checkPropertiesAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    79
    public static void registerEditor(Class<?> targetType, Class<?> editorClass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            sm.checkPropertiesAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    84
        PropertyEditorFinder finder = getFinder();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    85
        synchronized (finder) {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    86
            finder.register(targetType, editorClass);
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    87
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Locate a value editor for a given target type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param targetType  The Class object for the type to be edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return An editor object for the given target class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The result is null if no suitable editor can be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    97
    public static PropertyEditor findEditor(Class<?> targetType) {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    98
        PropertyEditorFinder finder = getFinder();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
    99
        synchronized (finder) {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   100
            return finder.find(targetType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Gets the package names that will be searched for property editors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return  The array of package names that will be searched in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *          order to find property editors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>     The default value for this array is implementation-dependent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *         e.g. Sun implementation initially sets to  {"sun.beans.editors"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   112
    public static String[] getEditorSearchPath() {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   113
        PropertyEditorFinder finder = getFinder();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   114
        synchronized (finder) {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   115
            return finder.getPackages();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   116
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Change the list of package names that will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *          finding property editors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * method is called. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param path  Array of package names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *             <code>checkPropertiesAccess</code> method doesn't allow setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *              of system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see SecurityManager#checkPropertiesAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   132
    public static void setEditorSearchPath(String[] path) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            sm.checkPropertiesAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   137
        PropertyEditorFinder finder = getFinder();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   138
        synchronized (finder) {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   139
            finder.setPackages(path);
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   140
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   143
    private static PropertyEditorFinder getFinder() {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   144
        AppContext context = AppContext.getAppContext();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   145
        Object object = context.get(FINDER_KEY);
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   146
        if (object instanceof PropertyEditorFinder) {
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   147
            return (PropertyEditorFinder) object;
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   148
        }
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   149
        PropertyEditorFinder finder = new PropertyEditorFinder();
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   150
        context.put(FINDER_KEY, finder);
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 1308
diff changeset
   151
        return finder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}