home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / PropertyEditorManager.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  187 lines

  1. /*
  2.  * @(#)PropertyEditorManager.java    1.27 98/07/01
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.beans;
  16.  
  17. /**
  18.  * The PropertyEditorManager can be used to locate a property editor for
  19.  * any given type name.  This property editor must support the
  20.  * java.beans.PropertyEditor interface for editing a given object.
  21.  * <P>
  22.  * The PropertyEditorManager uses three techniques for locating an editor
  23.  * for a given type.  First, it provides a registerEditor method to allow
  24.  * an editor to be specifically registered for a given type.  Second it
  25.  * tries to locate a suitable class by adding "Editor" to the full 
  26.  * qualified classname of the given type (e.g. "foo.bah.FozEditor").
  27.  * Finally it takes the simple classname (without the package name) adds
  28.  * "Editor" to it and looks in a search-path of packages for a matching
  29.  * class.
  30.  * <P>
  31.  * So for an input class foo.bah.Fred, the PropertyEditorManager would
  32.  * first look in its tables to see if an editor had been registered for
  33.  * foo.bah.Fred and if so use that.  Then it will look for a
  34.  * foo.bah.FredEditor class.  Then it will look for (say) 
  35.  * standardEditorsPackage.FredEditor class.
  36.  * <p>
  37.  * Default PropertyEditors will be provided for the Java builtin types
  38.  * "boolean", "byte", "short", "int", "long", "float", and "double"; and
  39.  * for the classes java.lang.String. java.awt.Color, and java.awt.Font.
  40.  */
  41.  
  42. public class PropertyEditorManager {
  43.  
  44.     /**
  45.      * Register an editor class to be used to editor values of
  46.      * a given target class.
  47.      * @param targetType the Class object of the type to be edited
  48.      * @param editorClass the Class object of the editor class.  If
  49.      *       this is null, then any existing definition will be removed.
  50.      */
  51.  
  52.     public static void registerEditor(Class targetType, Class editorClass) {
  53.     initialize();
  54.     if (editorClass == null) {
  55.         registry.remove(targetType);
  56.     } else {
  57.         registry.put(targetType, editorClass);
  58.     }
  59.     }
  60.  
  61.     /**
  62.      * Locate a value editor for a given target type.
  63.      * @param targetType  The Class object for the type to be edited
  64.      * @return An editor object for the given target class. 
  65.      * The result is null if no suitable editor can be found.
  66.      */
  67.  
  68.     public static PropertyEditor findEditor(Class targetType) {
  69.     initialize();
  70.     Class editorClass = (Class)registry.get(targetType);
  71.     if (editorClass != null) {
  72.         try {
  73.         Object o = editorClass.newInstance();
  74.             return (PropertyEditor)o;
  75.         } catch (Exception ex) {
  76.          System.err.println("Couldn't instantiate type editor \"" +
  77.             editorClass.getName() + "\" : " + ex);
  78.         }
  79.     }
  80.  
  81.     // Now try adding "Editor" to the class name.
  82.  
  83.     String editorName = targetType.getName() + "Editor";
  84.     try {
  85.         return instantiate(targetType, editorName);
  86.     } catch (Exception ex) {
  87.        // Silently ignore any errors.
  88.     }
  89.  
  90.     // Now try looking for <searchPath>.fooEditor
  91.     editorName = targetType.getName();
  92.        while (editorName.indexOf('.') > 0) {
  93.         editorName = editorName.substring(editorName.indexOf('.')+1);
  94.     }
  95.     for (int i = 0; i < searchPath.length; i++) {
  96.         String name = searchPath[i] + "." + editorName + "Editor";
  97.         try {
  98.             return instantiate(targetType, name);
  99.         } catch (Exception ex) {
  100.            // Silently ignore any errors.
  101.         }
  102.     }
  103.  
  104.     // We couldn't find a suitable Editor.
  105.     return (null);
  106.     }
  107.  
  108.     private static PropertyEditor instantiate(Class sibling, String className)
  109.          throws InstantiationException, IllegalAccessException,
  110.                         ClassNotFoundException {
  111.     // First check with siblings classloader (if any). 
  112.     ClassLoader cl = sibling.getClassLoader();
  113.     if (cl != null) {
  114.         try {
  115.             Class cls = cl.loadClass(className);
  116.             Object o = cls.newInstance();
  117.             PropertyEditor ed = (PropertyEditor)o;
  118.         return ed;
  119.         } catch (Exception ex) {
  120.             // Just drop through
  121.         }
  122.         }
  123.     // Now try the system classloader.
  124.     Class cls = Class.forName(className);
  125.     Object o = cls.newInstance();
  126.         PropertyEditor ed = (PropertyEditor)o;
  127.     return ed;
  128.     }
  129.  
  130.     /**
  131.      * @return  The array of package names that will be searched in
  132.      *        order to find property editors.
  133.      * <p>     This is initially set to {"sun.beans.editors"}.
  134.      */
  135.  
  136.     public static String[] getEditorSearchPath() {
  137.     return searchPath;
  138.     }
  139.  
  140.     /**
  141.      * Change the list of package names that will be used for
  142.      *        finding property editors.
  143.      * @param path  Array of package names.
  144.      */
  145.  
  146.     public static void setEditorSearchPath(String path[]) {
  147.     if (path == null) {
  148.         path = new String[0];
  149.     }
  150.     searchPath = path;
  151.     }
  152.  
  153.     private static void load(Class targetType, String name) {
  154.     String editorName = name;
  155.     for (int i = 0; i < searchPath.length; i++) {
  156.         try {
  157.             editorName = searchPath[i] + "." + name;
  158.             Class cls = Class.forName(editorName);
  159.             registry.put(targetType, cls);
  160.         return;
  161.         } catch (Exception ex) {
  162.         // Drop through and try next package.
  163.         }
  164.     }
  165.     // This shouldn't happen.
  166.     System.err.println("load of " + editorName + " failed");
  167.     }
  168.  
  169.  
  170.     private static synchronized void initialize() {
  171.     if (registry != null) {
  172.         return;
  173.     }
  174.     registry = new java.util.Hashtable();
  175.     load(Byte.TYPE, "ByteEditor");
  176.     load(Short.TYPE, "ShortEditor");
  177.     load(Integer.TYPE, "IntEditor");
  178.     load(Long.TYPE ,"LongEditor");
  179.     load(Boolean.TYPE, "BoolEditor");
  180.     load(Float.TYPE, "FloatEditor");
  181.     load(Double.TYPE, "DoubleEditor");
  182.     }
  183.  
  184.     private static String[] searchPath = { "sun.beans.editors" };
  185.     private static java.util.Hashtable registry;
  186. }
  187.