home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / beans / PropertyEditorManager.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.2 KB  |  165 lines

  1. /*
  2.  * @(#)PropertyEditorManager.java    1.30 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 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 primitive 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 (PropertyEditor) Introspector.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 (PropertyEditor) Introspector.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.     /**
  109.      * @return  The array of package names that will be searched in
  110.      *        order to find property editors.
  111.      * <p>     This is initially set to {"sun.beans.editors"}.
  112.      */
  113.  
  114.     public static String[] getEditorSearchPath() {
  115.     return searchPath;
  116.     }
  117.  
  118.     /**
  119.      * Change the list of package names that will be used for
  120.      *        finding property editors.
  121.      * @param path  Array of package names.
  122.      */
  123.  
  124.     public static void setEditorSearchPath(String path[]) {
  125.     if (path == null) {
  126.         path = new String[0];
  127.     }
  128.     searchPath = path;
  129.     }
  130.  
  131.     private static void load(Class targetType, String name) {
  132.     String editorName = name;
  133.     for (int i = 0; i < searchPath.length; i++) {
  134.         try {
  135.             editorName = searchPath[i] + "." + name;
  136.             Class cls = Class.forName(editorName);
  137.             registry.put(targetType, cls);
  138.         return;
  139.         } catch (Exception ex) {
  140.         // Drop through and try next package.
  141.         }
  142.     }
  143.     // This shouldn't happen.
  144.     System.err.println("load of " + editorName + " failed");
  145.     }
  146.  
  147.  
  148.     private static synchronized void initialize() {
  149.     if (registry != null) {
  150.         return;
  151.     }
  152.     registry = new java.util.Hashtable();
  153.     load(Byte.TYPE, "ByteEditor");
  154.     load(Short.TYPE, "ShortEditor");
  155.     load(Integer.TYPE, "IntEditor");
  156.     load(Long.TYPE ,"LongEditor");
  157.     load(Boolean.TYPE, "BoolEditor");
  158.     load(Float.TYPE, "FloatEditor");
  159.     load(Double.TYPE, "DoubleEditor");
  160.     }
  161.  
  162.     private static String[] searchPath = { "sun.beans.editors" };
  163.     private static java.util.Hashtable registry;
  164. }
  165.