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 import gnu.java.beans.editors.ColorEditor; 43 import gnu.java.beans.editors.FontEditor; 44 import gnu.java.beans.editors.NativeBooleanEditor; 45 import gnu.java.beans.editors.NativeByteEditor; 46 import gnu.java.beans.editors.NativeDoubleEditor; 47 import gnu.java.beans.editors.NativeFloatEditor; 48 import gnu.java.beans.editors.NativeIntEditor; 49 import gnu.java.beans.editors.NativeLongEditor; 50 import gnu.java.beans.editors.NativeShortEditor; 51 import gnu.java.beans.editors.StringEditor; 52 import java.awt.Color; 53 import java.awt.Font; 54 55 /** 56 * PropertyEditorManager is used to find property editors 57 * for various types (not necessarily Beans).<P> 58 * 59 * It first checks to see if the property editor is 60 * already registered; if it is, that property editor is 61 * used. Next it takes the type's classname and appends 62 * "Editor" to it, and searches first in the class's 63 * package and then in the property editor search path.<P> 64 * 65 * Default property editors are provided for:<P> 66 * <OL> 67 * <LI>boolean, byte, short, int, long, float, and double</LI> 68 * <LI>java.lang.String</LI> 69 * <LI>java.awt.Color</LI> 70 * <LI>java.awt.Font</LI> 71 * <OL> 72 * 73 * <STRONG>Spec Suggestion:</STRONG> Perhaps an editor for 74 * Filename or something like it should be provided. As well 75 * as char. 76 * 77 * @author John Keiser 78 * @since 1.1 79 * @version 1.1.0, 29 Jul 1998 80 */ 81 82 public class PropertyEditorManager 83 { 84 static java.util.Hashtable editors = new java.util.Hashtable(); 85 static String[] editorSearchPath = { "gnu.java.beans.editors", 86 "sun.beans.editors" }; 87 88 static 89 { registerEditor(Boolean.TYPE, NativeBooleanEditor.class)90 registerEditor(Boolean.TYPE, NativeBooleanEditor.class); registerEditor(Byte.TYPE, NativeByteEditor.class)91 registerEditor(Byte.TYPE, NativeByteEditor.class); registerEditor(Short.TYPE, NativeShortEditor.class)92 registerEditor(Short.TYPE, NativeShortEditor.class); registerEditor(Integer.TYPE, NativeIntEditor.class)93 registerEditor(Integer.TYPE, NativeIntEditor.class); registerEditor(Long.TYPE, NativeLongEditor.class)94 registerEditor(Long.TYPE, NativeLongEditor.class); registerEditor(Float.TYPE, NativeFloatEditor.class)95 registerEditor(Float.TYPE, NativeFloatEditor.class); registerEditor(Double.TYPE, NativeDoubleEditor.class)96 registerEditor(Double.TYPE, NativeDoubleEditor.class); registerEditor(String.class, StringEditor.class)97 registerEditor(String.class, StringEditor.class); registerEditor(Color.class, ColorEditor.class)98 registerEditor(Color.class, ColorEditor.class); registerEditor(Font.class, FontEditor.class)99 registerEditor(Font.class, FontEditor.class); 100 } 101 102 /** 103 * Beats me why this class can be instantiated, but there 104 * you have it. 105 */ PropertyEditorManager()106 public PropertyEditorManager() 107 { 108 // Do nothing here 109 } 110 111 /** 112 * Register an editor for a class. Replaces old editor 113 * if there was one registered before. 114 * 115 * @param editedClass the class that the property editor 116 * will edit. 117 * @param editorClass the PropertyEditor class. 118 */ registerEditor(Class editedClass, Class editorClass)119 public static void registerEditor(Class editedClass, Class editorClass) 120 { 121 editors.put(editedClass, editorClass); 122 } 123 124 /** 125 * Returns a new instance of the property editor for the 126 * specified class. 127 * 128 * @param editedClass the class that the property editor 129 * will edit. 130 * @return a PropertyEditor instance that can edit the 131 * specified class. 132 */ findEditor(Class editedClass)133 public static PropertyEditor findEditor(Class editedClass) 134 { 135 try 136 { 137 Class found = (Class)editors.get(editedClass); 138 if(found != null) 139 { 140 return (PropertyEditor)found.newInstance(); 141 } 142 143 ClassLoader contextClassLoader 144 = Thread.currentThread().getContextClassLoader(); 145 146 try 147 { 148 found = Class.forName(editedClass.getName()+"Editor", true, 149 contextClassLoader); 150 registerEditor(editedClass,found); 151 return (PropertyEditor)found.newInstance(); 152 } 153 catch(ClassNotFoundException E) 154 { 155 } 156 157 String appendName 158 = "." 159 + ClassHelper.getTruncatedClassName(editedClass) 160 + "Editor"; 161 synchronized(editorSearchPath) 162 { 163 for(int i=0;i<editorSearchPath.length;i++) 164 { 165 try 166 { 167 found = Class.forName(editorSearchPath[i] + appendName, 168 true, contextClassLoader); 169 registerEditor(editedClass,found); 170 return (PropertyEditor)found.newInstance(); 171 } 172 catch(ClassNotFoundException E) 173 { 174 } 175 } 176 } 177 } 178 catch(InstantiationException E) 179 { 180 } 181 catch(IllegalAccessException E) 182 { 183 } 184 185 return null; 186 } 187 188 /** 189 * Get the editor search path. 190 * As a minor departure from the spec, the default value 191 * for the editor search path is "gnu.java.beans.editors", 192 * "sun.beans.editors". 193 * 194 * @return the editor search path. 195 */ getEditorSearchPath()196 public static String[] getEditorSearchPath() 197 { 198 return editorSearchPath; 199 } 200 201 /** 202 * Set the editor search path. 203 * 204 * @param editorSearchPath the new value for the editor search path. 205 */ setEditorSearchPath(String[] editorSearchPath)206 public static void setEditorSearchPath(String[] editorSearchPath) 207 { 208 synchronized(editorSearchPath) 209 { 210 PropertyEditorManager.editorSearchPath = editorSearchPath; 211 } 212 } 213 } 214