PropertyEditorManager.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* java.beans.PropertyEditorManager
  2. Copyright (C) 1998 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.beans;
  32. import gnu.java.beans.editors.ColorEditor;
  33. import gnu.java.beans.editors.FontEditor;
  34. import gnu.java.beans.editors.NativeBooleanEditor;
  35. import gnu.java.beans.editors.NativeByteEditor;
  36. import gnu.java.beans.editors.NativeDoubleEditor;
  37. import gnu.java.beans.editors.NativeFloatEditor;
  38. import gnu.java.beans.editors.NativeIntEditor;
  39. import gnu.java.beans.editors.NativeLongEditor;
  40. import gnu.java.beans.editors.NativeShortEditor;
  41. import gnu.java.beans.editors.StringEditor;
  42. import gnu.java.lang.ClassHelper;
  43. import java.awt.Color;
  44. import java.awt.Font;
  45. /**
  46. * PropertyEditorManager is used to find property editors
  47. * for various types (not necessarily Beans).<P>
  48. *
  49. * It first checks to see if the property editor is
  50. * already registered; if it is, that property editor is
  51. * used. Next it takes the type's classname and appends
  52. * "Editor" to it, and searches first in the class's
  53. * package and then in the property editor search path.
  54. *
  55. * <p>Default property editors are provided for:</p>
  56. *
  57. * <ol>
  58. * <li>boolean, byte, short, int, long, float, and double</li>
  59. * <li>java.lang.String</li>
  60. * <li>java.awt.Color</li>
  61. * <li>java.awt.Font</li>
  62. * </ol>
  63. *
  64. * <p><strong>Spec Suggestion:</strong> Perhaps an editor for
  65. * Filename or something like it should be provided. As well
  66. * as char.</p>
  67. *
  68. * @author John Keiser
  69. * @since 1.1
  70. * @version 1.1.0, 29 Jul 1998
  71. */
  72. public class PropertyEditorManager
  73. {
  74. static java.util.Hashtable<Class<?>,Class<?>> editors =
  75. new java.util.Hashtable<Class<?>,Class<?>>();
  76. static String[] editorSearchPath = { "gnu.java.beans.editors",
  77. "sun.beans.editors" };
  78. static
  79. {
  80. registerEditor(Boolean.TYPE, NativeBooleanEditor.class);
  81. registerEditor(Byte.TYPE, NativeByteEditor.class);
  82. registerEditor(Short.TYPE, NativeShortEditor.class);
  83. registerEditor(Integer.TYPE, NativeIntEditor.class);
  84. registerEditor(Long.TYPE, NativeLongEditor.class);
  85. registerEditor(Float.TYPE, NativeFloatEditor.class);
  86. registerEditor(Double.TYPE, NativeDoubleEditor.class);
  87. registerEditor(String.class, StringEditor.class);
  88. registerEditor(Color.class, ColorEditor.class);
  89. registerEditor(Font.class, FontEditor.class);
  90. }
  91. /**
  92. * Beats me why this class can be instantiated, but there
  93. * you have it.
  94. */
  95. public PropertyEditorManager()
  96. {
  97. // Do nothing here
  98. }
  99. /**
  100. * Register an editor for a class. Replaces old editor
  101. * if there was one registered before.
  102. *
  103. * @param editedClass the class that the property editor
  104. * will edit.
  105. * @param editorClass the PropertyEditor class.
  106. */
  107. public static void registerEditor(Class<?> editedClass, Class<?> editorClass)
  108. {
  109. editors.put(editedClass, editorClass);
  110. }
  111. /**
  112. * Returns a new instance of the property editor for the
  113. * specified class.
  114. *
  115. * @param editedClass the class that the property editor
  116. * will edit.
  117. * @return a PropertyEditor instance that can edit the
  118. * specified class.
  119. */
  120. public static PropertyEditor findEditor(Class<?> editedClass)
  121. {
  122. try
  123. {
  124. Class found = (Class)editors.get(editedClass);
  125. if(found != null)
  126. {
  127. return (PropertyEditor)found.newInstance();
  128. }
  129. ClassLoader contextClassLoader
  130. = Thread.currentThread().getContextClassLoader();
  131. try
  132. {
  133. found = Class.forName(editedClass.getName()+"Editor", true,
  134. contextClassLoader);
  135. registerEditor(editedClass,found);
  136. return (PropertyEditor)found.newInstance();
  137. }
  138. catch(ClassNotFoundException E)
  139. {
  140. }
  141. String appendName
  142. = "."
  143. + ClassHelper.getTruncatedClassName(editedClass)
  144. + "Editor";
  145. synchronized(editorSearchPath)
  146. {
  147. for(int i=0;i<editorSearchPath.length;i++)
  148. {
  149. try
  150. {
  151. found = Class.forName(editorSearchPath[i] + appendName,
  152. true, contextClassLoader);
  153. registerEditor(editedClass,found);
  154. return (PropertyEditor)found.newInstance();
  155. }
  156. catch(ClassNotFoundException E)
  157. {
  158. }
  159. }
  160. }
  161. }
  162. catch(InstantiationException E)
  163. {
  164. }
  165. catch(IllegalAccessException E)
  166. {
  167. }
  168. return null;
  169. }
  170. /**
  171. * Get the editor search path.
  172. * As a minor departure from the spec, the default value
  173. * for the editor search path is "gnu.java.beans.editors",
  174. * "sun.beans.editors".
  175. *
  176. * @return the editor search path.
  177. */
  178. public static String[] getEditorSearchPath()
  179. {
  180. return editorSearchPath;
  181. }
  182. /**
  183. * Set the editor search path.
  184. *
  185. * @param editorSearchPath the new value for the editor search path.
  186. */
  187. public static void setEditorSearchPath(String[] editorSearchPath)
  188. {
  189. synchronized(editorSearchPath)
  190. {
  191. PropertyEditorManager.editorSearchPath = editorSearchPath;
  192. }
  193. }
  194. }