1 30 31 package com.nightlabs.util.bean; 32 33 import java.beans.PropertyEditor ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 38 public class PropertyEditorContext 39 { 40 protected Map targetClass2PropertyEditorClass; 41 protected Map propertyName2PropertyEditor; 42 43 public PropertyEditorContext() 44 { 45 targetClass2PropertyEditorClass = new HashMap (); 46 propertyName2PropertyEditor = new HashMap (); 47 } 48 49 public void registerEditor(Class targetType, Class editorClass) 50 { 51 if (targetType == null) 52 throw new NullPointerException ("Param targetType must not be null!"); 53 54 if (editorClass == null) 55 throw new NullPointerException ("Param editorClass must not be null!"); 56 57 targetClass2PropertyEditorClass.put(targetType, editorClass); 58 } 59 60 public void registerEditor(Object target, Class editorClass) 61 { 62 if (target == null) 63 throw new IllegalArgumentException ("Param target must not be null!"); 64 65 Class targetClass = target.getClass(); 66 registerEditor(targetClass, editorClass); 67 } 68 69 public void registerEditor(String propertyName, Class editorClass) 70 { 71 if(propertyName == null) 72 throw new NullPointerException ("Param propertyName must not be null!"); 73 74 if (editorClass == null) 75 throw new NullPointerException ("Param editorClass must not be null!"); 76 77 propertyName2PropertyEditor.put(propertyName, editorClass); 78 } 79 80 public PropertyEditor getEditor(Object o) 81 throws IllegalAccessException , 82 InstantiationException 83 { 84 if (o == null) 85 throw new IllegalArgumentException ("Param o must not be null!"); 86 87 Class targetClass = o.getClass(); 88 PropertyEditor pe = getEditor(targetClass); 89 if (pe != null) { 90 return pe; 91 } else { 92 for (Iterator it = targetClass2PropertyEditorClass.keySet().iterator(); it.hasNext(); ) { 93 Class targetType = (Class ) it.next(); 94 if (targetType.isAssignableFrom(targetClass)) { 95 return (PropertyEditor ) targetClass2PropertyEditorClass.get(targetType); 96 } 97 } 98 } 99 return null; 100 } 101 102 public PropertyEditor getEditor(Class targetType) 103 throws InstantiationException , 104 IllegalAccessException 105 { 106 if (targetClass2PropertyEditorClass.containsKey(targetType)) 107 { 108 Class pecClass = (Class ) targetClass2PropertyEditorClass.get(targetType); 109 PropertyEditor pe = (PropertyEditor ) pecClass.newInstance(); 110 return pe; 111 } 112 return null; 113 } 114 115 public PropertyEditor getEditor(String propertyName) 116 { 117 if (propertyName2PropertyEditor.containsKey(propertyName)) 118 { 119 PropertyEditor pe = (PropertyEditor ) propertyName2PropertyEditor.get(propertyName); 120 return pe; 121 } 122 return null; 123 } 124 125 } 126 | Popular Tags |