| 1 30 31 package com.nightlabs.util.bean; 32 33 import java.beans.PropertyEditor ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 public class PropertyEditorMan 38 { 39 private static Map class2Pec = new HashMap (); 40 41 public PropertyEditorMan() 42 { 43 } 44 45 public static void registerEditorContext(Class propertyEditorContextClass) 46 throws InstantiationException , 47 IllegalAccessException 48 { 49 PropertyEditorContext pec = (PropertyEditorContext) propertyEditorContextClass.newInstance(); 50 class2Pec.put(propertyEditorContextClass, pec); 51 } 52 53 public static void registerPropertyEditor(Class targetClass, Class propertyEditorClass, Class propertyEditorContextClass) 54 { 55 if (class2Pec.containsKey(propertyEditorContextClass)) 56 { 57 PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass); 58 pec.registerEditor(targetClass, propertyEditorClass); 59 } 60 } 61 62 public static void registerPropertyEditor(Class targetClass, Class propertyEditorClass) 63 { 64 registerPropertyEditor(targetClass, propertyEditorClass, DefaultPropertyEditorContext.class); 65 } 66 67 public static PropertyEditor getPropertyEditor(Class propertyEditorContextClass, Class targetType) 68 { 71 if (class2Pec.containsKey(propertyEditorContextClass)) 72 { 73 PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass); 74 try { 76 return pec.getEditor(targetType); 77 } catch (InstantiationException e) { 78 e.printStackTrace(); 79 } catch (IllegalAccessException e) { 80 e.printStackTrace(); 81 } 82 } 83 return null; 84 } 85 86 public static PropertyEditor getPropertyEditor(Class targetType) 87 { 90 return getPropertyEditor(DefaultPropertyEditorContext.class, targetType); 91 } 92 93 public static PropertyEditor getPropertyEditor(Class propertyEditorContextClass, Object target) 94 { 97 if (target == null) 98 throw new IllegalArgumentException ("Param target must not be null!"); 99 100 Class targetType = target.getClass(); 101 PropertyEditor pe = getPropertyEditor(propertyEditorContextClass, targetType); 102 if (pe != null) { 103 return pe; 104 } else { 105 if (class2Pec.containsKey(propertyEditorContextClass)) { 106 PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass); 107 try { 108 return pec.getEditor(target); 109 } catch (IllegalAccessException e) { 110 e.printStackTrace(); 111 } catch (InstantiationException e) { 112 e.printStackTrace(); 113 } 114 } 115 } 116 return null; 117 } 118 119 public static PropertyEditor getPropertyEditor(Class propertyEditorContextClass, String propertyName) 120 { 121 if (class2Pec.containsKey(propertyEditorContextClass)) 122 { 123 PropertyEditorContext pec = (PropertyEditorContext) class2Pec.get(propertyEditorContextClass); 124 PropertyEditor pe = pec.getEditor(propertyName); 125 return pe; 126 } 127 return null; 128 } 129 130 } 131 | Popular Tags |