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