2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
13 import java
.util
.HashMap
;
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
;
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
;
52 public class FreeTextElementFactory
implements IFreeTextElementFactory
{
53 private static final Logger logger
= Logger
54 .getLogger(FreeTextElementFactory
.class);
56 private static IFreeTextElementFactory factory
;
58 private Map
<Taxon
, IManagedForm
> nameForms
;
59 private Map
<Taxon
, IManagedForm
> descriptionForms
;
61 private HashMap
<Taxon
, IManagedForm
> imagesForms
;
63 public static IFreeTextElementFactory
getDefault() {
64 if (factory
== null) {
65 factory
= new FreeTextElementFactory();
70 public Composite
createAcceptedTaxon(IManagedForm form
, Taxon taxon
) {
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
);
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
);
81 // Create a name composite for the accepted taxon
82 Composite acceptedNameComposite
=
83 new AcceptedNameComposite(groupComposite
, form
, taxon
);
85 // Return the name composite
86 return acceptedNameComposite
;
89 public Composite
createSynonym(Taxon taxon
, Synonym synonym
) {
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.");
95 // Get the synonym's homotypic group
96 HomotypicalGroup group
= synonym
.getHomotypicGroup();
97 Assert
.isNotNull(group
, "Synonym does not have a homotypic group");
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
);
106 // Create a synonym composite in the homotypic group
107 Composite synonymComposite
=
108 new SynonymComposite(groupComposite
, form
, taxon
, synonym
);
110 return synonymComposite
;
114 public Composite
createMisappliedName(Taxon taxon
, Taxon misappliedName
) {
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.");
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
);
128 // Create the name's composite
129 Composite composite
= new MisappliedNameComposite
130 (groupComposite
, form
, taxon
, misappliedName
);
136 public Composite
createMisappliedGroup(Taxon taxon
) {
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.");
142 // Create the group composite
143 Composite groupComposite
= new MisappliedGroupComposite(getTopLevelComposite(form
), taxon
);
144 groupComposite
.addFocusListener(new CompositeBorderDecorator(groupComposite
, form
));
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
);
153 return groupComposite
;
156 public Composite
createHomotypicalGroup(Taxon taxon
, HomotypicalGroup group
) {
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.");
162 // Create the group composite
163 Composite groupComposite
= new HomotypicalGroupComposite(getTopLevelComposite(form
), taxon
, group
);
164 groupComposite
.addFocusListener(new CompositeBorderDecorator(groupComposite
, form
));
166 return groupComposite
;
169 private Map
<Taxon
, IManagedForm
> getTaxonNameForms() {
170 if (nameForms
== null) {
171 nameForms
= new HashMap
<Taxon
, IManagedForm
>();
176 private IManagedForm
getNameForm(Taxon taxon
) {
177 return getTaxonNameForms().get(taxon
);
180 private Composite
getTopLevelComposite(IManagedForm form
) {
181 return form
.getForm().getBody();
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.");
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
);
198 // Create the name's composite
199 Composite composite
= ConceptComposite
.getNewInstance(groupComposite
, form
,
200 taxon
, relationship
);
206 public Composite
createConceptGroup(Taxon taxon
) {
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.");
212 // Create the group composite
213 Composite groupComposite
= new ConceptGroupComposite(getTopLevelComposite(form
), taxon
);
214 groupComposite
.addFocusListener(new CompositeBorderDecorator(groupComposite
, form
));
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
);
223 return groupComposite
;
228 public Composite
createDescriptionLabel(IManagedForm form
, Taxon taxon
,
229 TaxonDescription description
) {
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
);
235 // Create a group composite for the description
236 Composite groupComposite
= createDescriptionGroup(taxon
, description
);
238 // Create label composite
239 Composite labelComposite
=
240 new DescriptionLabelComposite(groupComposite
, form
, description
);
242 // Return the label composite
243 return labelComposite
;
247 public Composite
createDescriptionElement(Taxon taxon
, TaxonDescription description
,
248 DescriptionElementBase element
) {
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.");
254 // Get the element's feature
255 Feature feature
= element
.getFeature();
256 Assert
.isNotNull(feature
, "Element does not have a feature.");
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
);
265 // Create an element composite in the feature group according to its type
266 Composite elementComposite
= null;
267 if (element
.isInstanceOf(CommonTaxonName
.class)) {
269 new CommonNameComposite(groupComposite
, form
, (CommonTaxonName
) element
);
272 new DescriptionElementComposite(groupComposite
, form
, element
);
275 return elementComposite
;
279 public Composite
createDescriptionGroup(Taxon taxon
,
280 TaxonDescription description
) {
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.");
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);
291 return groupComposite
;
295 public Composite
createDescriptionFeatureGroup(Taxon taxon
,
296 TaxonDescription description
, Feature feature
) {
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.");
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.");
308 // Create the group composite
309 Composite groupComposite
= new DescriptionFeatureGroupComposite
310 (descriptionGroupComposite
, taxon
, description
, feature
);
311 groupComposite
.addFocusListener(new CompositeBorderDecorator(groupComposite
, form
));
313 return groupComposite
;
316 private Map
<Taxon
, IManagedForm
> getTaxonDescriptionForms() {
317 if (descriptionForms
== null) {
318 descriptionForms
= new HashMap
<Taxon
, IManagedForm
>();
320 return descriptionForms
;
324 private IManagedForm
getDescriptionForm(Taxon taxon
) {
325 return getTaxonDescriptionForms().get(taxon
);
329 * @see eu.etaxonomy.taxeditor.editor.IFreeTextElementFactory#createImageElement(org.eclipse.ui.forms.ManagedForm, eu.etaxonomy.cdm.model.taxon.Taxon, java.net.URL)
331 public Composite
createImageElement(ManagedForm form
, Taxon taxon
, URL url
) {
333 // Get the taxon's form
334 Assert
.isNotNull(form
, "A non-null IManagedForm must be provided.");
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
);
343 // Create the image composite
344 GroupedComposite imageComposite
= new ImageComposite(groupComposite
, form
, taxon
, url
);
345 imageComposite
.addFocusListener(new CompositeBorderDecorator(imageComposite
, form
));
347 return imageComposite
;
355 private GroupComposite
createImagesGroup(Taxon taxon
, ManagedForm form
) {
357 // Create the group composite
358 GroupComposite groupComposite
= new ImagesGroupComposite(getTopLevelComposite(form
), taxon
);
360 return groupComposite
;
367 private IManagedForm
getImagesForm(Taxon taxon
) {
368 return getTaxonImagesForms().get(taxon
);
374 private Map
<Taxon
, IManagedForm
> getTaxonImagesForms() {
375 if (imagesForms
== null) {
376 imagesForms
= new HashMap
<Taxon
, IManagedForm
>();
382 * @see eu.etaxonomy.taxeditor.editor.IFreeTextElementFactory#createTaxonHasNoImagesElement(org.eclipse.ui.forms.ManagedForm, eu.etaxonomy.cdm.model.taxon.Taxon)
384 public Composite
createTaxonHasNoImagesElement(ManagedForm form
,
386 // Get the taxon's form
387 Assert
.isNotNull(form
, "A non-null IManagedForm must be provided.");
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
);
396 // Create the image composite
397 GroupedComposite imageComposite
= new TaxonHasNoImagesComposite(groupComposite
, form
, taxon
);
399 return imageComposite
;