Save This Page
Home » openjdk-7 » java » beans » [javadoc | source]
    1   /*
    2    * Copyright 1996-2006 Sun Microsystems, Inc.  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.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package java.beans;
   27   
   28   import sun.beans.editors;
   29   
   30   /**
   31    * The PropertyEditorManager can be used to locate a property editor for
   32    * any given type name.  This property editor must support the
   33    * java.beans.PropertyEditor interface for editing a given object.
   34    * <P>
   35    * The PropertyEditorManager uses three techniques for locating an editor
   36    * for a given type.  First, it provides a registerEditor method to allow
   37    * an editor to be specifically registered for a given type.  Second it
   38    * tries to locate a suitable class by adding "Editor" to the full
   39    * qualified classname of the given type (e.g. "foo.bah.FozEditor").
   40    * Finally it takes the simple classname (without the package name) adds
   41    * "Editor" to it and looks in a search-path of packages for a matching
   42    * class.
   43    * <P>
   44    * So for an input class foo.bah.Fred, the PropertyEditorManager would
   45    * first look in its tables to see if an editor had been registered for
   46    * foo.bah.Fred and if so use that.  Then it will look for a
   47    * foo.bah.FredEditor class.  Then it will look for (say)
   48    * standardEditorsPackage.FredEditor class.
   49    * <p>
   50    * Default PropertyEditors will be provided for the Java primitive types
   51    * "boolean", "byte", "short", "int", "long", "float", and "double"; and
   52    * for the classes java.lang.String. java.awt.Color, and java.awt.Font.
   53    */
   54   
   55   public class PropertyEditorManager {
   56   
   57       /**
   58        * Register an editor class to be used to edit values of
   59        * a given target class.
   60        *
   61        * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
   62        * method is called. This could result in a SecurityException.
   63        *
   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
   71        */
   72   
   73       public static void registerEditor(Class<?> targetType, Class<?> editorClass) {
   74           SecurityManager sm = System.getSecurityManager();
   75           if (sm != null) {
   76               sm.checkPropertiesAccess();
   77           }
   78           initialize();
   79           if (editorClass == null) {
   80               registry.remove(targetType);
   81           } else {
   82               registry.put(targetType, editorClass);
   83           }
   84       }
   85   
   86       /**
   87        * Locate a value editor for a given target type.
   88        *
   89        * @param targetType  The Class object for the type to be edited
   90        * @return An editor object for the given target class.
   91        * The result is null if no suitable editor can be found.
   92        */
   93   
   94       public static synchronized PropertyEditor findEditor(Class<?> targetType) {
   95           initialize();
   96           Class editorClass = (Class)registry.get(targetType);
   97           if (editorClass != null) {
   98               try {
   99                   Object o = editorClass.newInstance();
  100                   return (PropertyEditor)o;
  101               } catch (Exception ex) {
  102                   System.err.println("Couldn't instantiate type editor \"" +
  103                           editorClass.getName() + "\" : " + ex);
  104               }
  105           }
  106   
  107           // Now try adding "Editor" to the class name.
  108   
  109           String editorName = targetType.getName() + "Editor";
  110           try {
  111               return (PropertyEditor) Introspector.instantiate(targetType, editorName);
  112           } catch (Exception ex) {
  113              // Silently ignore any errors.
  114           }
  115   
  116           // Now try looking for <searchPath>.fooEditor
  117           int index = editorName.lastIndexOf('.') + 1;
  118           if (index > 0) {
  119               editorName = editorName.substring(index);
  120           }
  121           for (String path : searchPath) {
  122               String name = path + '.' + editorName;
  123               try {
  124                   return (PropertyEditor) Introspector.instantiate(targetType, name);
  125               } catch (Exception ex) {
  126                  // Silently ignore any errors.
  127               }
  128           }
  129   
  130           if (null != targetType.getEnumConstants()) {
  131               return new EnumEditor(targetType);
  132           }
  133           // We couldn't find a suitable Editor.
  134           return null;
  135       }
  136   
  137       /**
  138        * Gets the package names that will be searched for property editors.
  139        *
  140        * @return  The array of package names that will be searched in
  141        *          order to find property editors.
  142        * <p>     The default value for this array is implementation-dependent,
  143        *         e.g. Sun implementation initially sets to  {"sun.beans.editors"}.
  144        */
  145       public static synchronized String[] getEditorSearchPath() {
  146           // Return a copy of the searchPath.
  147           String result[] = new String[searchPath.length];
  148           System.arraycopy(searchPath, 0, result, 0, searchPath.length);
  149           return result;
  150       }
  151   
  152       /**
  153        * Change the list of package names that will be used for
  154        *          finding property editors.
  155        *
  156        * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
  157        * method is called. This could result in a SecurityException.
  158        *
  159        * @param path  Array of package names.
  160        * @exception  SecurityException  if a security manager exists and its
  161        *             <code>checkPropertiesAccess</code> method doesn't allow setting
  162        *              of system properties.
  163        * @see SecurityManager#checkPropertiesAccess
  164        */
  165   
  166       public static synchronized void setEditorSearchPath(String path[]) {
  167           SecurityManager sm = System.getSecurityManager();
  168           if (sm != null) {
  169               sm.checkPropertiesAccess();
  170           }
  171           if (path == null) {
  172               path = new String[0];
  173           }
  174           searchPath = path;
  175       }
  176   
  177       private static synchronized void initialize() {
  178           if (registry != null) {
  179               return;
  180           }
  181           registry = new java.util.Hashtable();
  182           registry.put(Byte.TYPE, ByteEditor.class);
  183           registry.put(Short.TYPE, ShortEditor.class);
  184           registry.put(Integer.TYPE, IntegerEditor.class);
  185           registry.put(Long.TYPE, LongEditor.class);
  186           registry.put(Boolean.TYPE, BooleanEditor.class);
  187           registry.put(Float.TYPE, FloatEditor.class);
  188           registry.put(Double.TYPE, DoubleEditor.class);
  189       }
  190   
  191       private static String[] searchPath = { "sun.beans.editors" };
  192       private static java.util.Hashtable registry;
  193   }

Save This Page
Home » openjdk-7 » java » beans » [javadoc | source]