p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / FreeTextElementFactory.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor;
11
12 import java.net.URL;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.forms.IManagedForm;
20 import org.eclipse.ui.forms.ManagedForm;
21
22 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
23 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.cdm.model.description.TaxonDescription;
26 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
30 import eu.etaxonomy.taxeditor.controller.EditorController;
31 import eu.etaxonomy.taxeditor.editor.description.CommonNameComposite;
32 import eu.etaxonomy.taxeditor.editor.description.DescriptionElementComposite;
33 import eu.etaxonomy.taxeditor.editor.description.DescriptionFeatureGroupComposite;
34 import eu.etaxonomy.taxeditor.editor.description.DescriptionGroupComposite;
35 import eu.etaxonomy.taxeditor.editor.description.DescriptionLabelComposite;
36 import eu.etaxonomy.taxeditor.editor.images.ImageComposite;
37 import eu.etaxonomy.taxeditor.editor.images.ImagesGroupComposite;
38 import eu.etaxonomy.taxeditor.editor.images.TaxonHasNoImagesComposite;
39 import eu.etaxonomy.taxeditor.editor.name.AcceptedNameComposite;
40 import eu.etaxonomy.taxeditor.editor.name.ConceptComposite;
41 import eu.etaxonomy.taxeditor.editor.name.ConceptGroupComposite;
42 import eu.etaxonomy.taxeditor.editor.name.HomotypicalGroupComposite;
43 import eu.etaxonomy.taxeditor.editor.name.MisappliedGroupComposite;
44 import eu.etaxonomy.taxeditor.editor.name.MisappliedNameComposite;
45 import eu.etaxonomy.taxeditor.editor.name.SynonymComposite;
46
47 /**
48 * @author p.ciardelli
49 * @created 08.01.2009
50 * @version 1.0
51 */
52 public class FreeTextElementFactory implements IFreeTextElementFactory {
53 private static final Logger logger = Logger
54 .getLogger(FreeTextElementFactory.class);
55
56 private static IFreeTextElementFactory factory;
57
58 private Map<Taxon, IManagedForm> nameForms;
59 private Map<Taxon, IManagedForm> descriptionForms;
60
61 private HashMap<Taxon, IManagedForm> imagesForms;
62
63 public static IFreeTextElementFactory getDefault() {
64 if (factory == null) {
65 factory = new FreeTextElementFactory();
66 }
67 return factory;
68 }
69
70 public Composite createAcceptedTaxon(IManagedForm form, Taxon taxon) {
71
72 // Put the taxon's form in the form map
73 Assert.isNotNull(form, "A non-null IManagedForm must be provided.");
74 getTaxonNameForms().put(taxon, form);
75
76 // Create a homotypic group composite for the accepted taxon
77 HomotypicalGroup group = taxon.getHomotypicGroup();
78 Assert.isNotNull(group, "Taxon does not have a homotypic group");
79 Composite groupComposite = createHomotypicalGroup(taxon, group);
80
81 // Create a name composite for the accepted taxon
82 Composite acceptedNameComposite =
83 new AcceptedNameComposite(groupComposite, form, taxon);
84
85 // Return the name composite
86 return acceptedNameComposite;
87 }
88
89 public Composite createSynonym(Taxon taxon, Synonym synonym) {
90
91 // Get the taxon's form
92 IManagedForm form = getNameForm(taxon);
93 Assert.isNotNull(form, "No IManagedForm has been provided for the synonym's accepted taxon.");
94
95 // Get the synonym's homotypic group
96 HomotypicalGroup group = synonym.getHomotypicGroup();
97 Assert.isNotNull(group, "Synonym does not have a homotypic group");
98
99 // If the group doesn't yet have a composite, create one and add it to the repository
100 Composite groupComposite =
101 EditorController.getNameEditor(taxon).getHomotypicGroup(group);
102 if (groupComposite == null) {
103 groupComposite = createHomotypicalGroup(taxon, group);
104 }
105
106 // Create a synonym composite in the homotypic group
107 Composite synonymComposite =
108 new SynonymComposite(groupComposite, form, taxon, synonym);
109
110 return synonymComposite;
111 }
112
113
114 public Composite createMisappliedName(Taxon taxon, Taxon misappliedName) {
115
116 // Get the taxon's form
117 IManagedForm form = getNameForm(taxon);
118 Assert.isNotNull(form, "No IManagedForm has been provided for the synonym's accepted taxon.");
119
120 // If there is no composite for misapplied names,
121 // create one and add it to the repository
122 Composite groupComposite =
123 EditorController.getNameEditor(taxon).getMisappliedGroup();
124 if (groupComposite == null) {
125 groupComposite = createMisappliedGroup(taxon);
126 }
127
128 // Create the name's composite
129 Composite composite = new MisappliedNameComposite
130 (groupComposite, form, taxon, misappliedName);
131
132 return composite;
133 }
134
135
136 public Composite createMisappliedGroup(Taxon taxon) {
137
138 // Get the taxon's form
139 IManagedForm form = getNameForm(taxon);
140 Assert.isNotNull(form, "No IManagedForm has been provided for the misapplied group's accepted taxon.");
141
142 // Create the group composite
143 Composite groupComposite = new MisappliedGroupComposite(getTopLevelComposite(form), taxon);
144 groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, form));
145
146 // Put the group composite before concept group composite, if any
147 Composite conceptGroupComposite =
148 EditorController.getNameEditor(taxon).getConceptGroup();
149 if (conceptGroupComposite != null) {
150 groupComposite.moveAbove(conceptGroupComposite);
151 }
152
153 return groupComposite;
154 }
155
156 public Composite createHomotypicalGroup(Taxon taxon, HomotypicalGroup group) {
157
158 // Get the taxon's form
159 IManagedForm form = getNameForm(taxon);
160 Assert.isNotNull(form, "No IManagedForm has been provided for the homotypical group's accepted taxon.");
161
162 // Create the group composite
163 Composite groupComposite = new HomotypicalGroupComposite(getTopLevelComposite(form), taxon, group);
164 groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, form));
165
166 return groupComposite;
167 }
168
169 private Map<Taxon, IManagedForm> getTaxonNameForms() {
170 if (nameForms == null) {
171 nameForms = new HashMap<Taxon, IManagedForm>();
172 }
173 return nameForms;
174 }
175
176 private IManagedForm getNameForm(Taxon taxon) {
177 return getTaxonNameForms().get(taxon);
178 }
179
180 private Composite getTopLevelComposite(IManagedForm form) {
181 return form.getForm().getBody();
182 }
183
184
185 public Composite createConcept(Taxon taxon, TaxonRelationship relationship) {
186 // Get the taxon's form
187 IManagedForm form = getNameForm(taxon);
188 Assert.isNotNull(form, "No IManagedForm has been provided for the synonym's accepted taxon.");
189
190 // If there is no composite for misapplied names,
191 // create one and add it to the repository
192 Composite groupComposite =
193 EditorController.getNameEditor(taxon).getConceptGroup();
194 if (groupComposite == null) {
195 groupComposite = createConceptGroup(taxon);
196 }
197
198 // Create the name's composite
199 Composite composite = ConceptComposite.getNewInstance(groupComposite, form,
200 taxon, relationship);
201
202 return composite;
203 }
204
205
206 public Composite createConceptGroup(Taxon taxon) {
207
208 // Get the taxon's form
209 IManagedForm form = getNameForm(taxon);
210 Assert.isNotNull(form, "No IManagedForm has been provided for the concept group's accepted taxon.");
211
212 // Create the group composite
213 Composite groupComposite = new ConceptGroupComposite(getTopLevelComposite(form), taxon);
214 groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, form));
215
216 // Put the group composite after misapplied group composite, if any
217 Composite misappliedGroupComposite =
218 EditorController.getNameEditor(taxon).getMisappliedGroup();
219 if (misappliedGroupComposite != null) {
220 groupComposite.moveBelow(misappliedGroupComposite);
221 }
222
223 return groupComposite;
224
225 }
226
227
228 public Composite createDescriptionLabel(IManagedForm form, Taxon taxon,
229 TaxonDescription description) {
230
231 // Put the taxon's form in the form map if no other description has done so
232 Assert.isNotNull(form, "A non-null IManagedForm must be provided.");
233 getTaxonDescriptionForms().put(taxon, form);
234
235 // Create a group composite for the description
236 Composite groupComposite = createDescriptionGroup(taxon, description);
237
238 // Create label composite
239 Composite labelComposite =
240 new DescriptionLabelComposite(groupComposite, form, description);
241
242 // Return the label composite
243 return labelComposite;
244 }
245
246
247 public Composite createDescriptionElement(Taxon taxon, TaxonDescription description,
248 DescriptionElementBase element) {
249
250 // Get the taxon's form
251 IManagedForm form = getDescriptionForm(taxon);
252 Assert.isNotNull(form, "No IManagedForm has been provided for the element's taxon.");
253
254 // Get the element's feature
255 Feature feature = element.getFeature();
256 Assert.isNotNull(feature, "Element does not have a feature.");
257
258 // If the feature doesn't yet have a composite, create one and add it to the repository
259 Composite groupComposite = EditorController.getDescriptionEditor(taxon)
260 .getDescriptionFeatureGroup(description, feature);
261 if (groupComposite == null) {
262 groupComposite = createDescriptionFeatureGroup(taxon, description, feature);
263 }
264
265 // Create an element composite in the feature group according to its type
266 Composite elementComposite = null;
267 if (element.isInstanceOf(CommonTaxonName.class)) {
268 elementComposite =
269 new CommonNameComposite(groupComposite, form, (CommonTaxonName) element);
270 } else {
271 elementComposite =
272 new DescriptionElementComposite(groupComposite, form, element);
273 }
274
275 return elementComposite;
276 }
277
278
279 public Composite createDescriptionGroup(Taxon taxon,
280 TaxonDescription description) {
281
282 // Get the taxon's form
283 IManagedForm form = getDescriptionForm(taxon);
284 Assert.isNotNull(form, "No IManagedForm has been provided for the description's taxon.");
285
286 // Create the group composite
287 GroupComposite groupComposite = new DescriptionGroupComposite(getTopLevelComposite(form), taxon, description);
288 groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, form));
289 groupComposite.setDroppable(false);
290
291 return groupComposite;
292 }
293
294
295 public Composite createDescriptionFeatureGroup(Taxon taxon,
296 TaxonDescription description, Feature feature) {
297
298 // Get the taxon's form
299 IManagedForm form = getDescriptionForm(taxon);
300 Assert.isNotNull(form, "No IManagedForm has been provided for the feature's taxon.");
301
302 // Description label should be created before adding features
303 Composite descriptionGroupComposite =
304 EditorController.getDescriptionEditor(taxon).getDescriptionGroup(description);
305 Assert.isNotNull(descriptionGroupComposite,
306 "Description group must be created - usually by creating its label - before adding features.");
307
308 // Create the group composite
309 Composite groupComposite = new DescriptionFeatureGroupComposite
310 (descriptionGroupComposite, taxon, description, feature);
311 groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, form));
312
313 return groupComposite;
314 }
315
316 private Map<Taxon, IManagedForm> getTaxonDescriptionForms() {
317 if (descriptionForms == null) {
318 descriptionForms = new HashMap<Taxon, IManagedForm>();
319 }
320 return descriptionForms;
321 }
322
323
324 private IManagedForm getDescriptionForm(Taxon taxon) {
325 return getTaxonDescriptionForms().get(taxon);
326 }
327
328 /* (non-Javadoc)
329 * @see eu.etaxonomy.taxeditor.editor.IFreeTextElementFactory#createImageElement(org.eclipse.ui.forms.ManagedForm, eu.etaxonomy.cdm.model.taxon.Taxon, java.net.URL)
330 */
331 public Composite createImageElement(ManagedForm form, Taxon taxon, URL url) {
332
333 // Get the taxon's form
334 Assert.isNotNull(form, "A non-null IManagedForm must be provided.");
335
336 // Get the group composite for all images
337 GroupComposite groupComposite =
338 EditorController.getImageEditor(taxon).getImageGroup();
339 if (groupComposite == null) {
340 groupComposite = createImagesGroup(taxon, form);
341 }
342
343 // Create the image composite
344 GroupedComposite imageComposite = new ImageComposite(groupComposite, form, taxon, url);
345 imageComposite.addFocusListener(new CompositeBorderDecorator(imageComposite, form));
346
347 return imageComposite;
348 }
349
350 /**
351 * @param taxon
352 * @param form2
353 * @return
354 */
355 private GroupComposite createImagesGroup(Taxon taxon, ManagedForm form) {
356
357 // Create the group composite
358 GroupComposite groupComposite = new ImagesGroupComposite(getTopLevelComposite(form), taxon);
359
360 return groupComposite;
361 }
362
363 /**
364 * @param taxon
365 * @return
366 */
367 private IManagedForm getImagesForm(Taxon taxon) {
368 return getTaxonImagesForms().get(taxon);
369 }
370
371 /**
372 * @return
373 */
374 private Map<Taxon, IManagedForm> getTaxonImagesForms() {
375 if (imagesForms == null) {
376 imagesForms = new HashMap<Taxon, IManagedForm>();
377 }
378 return imagesForms;
379 }
380
381 /* (non-Javadoc)
382 * @see eu.etaxonomy.taxeditor.editor.IFreeTextElementFactory#createTaxonHasNoImagesElement(org.eclipse.ui.forms.ManagedForm, eu.etaxonomy.cdm.model.taxon.Taxon)
383 */
384 public Composite createTaxonHasNoImagesElement(ManagedForm form,
385 Taxon taxon) {
386 // Get the taxon's form
387 Assert.isNotNull(form, "A non-null IManagedForm must be provided.");
388
389 // Get the group composite for all images
390 GroupComposite groupComposite =
391 EditorController.getImageEditor(taxon).getImageGroup();
392 if (groupComposite == null) {
393 groupComposite = createImagesGroup(taxon, form);
394 }
395
396 // Create the image composite
397 GroupedComposite imageComposite = new TaxonHasNoImagesComposite(groupComposite, form, taxon);
398
399 return imageComposite;
400 }
401 }