1 6 package com.nightlabs.ipanema.person.edit; 7 8 import java.util.HashMap ; 9 import java.util.Map ; 10 11 import org.apache.log4j.Logger; 12 import org.eclipse.core.runtime.IConfigurationElement; 13 14 import com.nightlabs.ipanema.person.AbstractPersonDataField; 15 import com.nightlabs.rcp.extensionpoint.AbstractEPProcessor; 16 import com.nightlabs.rcp.extensionpoint.EPProcessorException; 17 18 25 public class PersonDataFieldEditorFactoryRegistry extends AbstractEPProcessor { 26 private Logger LOGGER = Logger.getLogger(PersonDataFieldEditorFactoryRegistry.class); 27 28 public static final String EXTENSION_POINT_ID = "com.nightlabs.ipanema.base.person_edit_personDataFieldEditorFactory"; 29 public static final String EXTENSION_POINT_ELEMENT_NAME = "persondatafieldeditorfactory"; 31 39 private static Map registriesMap = new HashMap (); 40 41 private Map getTypedRegistry(String type) { 42 Map registry = (Map )registriesMap.get(type); 43 if (registry == null) { 44 registry = new HashMap (); 45 registriesMap.put(type,registry); 46 } 47 return registry; 48 } 49 50 57 public synchronized void addDataFieldEditorFactory(PersonDataFieldEditorFactory editorFactory) { 58 if (editorFactory == null) 59 throw new IllegalArgumentException ("Parameter editor must not be null!"); 60 Class targetType = editorFactory.getTargetPersonDataFieldType(); 61 if (!(AbstractPersonDataField.class.isAssignableFrom(targetType))) 62 throw new IllegalArgumentException ("TargetType must be subclass of AbstractPersonDataField but is "+targetType.getName()); 63 LOGGER.debug("Adding registration for "+targetType.getName()+" on editor "+editorFactory+" editorType is "+editorFactory.getEditorType()); 64 65 getTypedRegistry(editorFactory.getEditorType()).put(targetType,editorFactory); 66 } 67 68 69 private boolean extensionPointProcessed = false; 70 78 public synchronized PersonDataFieldEditorFactory getEditorFactory(Class targetType, String editorType) 79 throws PersonDataFieldEditorNotFoundException 80 { 81 if (!extensionPointProcessed) { 82 try { 84 process(); 85 } catch (EPProcessorException e) { 86 throw new PersonDataFieldEditorNotFoundException(e); 87 } 88 extensionPointProcessed = true; 89 } 90 91 if (targetType == null) 92 throw new IllegalArgumentException ("Parameter targetType must not be null"); 93 Map registry = getTypedRegistry(editorType); 94 95 if (!registry.containsKey(targetType)) 96 throw new PersonDataFieldEditorNotFoundException("No editor found for class "+targetType.getName()); 97 98 return (PersonDataFieldEditorFactory)registry.get(targetType); 99 } 100 101 111 public PersonDataFieldEditor getNewEditorInstance(AbstractPersonDataField dataField, String editorType, boolean setData) 112 throws PersonDataFieldEditorNotFoundException 113 { 114 PersonDataFieldEditorFactory fieldEditorFactry = getEditorFactory(dataField.getClass(), editorType); 115 return fieldEditorFactry.createPersonDataFieldEditor(dataField, setData); 116 } 117 118 127 public PersonDataFieldEditor getNewEditorInstance(AbstractPersonDataField dataField, String editorType) 128 throws PersonDataFieldEditorNotFoundException 129 { 130 PersonDataFieldEditorFactory fieldEditorFactry = getEditorFactory(dataField.getClass(), editorType); 131 return fieldEditorFactry.createPersonDataFieldEditor(dataField, true); 132 } 133 134 140 public synchronized boolean hasRegistration(Class targetType, String editorType) { 141 return getTypedRegistry(editorType).containsKey(targetType); 142 } 143 144 private static PersonDataFieldEditorFactoryRegistry sharedInstance; 145 146 150 public static PersonDataFieldEditorFactoryRegistry getSharedInstance() { 151 if (sharedInstance == null) { 152 sharedInstance = new PersonDataFieldEditorFactoryRegistry(); 153 } 154 return sharedInstance; 155 } 156 157 160 public String getExtensionPointID() { 161 return EXTENSION_POINT_ID; 162 } 163 164 167 public void processElement(IConfigurationElement element) throws EPProcessorException { 168 try{ 169 if (element.getName().toLowerCase().equals(EXTENSION_POINT_ELEMENT_NAME)){ 170 PersonDataFieldEditorFactory fieldEditorFactory = (PersonDataFieldEditorFactory) element.createExecutableExtension("class"); 171 getSharedInstance().addDataFieldEditorFactory(fieldEditorFactory); 172 } 173 else { 174 throw new IllegalArgumentException ("Element "+element.getName()+" is not supported by extension-point "+EXTENSION_POINT_ID); 175 } 176 }catch(Throwable e){ 177 throw new EPProcessorException(e); 178 } 179 } 180 } 181 | Popular Tags |