1 19 20 21 package org.netbeans.modules.form; 22 23 import java.beans.*; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 27 31 final public class FormPropertyEditorManager extends Object 32 { 33 private static HashMap editorsCache = new HashMap (30); 34 35 private static HashMap expliciteEditors = new HashMap (10); 36 37 39 public static synchronized PropertyEditor findEditor(FormProperty property) { 40 Class propType = property.getValueType(); 41 FormModel form = property.getPropertyContext().getFormModel(); 42 Class [] edClasses = findEditorClasses(propType, form); 43 if (edClasses.length > 0) { 44 PropertyEditor[] editors = 45 createEditorInstances(new Class [] { edClasses[0] }, propType); 46 if (editors.length > 0) 47 return editors[0]; 48 } 49 return null; 50 } 51 52 public static synchronized PropertyEditor[] getAllEditors(FormProperty property) { 53 Class propType = property.getValueType(); 54 FormModel form = property.getPropertyContext().getFormModel(); 55 return createEditorInstances(findEditorClasses(propType, form), propType); 56 } 57 58 public static synchronized void registerEditor(Class propertyType, 59 Class editorClass) 60 { 61 Class [] currentEditors = (Class [])expliciteEditors.get(getTypeName(propertyType)); 62 Class [] newEditors; 63 if (currentEditors == null) { 64 newEditors = new Class [1]; 65 newEditors[0] = editorClass; 66 } 67 else { 68 for (int i = 0; i < currentEditors.length; i++) { 70 if (currentEditors[i].equals(editorClass)) { 71 return; } 73 } 74 newEditors = new Class [currentEditors.length + 1]; 75 System.arraycopy(currentEditors, 0, newEditors, 0, currentEditors.length); 76 newEditors[newEditors.length - 1] = editorClass; 77 } 78 expliciteEditors.put(getTypeName(propertyType), newEditors); 79 } 80 81 synchronized static void clearEditorsCache() { 82 editorsCache.clear(); 83 } 84 85 87 private static String getTypeName(Class type) { 88 String typeName = type.getName(); 89 if (type.isPrimitive()) { 90 if (Byte.TYPE.equals(type)) typeName = "byte"; else if (Short.TYPE.equals(type)) typeName = "short"; else if (Integer.TYPE.equals(type)) typeName = "integer"; else if (Long.TYPE.equals(type)) typeName = "long"; else if (Boolean.TYPE.equals(type)) typeName = "boolean"; else if (Float.TYPE.equals(type)) typeName = "float"; else if (Double.TYPE.equals(type)) typeName = "double"; else if (Character.TYPE.equals(type)) typeName = "char"; } 99 return typeName; 100 } 101 102 private static Class [] findEditorClasses(Class type, FormModel form) { 103 Class [] edClasses = (Class []) editorsCache.get(type); 105 if (edClasses != null) 106 return edClasses; 107 108 FormLoaderSettings formSettings = FormLoaderSettings.getInstance(); 109 110 ArrayList editorsList = new ArrayList (5); 111 112 PropertyEditor stdPropEd = (type == Object .class || type == java.awt.Font .class) ? null : 114 PropertyEditorManager.findEditor(type); 115 if (stdPropEd != null) { 116 editorsList.add(stdPropEd.getClass()); 117 } 118 else { 119 String editorName = type.getName(); 121 if (!editorName.startsWith("[")) { int dot = editorName.lastIndexOf('.'); 123 if (dot > 0) 124 editorName = editorName.substring(dot+1); 125 126 String [] searchPath = formSettings.getEditorSearchPath(); 127 for (int i = 0; i < searchPath.length; i++) { 128 String name = searchPath[i] + "." + editorName + "Editor"; try { 130 Class edClass = FormUtils.loadClass(name, form); 131 editorsList.add(edClass); 132 break; } 134 catch (Exception e) {} catch (LinkageError e) {} } 137 } 138 } 139 140 String typeName = getTypeName(type); 142 String [][] registered = formSettings.getRegisteredEditors(); 143 for (int i = 0; i < registered.length; i++) { 144 String [] typereg = registered[i]; 145 if ((typereg != null) && (typereg.length > 0)) { 146 if (typereg[0].equals(typeName)) { 147 for (int j = 1; j < typereg.length; j++) { 148 try { 149 Class edClass = FormUtils.loadClass(typereg[j], form); 150 if (!editorsList.contains(edClass)) 151 editorsList.add(edClass); 152 } 153 catch (Exception e) { org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 155 } 156 } 157 } 158 } 159 } 160 161 Class [] explicite = (Class []) expliciteEditors.get(typeName); 163 if (explicite != null) { 164 for (int i = 0; i < explicite.length; i++) { 165 Class edClass = explicite[i]; 166 if (!editorsList.contains(edClass)) 167 editorsList.add(edClass); 168 } 169 } 170 171 if (java.awt.Component .class.isAssignableFrom(type)) 173 editorsList.add(ComponentChooserEditor.class); 174 175 editorsList.add(RADConnectionPropertyEditor.class); 177 178 edClasses = new Class [editorsList.size()]; 179 editorsList.toArray(edClasses); 180 editorsCache.put(type, edClasses); 181 182 return edClasses; 183 } 184 185 private static PropertyEditor[] createEditorInstances(Class [] edClasses, 186 Class propertyType) { 187 ArrayList instancesList = new ArrayList (edClasses.length); 188 189 for (int i = 0; i < edClasses.length; i++) { 190 Class edType = edClasses[i]; 191 if (RADConnectionPropertyEditor.class.isAssignableFrom(edType)) { 192 instancesList.add(new RADConnectionPropertyEditor(propertyType)); 193 } 194 else if (ComponentChooserEditor.class.isAssignableFrom(edType)) { 195 instancesList.add(new ComponentChooserEditor( 196 new Class [] { propertyType })); 197 } 198 else if (PropertyEditor.class.isAssignableFrom(edType)) { 199 try { 200 instancesList.add(edType.newInstance()); 201 } 202 catch (Exception e) { org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 204 } 205 } 206 } 208 209 PropertyEditor[] editors = new PropertyEditor[instancesList.size()]; 210 instancesList.toArray(editors); 211 return editors; 212 } 213 } 214 | Popular Tags |