home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PropertyEditorManager.java < prev    next >
Text File  |  1997-05-20  |  7KB  |  195 lines

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