Replaced method to create new name using preferred nomenclatural code w existing...
[taxeditor.git] / taxeditor-editor / src / main / java / 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 org.apache.log4j.Logger;
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.ui.forms.widgets.FormToolkit;
16
17 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
18 import eu.etaxonomy.cdm.model.taxon.Synonym;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
21 import eu.etaxonomy.taxeditor.editor.name.AcceptedNameComposite;
22 import eu.etaxonomy.taxeditor.editor.name.ConceptComposite;
23 import eu.etaxonomy.taxeditor.editor.name.ConceptGroupComposite;
24 import eu.etaxonomy.taxeditor.editor.name.HomotypicalGroupComposite;
25 import eu.etaxonomy.taxeditor.editor.name.MisappliedGroupComposite;
26 import eu.etaxonomy.taxeditor.editor.name.MisappliedNameComposite;
27 import eu.etaxonomy.taxeditor.editor.name.SynonymComposite;
28 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
29
30 /**
31 * @author p.ciardelli
32 * @created 08.01.2009
33 * @version 1.0
34 */
35 public class FreeTextElementFactory implements IFreeTextElementFactory {
36 private static final Logger logger = Logger
37 .getLogger(FreeTextElementFactory.class);
38
39 private static IFreeTextElementFactory factory;
40
41 public static IFreeTextElementFactory getDefault() {
42 if (factory == null) {
43 factory = new FreeTextElementFactory();
44 }
45 logger.trace("Created a new FreeTextElementFactory instance.");
46 return factory;
47 }
48
49 public Composite createAcceptedTaxon(TaxonNameEditor editor) {
50 // Create a homotypic group composite for the accepted taxon
51 HomotypicalGroup group = editor.getTaxon().getHomotypicGroup();
52 Assert.isNotNull(group, "Taxon does not have a homotypic group");
53 Composite groupComposite = createHomotypicalGroup(editor, group);
54
55 // Create a name composite for the accepted taxon
56 Composite acceptedNameComposite = new AcceptedNameComposite(editor, groupComposite);
57
58 // Return the name composite
59 return acceptedNameComposite;
60 }
61
62 public Composite createSynonym(TaxonNameEditor editor, Synonym synonym) {
63 // Get the synonym's homotypic group
64 HomotypicalGroup group = synonym.getHomotypicGroup();
65 Assert.isNotNull(group, "Synonym does not have a homotypic group");
66
67 // If the group doesn't yet have a composite, create one and add it to the repository
68 Composite groupComposite = editor.getHomotypicGroup(group);
69 if (groupComposite == null) {
70 groupComposite = createHomotypicalGroup(editor, group);
71 }
72
73 // Create a synonym composite in the homotypical group
74 Composite synonymComposite = new SynonymComposite(editor, groupComposite, synonym);
75
76 return synonymComposite;
77 }
78
79
80 public Composite createMisappliedName(TaxonNameEditor editor, Taxon misappliedName) {
81
82 // If there is no composite for misapplied names,
83 // create one and add it to the repository
84 Composite groupComposite = editor.getMisappliedGroup();
85 if (groupComposite == null) {
86 groupComposite = createMisappliedGroup(editor);
87 }
88
89 // Create the name's composite
90 Composite composite = new MisappliedNameComposite(editor, groupComposite, misappliedName);
91
92 return composite;
93 }
94
95
96 public Composite createMisappliedGroup(TaxonNameEditor editor) {
97 // Create the group composite
98 Composite groupComposite = new MisappliedGroupComposite(editor, editor.getTopLevelComposite());
99 groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, editor.getManagedForm()));
100
101 // Put the group composite before concept group composite, if any
102 Composite conceptGroupComposite = editor.getConceptGroup();
103 if (conceptGroupComposite != null) {
104 groupComposite.moveAbove(conceptGroupComposite);
105 }
106
107 return groupComposite;
108 }
109
110 public Composite createHomotypicalGroup(TaxonNameEditor editor, HomotypicalGroup group) {
111
112 // Create the group composite
113 Composite groupComposite = new HomotypicalGroupComposite(editor, editor.getTopLevelComposite(), group);
114 // groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, editor.getManagedForm()));
115
116 groupComposite.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
117 editor.getManagedForm().getToolkit().paintBordersFor(groupComposite.getParent());
118
119 return groupComposite;
120 }
121
122 public Composite createConcept(TaxonNameEditor editor, TaxonRelationship relationship) {
123 // If there is no composite for misapplied names,
124 // create one and add it to the repository
125 Composite groupComposite = editor.getConceptGroup();
126 if (groupComposite == null) {
127 groupComposite = createConceptGroup(editor);
128 }
129
130 // Create the name's composite
131 Composite composite = ConceptComposite.getNewInstance(editor, groupComposite, relationship);
132
133 return composite;
134 }
135
136
137 public Composite createConceptGroup(TaxonNameEditor editor) {
138 // Create the group composite
139 Composite groupComposite = new ConceptGroupComposite(editor, editor.getTopLevelComposite());
140 groupComposite.addFocusListener(new CompositeBorderDecorator(groupComposite, editor.getManagedForm()));
141
142 // Put the group composite after misapplied group composite, if any
143 Composite misappliedGroupComposite = editor.getMisappliedGroup();
144 if (misappliedGroupComposite != null) {
145 groupComposite.moveBelow(misappliedGroupComposite);
146 }
147
148 return groupComposite;
149
150 }
151 }