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