Path: blob/master/src/java.desktop/share/classes/java/beans/PropertyEditorManager.java
41152 views
/*1* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.beans;2627/**28* The PropertyEditorManager can be used to locate a property editor for29* any given type name. This property editor must support the30* java.beans.PropertyEditor interface for editing a given object.31* <P>32* The PropertyEditorManager uses three techniques for locating an editor33* for a given type. First, it provides a registerEditor method to allow34* an editor to be specifically registered for a given type. Second it35* tries to locate a suitable class by adding "Editor" to the full36* qualified classname of the given type (e.g. "foo.bah.FozEditor").37* Finally it takes the simple classname (without the package name) adds38* "Editor" to it and looks in a search-path of packages for a matching39* class.40* <P>41* So for an input class foo.bah.Fred, the PropertyEditorManager would42* first look in its tables to see if an editor had been registered for43* foo.bah.Fred and if so use that. Then it will look for a44* foo.bah.FredEditor class. Then it will look for (say)45* standardEditorsPackage.FredEditor class.46* <p>47* Default PropertyEditors will be provided for the Java primitive types48* "boolean", "byte", "short", "int", "long", "float", and "double"; and49* for the classes java.lang.String. java.awt.Color, and java.awt.Font.50*51* @since 1.152*/5354public class PropertyEditorManager {5556/**57* Constructs a {@code PropertyEditorManager}.58*/59public PropertyEditorManager() {}6061/**62* Registers an editor class to edit values of the given target class.63* If the editor class is {@code null},64* then any existing definition will be removed.65* Thus this method can be used to cancel the registration.66* The registration is canceled automatically67* if either the target or editor class is unloaded.68* <p>69* If there is a security manager, its {@code checkPropertiesAccess}70* method is called. This could result in a {@linkplain SecurityException}.71*72* @param targetType the class object of the type to be edited73* @param editorClass the class object of the editor class74* @throws SecurityException if a security manager exists and75* its {@code checkPropertiesAccess} method76* doesn't allow setting of system properties77*78* @see SecurityManager#checkPropertiesAccess79*/80public static void registerEditor(Class<?> targetType, Class<?> editorClass) {81@SuppressWarnings("removal")82SecurityManager sm = System.getSecurityManager();83if (sm != null) {84sm.checkPropertiesAccess();85}86ThreadGroupContext.getContext().getPropertyEditorFinder().register(targetType, editorClass);87}8889/**90* Locate a value editor for a given target type.91*92* @param targetType The Class object for the type to be edited93* @return An editor object for the given target class.94* The result is null if no suitable editor can be found.95*/96public static PropertyEditor findEditor(Class<?> targetType) {97return ThreadGroupContext.getContext().getPropertyEditorFinder().find(targetType);98}99100/**101* Gets the package names that will be searched for property editors.102*103* @return The array of package names that will be searched in104* order to find property editors.105* <p> The default value for this array is implementation-dependent,106* e.g. Sun implementation initially sets to {"sun.beans.editors"}.107*/108public static String[] getEditorSearchPath() {109return ThreadGroupContext.getContext().getPropertyEditorFinder().getPackages();110}111112/**113* Change the list of package names that will be used for114* finding property editors.115*116* <p>First, if there is a security manager, its {@code checkPropertiesAccess}117* method is called. This could result in a SecurityException.118*119* @param path Array of package names.120* @exception SecurityException if a security manager exists and its121* {@code checkPropertiesAccess} method doesn't allow setting122* of system properties.123* @see SecurityManager#checkPropertiesAccess124*/125public static void setEditorSearchPath(String[] path) {126@SuppressWarnings("removal")127SecurityManager sm = System.getSecurityManager();128if (sm != null) {129sm.checkPropertiesAccess();130}131ThreadGroupContext.getContext().getPropertyEditorFinder().setPackages(path);132}133}134135136