Showing the changes to java/pkg/java/beans/PropertyEditorManager.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,141 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 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 | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package java.beans; | |
| 27 | + | |
| 28 | +import com.sun.beans.finder.PropertyEditorFinder; | |
| 29 | +import sun.awt.AppContext; | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * The PropertyEditorManager can be used to locate a property editor for | |
| 33 | + * any given type name. This property editor must support the | |
| 34 | + * java.beans.PropertyEditor interface for editing a given object. | |
| 35 | + * <P> | |
| 36 | + * The PropertyEditorManager uses three techniques for locating an editor | |
| 37 | + * for a given type. First, it provides a registerEditor method to allow | |
| 38 | + * an editor to be specifically registered for a given type. Second it | |
| 39 | + * tries to locate a suitable class by adding "Editor" to the full | |
| 40 | + * qualified classname of the given type (e.g. "foo.bah.FozEditor"). | |
| 41 | + * Finally it takes the simple classname (without the package name) adds | |
| 42 | + * "Editor" to it and looks in a search-path of packages for a matching | |
| 43 | + * class. | |
| 44 | + * <P> | |
| 45 | + * So for an input class foo.bah.Fred, the PropertyEditorManager would | |
| 46 | + * first look in its tables to see if an editor had been registered for | |
| 47 | + * foo.bah.Fred and if so use that. Then it will look for a | |
| 48 | + * foo.bah.FredEditor class. Then it will look for (say) | |
| 49 | + * standardEditorsPackage.FredEditor class. | |
| 50 | + * <p> | |
| 51 | + * Default PropertyEditors will be provided for the Java primitive types | |
| 52 | + * "boolean", "byte", "short", "int", "long", "float", and "double"; and | |
| 53 | + * for the classes java.lang.String. java.awt.Color, and java.awt.Font. | |
| 54 | + */ | |
| 55 | + | |
| 56 | +public class PropertyEditorManager { | |
| 57 | + | |
| 58 | + private static final Object FINDER_KEY = new Object(); | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Registers an editor class to edit values of the given target class. | |
| 62 | + * If the editor class is {@code null}, | |
| 63 | + * then any existing definition will be removed. | |
| 64 | + * Thus this method can be used to cancel the registration. | |
| 65 | + * The registration is canceled automatically | |
| 66 | + * if either the target or editor class is unloaded. | |
| 67 | + * <p> | |
| 68 | + * If there is a security manager, its {@code checkPropertiesAccess} | |
| 69 | + * method is called. This could result in a {@linkplain SecurityException}. | |
| 70 | + * | |
| 71 | + * @param targetType the class object of the type to be edited | |
| 72 | + * @param editorClass the class object of the editor class | |
| 73 | + * @throws SecurityException if a security manager exists and | |
| 74 | + * its {@code checkPropertiesAccess} method | |
| 75 | + * doesn't allow setting of system properties | |
| 76 | + * | |
| 77 | + * @see SecurityManager#checkPropertiesAccess | |
| 78 | + */ | |
| 79 | + public static void registerEditor(Class<?> targetType, Class<?> editorClass) { | |
| 80 | + SecurityManager sm = System.getSecurityManager(); | |
| 81 | + if (sm != null) { | |
| 82 | + sm.checkPropertiesAccess(); | |
| 83 | + } | |
| 84 | + getFinder().register(targetType, editorClass); | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Locate a value editor for a given target type. | |
| 89 | + * | |
| 90 | + * @param targetType The Class object for the type to be edited | |
| 91 | + * @return An editor object for the given target class. | |
| 92 | + * The result is null if no suitable editor can be found. | |
| 93 | + */ | |
| 94 | + public static PropertyEditor findEditor(Class<?> targetType) { | |
| 95 | + return getFinder().find(targetType); | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Gets the package names that will be searched for property editors. | |
| 100 | + * | |
| 101 | + * @return The array of package names that will be searched in | |
| 102 | + * order to find property editors. | |
| 103 | + * <p> The default value for this array is implementation-dependent, | |
| 104 | + * e.g. Sun implementation initially sets to {"sun.beans.editors"}. | |
| 105 | + */ | |
| 106 | + public static String[] getEditorSearchPath() { | |
| 107 | + return getFinder().getPackages(); | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * Change the list of package names that will be used for | |
| 112 | + * finding property editors. | |
| 113 | + * | |
| 114 | + * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code> | |
| 115 | + * method is called. This could result in a SecurityException. | |
| 116 | + * | |
| 117 | + * @param path Array of package names. | |
| 118 | + * @exception SecurityException if a security manager exists and its | |
| 119 | + * <code>checkPropertiesAccess</code> method doesn't allow setting | |
| 120 | + * of system properties. | |
| 121 | + * @see SecurityManager#checkPropertiesAccess | |
| 122 | + */ | |
| 123 | + public static void setEditorSearchPath(String[] path) { | |
| 124 | + SecurityManager sm = System.getSecurityManager(); | |
| 125 | + if (sm != null) { | |
| 126 | + sm.checkPropertiesAccess(); | |
| 127 | + } | |
| 128 | + getFinder().setPackages(path); | |
| 129 | + } | |
| 130 | + | |
| 131 | + private static PropertyEditorFinder getFinder() { | |
| 132 | + AppContext context = AppContext.getAppContext(); | |
| 133 | + Object object = context.get(FINDER_KEY); | |
| 134 | + if (object instanceof PropertyEditorFinder) { | |
| 135 | + return (PropertyEditorFinder) object; | |
| 136 | + } | |
| 137 | + PropertyEditorFinder finder = new PropertyEditorFinder(); | |
| 138 | + context.put(FINDER_KEY, finder); | |
| 139 | + return finder; | |
| 140 | + } | |
| 141 | +} | |