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