- set CdmFormFactory.createTermComboElement() to deprecated
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / NameDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.section.name;
12
13 import java.util.Arrays;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.swt.SWT;
17
18 import eu.etaxonomy.cdm.model.common.TermType;
19 import eu.etaxonomy.cdm.model.name.BotanicalName;
20 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
21 import eu.etaxonomy.cdm.model.name.NonViralName;
22 import eu.etaxonomy.cdm.model.name.Rank;
23 import eu.etaxonomy.cdm.model.name.ZoologicalName;
24 import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
25 import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
28 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
30 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
31 import eu.etaxonomy.taxeditor.ui.element.NumberWithLabelElement;
32 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
33 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
34 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
35
36 /**
37 * <p>NameDetailElement class.</p>
38 *
39 * @author n.hoffmann
40 * @created Feb 26, 2010
41 * @version 1.0
42 */
43 public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<NonViralName> implements ISelectableElement, IEnableableFormElement{
44
45 private final Logger logger = Logger.getLogger(NameDetailElement.class);
46
47 private TermComboElement<Rank> combo_rank;
48 private TextWithLabelElement text_appendedPhrase;
49 private TextWithLabelElement text_uninomial;
50 private TextWithLabelElement text_infragenericEpithet;
51 private TextWithLabelElement text_specificEpithet;
52 private TextWithLabelElement text_infraspecificEpithet;
53 private SelectionArbitrator selectionArbitrator;
54 private CheckboxElement checkbox_anamorphic;
55 private TextWithLabelElement text_breed;
56 private NumberWithLabelElement text_publicationYear;
57 private NumberWithLabelElement text_originalPublicationYear;
58 private int cursorPosition;
59
60 /**
61 * <p>Constructor for NameDetailElement.</p>
62 *
63 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
64 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
65 * @param style a int.
66 */
67 public NameDetailElement(CdmFormFactory cdmFormFactory, ICdmFormElement formElement,
68 int style) {
69 super(cdmFormFactory, formElement);
70 // register as selection listener
71 if(cdmFormFactory.getSelectionProvider() != null){
72 selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
73 }
74 }
75
76 /** {@inheritDoc} */
77 @Override
78 protected void createControls(ICdmFormElement formElement, NonViralName nonViralName, int style) {
79
80 toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
81
82 combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style);
83
84 createGenusOrUninomialControls(this, nonViralName, style);
85 createInfragenerericEpithetControls(this, nonViralName, style);
86 createSpecificEpithetControls(this, nonViralName, style);
87 createInfraSpecificEpithetControls(this, nonViralName, style);
88
89 createSpecificNameParts(this, nonViralName, style);
90
91 text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
92
93 }
94
95 /**
96 * <p>clearCheckRankWarnings</p>
97 */
98 protected void clearCheckRankWarnings() {
99 if(getEntity().hasProblem(ParserProblem.CheckRank)){
100 getEntity().removeParsingProblem(ParserProblem.CheckRank);
101
102 // FIXME this has to be reafctored completely. See tickets #1959, #1663, #1957, #1958
103 // TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
104 // nameEditor.getSelectedContainer().getNameViewer().clearErrors();
105 }
106 }
107
108 /** {@inheritDoc} */
109 @Override
110 protected void updateContent() {
111 if(getEntity() == null){
112 setEntity(NonViralName.NewInstance(null));
113 }
114
115 super.updateContent();
116
117 if(isIrrelevant()){
118 setIrrelevant(isIrrelevant());
119 }else{
120 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
121 }
122 }
123
124
125 private void createSpecificNameParts(ICdmFormElement formElement,
126 NonViralName nonViralName, int style) {
127 NomenclaturalCode code = nonViralName.getNomenclaturalCode();
128 if (code != null){
129 switch(nonViralName.getNomenclaturalCode()){
130 case ICNAFP :
131 createBotanicalNameParts(formElement, nonViralName, style);
132 break;
133 case ICZN:
134 createZoologicalNameParts(formElement, nonViralName, style);
135 break;
136 case ICVCN:
137 //TODO implement
138 logger.warn("ICVCN not yet implemented");
139 break;
140 case ICNB:
141 //TODO implement
142 logger.warn("ICNB not yet implemented");
143 break;
144 case ICNCP:
145 //TODO implement
146 logger.warn("ICNCP not yet implemented");
147 break;
148 }
149 }
150 }
151
152 private void createBotanicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
153 BotanicalName botanicalName = (BotanicalName) nonViralName;
154 checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", botanicalName.isAnamorphic(), style);
155 }
156
157 private void createZoologicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
158 ZoologicalName zoologicalName = (ZoologicalName) nonViralName;
159 text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
160 text_publicationYear = formFactory.createNumberTextWithLabelElement(formElement, "Publication Year", zoologicalName.getPublicationYear(), style);
161 text_originalPublicationYear = formFactory.createNumberTextWithLabelElement(formElement, "Orig. Publication Year", zoologicalName.getOriginalPublicationYear(), style);
162 }
163
164 private void createGenusOrUninomialControls(ICdmFormElement element, NonViralName nonViralName, int style){
165 String title = "Genus";
166 Rank rank = nonViralName.getRank();
167 if(rank != null && rank.isSupraGeneric()){
168 title = "Uninomial";
169 }
170 text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
171 }
172
173 private void createInfragenerericEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
174 if(nonViralName.getRank() != null && nonViralName.getRank().isInfraGeneric() && !nonViralName.getRank().isSpeciesAggregate()){
175 text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
176 }
177 }
178
179 private void createSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
180 if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies() || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
181 text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
182 }
183 }
184
185 private void createInfraSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
186 if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
187 text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
188 }
189 }
190
191 /** {@inheritDoc} */
192 @Override
193 public void handleEvent(Object eventSource){
194 if (eventSource == toggleable_cache) {
195 getEntity().setNameCache(toggleable_cache.getText(),
196 toggleable_cache.getState());
197 if (!isIrrelevant()) {
198 setIrrelevant(toggleable_cache.getState(),
199 Arrays.asList(new Object[] { toggleable_cache }));
200 }
201 }
202 else if(eventSource == combo_rank){
203 getEntity().setRank(combo_rank.getSelection());
204 clearCheckRankWarnings();
205 updateContent();
206 }
207 else if(eventSource == text_appendedPhrase){
208 getEntity().setAppendedPhrase(text_appendedPhrase.getText());
209 }
210 else if(eventSource == text_infragenericEpithet){
211 getEntity().setInfraGenericEpithet(text_infragenericEpithet.getText());
212 }
213 else if(eventSource == text_infraspecificEpithet){
214 getEntity().setInfraSpecificEpithet(text_infraspecificEpithet.getText());
215 }
216 else if(eventSource == text_specificEpithet){
217 getEntity().setSpecificEpithet(text_specificEpithet.getText());
218 }
219 else if(eventSource == text_uninomial){
220 getEntity().setGenusOrUninomial(text_uninomial.getText());
221 }
222 else if(eventSource == checkbox_anamorphic){
223 ((BotanicalName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
224 }
225 }
226
227 /*
228 * (non-Javadoc)
229 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
230 */
231 /**
232 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
233 *
234 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator} object.
235 */
236 @Override
237 public SelectionArbitrator getSelectionArbitrator() {
238 return selectionArbitrator;
239 }
240
241 @Override
242 public void updateToggleableCacheField() {
243 if(! getEntity().isProtectedNameCache()){
244 toggleable_cache.setText(getEntity().getNameCache());
245 }
246 }
247 }