1 7 8 package java.beans; 9 10 34 35 public class PropertyEditorManager { 36 37 52 53 public static void registerEditor(Class <?> targetType, Class <?> editorClass) { 54 SecurityManager sm = System.getSecurityManager(); 55 if (sm != null) { 56 sm.checkPropertiesAccess(); 57 } 58 initialize(); 59 if (editorClass == null) { 60 registry.remove(targetType); 61 } else { 62 registry.put(targetType, editorClass); 63 } 64 } 65 66 73 74 public static synchronized PropertyEditor findEditor(Class <?> targetType) { 75 initialize(); 76 Class editorClass = (Class )registry.get(targetType); 77 if (editorClass != null) { 78 try { 79 Object o = editorClass.newInstance(); 80 return (PropertyEditor )o; 81 } catch (Exception ex) { 82 System.err.println("Couldn't instantiate type editor \"" + 83 editorClass.getName() + "\" : " + ex); 84 } 85 } 86 87 89 String editorName = targetType.getName() + "Editor"; 90 try { 91 return (PropertyEditor ) Introspector.instantiate(targetType, editorName); 92 } catch (Exception ex) { 93 } 95 96 editorName = targetType.getName(); 98 while (editorName.indexOf('.') > 0) { 99 editorName = editorName.substring(editorName.indexOf('.')+1); 100 } 101 for (int i = 0; i < searchPath.length; i++) { 102 String name = searchPath[i] + "." + editorName + "Editor"; 103 try { 104 return (PropertyEditor ) Introspector.instantiate(targetType, name); 105 } catch (Exception ex) { 106 } 108 } 109 110 return null; 112 } 113 114 122 public static synchronized String [] getEditorSearchPath() { 123 String result[] = new String [searchPath.length]; 125 for (int i = 0; i < searchPath.length; i++) { 126 result[i] = searchPath[i]; 127 } 128 return result; 129 } 130 131 144 145 public static synchronized void setEditorSearchPath(String path[]) { 146 SecurityManager sm = System.getSecurityManager(); 147 if (sm != null) { 148 sm.checkPropertiesAccess(); 149 } 150 initialize(); 151 if (path == null) { 152 path = new String [0]; 153 } 154 searchPath = path; 155 } 156 157 private static synchronized void load(Class targetType, String name) { 158 String editorName = name; 159 for (int i = 0; i < searchPath.length; i++) { 160 try { 161 editorName = searchPath[i] + "." + name; 162 Class cls = Class.forName(editorName); 163 registry.put(targetType, cls); 164 return; 165 } catch (Exception ex) { 166 } 168 } 169 System.err.println("load of " + editorName + " failed"); 171 } 172 173 174 private static synchronized void initialize() { 175 if (registry != null) { 176 return; 177 } 178 registry = new java.util.Hashtable (); 179 load(Byte.TYPE, "ByteEditor"); 180 load(Short.TYPE, "ShortEditor"); 181 load(Integer.TYPE, "IntEditor"); 182 load(Long.TYPE ,"LongEditor"); 183 load(Boolean.TYPE, "BoolEditor"); 184 load(Float.TYPE, "FloatEditor"); 185 load(Double.TYPE, "DoubleEditor"); 186 } 187 188 private static String [] searchPath = { "sun.beans.editors" }; 189 private static java.util.Hashtable registry; 190 } 191 | Popular Tags |