KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > person > edit > PersonDataFieldEditorFactoryRegistry


1 /*
2  * Created on Nov 26, 2004
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.person.edit;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
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 /**
19  * A Registry holding Associations from subclasses of {@link com.nightlabs.ipanema.person.AbstractPersonDataField}
20  * to {@link com.nightlabs.ipanema.person.edit.PersonDataFieldEditor}s grouped by editorTypes.<br/>
21  * As EPProcessor it processes extensions to com.nightlabs.ipanema.person.edit.personDataFieldEditor.
22  *
23  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
24  */

25 public class PersonDataFieldEditorFactoryRegistry extends AbstractEPProcessor {
26     private Logger LOGGER = Logger.getLogger(PersonDataFieldEditorFactoryRegistry.class);
27     
28     public static final String JavaDoc EXTENSION_POINT_ID = "com.nightlabs.ipanema.base.person_edit_personDataFieldEditorFactory";
29     public static final String JavaDoc EXTENSION_POINT_ELEMENT_NAME = "persondatafieldeditorfactory"; // lower case for error tolerance
30

31     /**
32      * A Map holding the registries for all editorTypes.<br/>
33      *
34      * key: String editorType<br/>
35      * value: Map registry<br/>
36      * key: Class targetType<br/>
37      * value: PersonDataFieldEditor editor<br/>
38      */

39     private static Map JavaDoc registriesMap = new HashMap JavaDoc();
40     
41     private Map JavaDoc getTypedRegistry(String JavaDoc type) {
42         Map JavaDoc registry = (Map JavaDoc)registriesMap.get(type);
43         if (registry == null) {
44             registry = new HashMap JavaDoc();
45             registriesMap.put(type,registry);
46         }
47         return registry;
48     }
49     
50     /**
51      * Add a new {@link PersonDataFieldEditor} to the registry.
52      * Checks if {@link PersonDataFieldEditor#getTargetPersonStructType()} returns
53      * a subclass of {@link com.nightlabs.ipanema.person.AbstractPersonDataField}
54      * and throws a {@link IllegalArgumentException}
55      * @param editor
56      */

57     public synchronized void addDataFieldEditorFactory(PersonDataFieldEditorFactory editorFactory) {
58         if (editorFactory == null)
59             throw new IllegalArgumentException JavaDoc("Parameter editor must not be null!");
60         Class JavaDoc targetType = editorFactory.getTargetPersonDataFieldType();
61         if (!(AbstractPersonDataField.class.isAssignableFrom(targetType)))
62             throw new IllegalArgumentException JavaDoc("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     /**
71      * Find the editor for a specific PersonDataField-type and editor type.
72      *
73      * @param targetType
74      * @param editorType
75      * @return The registered PersonDataFieldEditor for the given targetType
76      * @throws PersonDataFieldEditorNotFoundException
77      */

78     public synchronized PersonDataFieldEditorFactory getEditorFactory(Class JavaDoc targetType, String JavaDoc editorType)
79     throws PersonDataFieldEditorNotFoundException
80     {
81         if (!extensionPointProcessed) {
82             // process the extensionpoint to make the registrations
83
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 JavaDoc("Parameter targetType must not be null");
93         Map JavaDoc 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     /**
102      * Find the PersonDataFieldEditorFactory for the Class of the given dataField
103      * and editorType and invokes createPersonDataFieldEditor(dataField, setData)
104      *
105      * @param dataField
106      * @param editorType
107      * @param setData
108      * @return A new instance of the appropriate PersonDataFieldEditor
109      * @throws PersonDataFieldEditorNotFoundException
110      */

111     public PersonDataFieldEditor getNewEditorInstance(AbstractPersonDataField dataField, String JavaDoc editorType, boolean setData)
112     throws PersonDataFieldEditorNotFoundException
113     {
114         PersonDataFieldEditorFactory fieldEditorFactry = getEditorFactory(dataField.getClass(), editorType);
115         return fieldEditorFactry.createPersonDataFieldEditor(dataField, setData);
116     }
117     
118     /**
119      * Find the PersonDataFieldEditorFactory for the Class of the given dataField
120      * and editorType and invokes createPersonDataFieldEditor(dataField, true)
121      *
122      * @param dataField
123      * @param editorType
124      * @return A new instance of the appropriate PersonDataFieldEditor
125      * @throws PersonDataFieldEditorNotFoundException
126      */

127     public PersonDataFieldEditor getNewEditorInstance(AbstractPersonDataField dataField, String JavaDoc editorType)
128     throws PersonDataFieldEditorNotFoundException
129     {
130         PersonDataFieldEditorFactory fieldEditorFactry = getEditorFactory(dataField.getClass(), editorType);
131         return fieldEditorFactry.createPersonDataFieldEditor(dataField, true);
132     }
133     
134     /**
135      *
136      * @param targetType
137      * @param editorType
138      * @return Weather the registry has a registration for the given targetType and editorType
139      */

140     public synchronized boolean hasRegistration(Class JavaDoc targetType, String JavaDoc editorType) {
141         return getTypedRegistry(editorType).containsKey(targetType);
142     }
143     
144     private static PersonDataFieldEditorFactoryRegistry sharedInstance;
145     
146     /**
147      * Returns the static shared instance of a PersonDataFieldEditorFactoryRegistry.
148      * @return The static shared instance of a PersonDataFieldEditorFactoryRegistry.
149      */

150     public static PersonDataFieldEditorFactoryRegistry getSharedInstance() {
151         if (sharedInstance == null) {
152             sharedInstance = new PersonDataFieldEditorFactoryRegistry();
153         }
154         return sharedInstance;
155     }
156     
157     /**
158      * @see com.nightlabs.rcp.extensionpoint.AbstractEPProcessor#getExtensionPointID()
159      */

160     public String JavaDoc getExtensionPointID() {
161         return EXTENSION_POINT_ID;
162     }
163
164     /**
165      * @see com.nightlabs.rcp.extensionpoint.AbstractEPProcessor#processElement(org.eclipse.core.runtime.IConfigurationElement)
166      */

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 JavaDoc("Element "+element.getName()+" is not supported by extension-point "+EXTENSION_POINT_ID);
175             }
176         }catch(Throwable JavaDoc e){
177             throw new EPProcessorException(e);
178         }
179     }
180 }
181
Popular Tags