Fixes #2422
[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.eclipse.swt.SWT;
16
17 import eu.etaxonomy.cdm.model.name.BotanicalName;
18 import eu.etaxonomy.cdm.model.name.NonViralName;
19 import eu.etaxonomy.cdm.model.name.Rank;
20 import eu.etaxonomy.cdm.model.name.ZoologicalName;
21 import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
22 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
23 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.TermComboType;
24 import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
25 import eu.etaxonomy.taxeditor.ui.forms.CheckboxElement;
26 import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
27 import eu.etaxonomy.taxeditor.ui.forms.IEnableableFormElement;
28 import eu.etaxonomy.taxeditor.ui.forms.ISelectableElement;
29 import eu.etaxonomy.taxeditor.ui.forms.NumberWithLabelElement;
30 import eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator;
31 import eu.etaxonomy.taxeditor.ui.forms.TextWithLabelElement;
32 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
33 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
34 import eu.etaxonomy.taxeditor.ui.term.AbstractTermComboElement;
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 AbstractTermComboElement<Rank> combo_rank;
46 private TextWithLabelElement text_appendedPhrase;
47 private TextWithLabelElement text_uninomial;
48 private TextWithLabelElement text_infragenericEpithet;
49 private TextWithLabelElement text_specificEpithet;
50 private TextWithLabelElement text_infraspecificEpithet;
51 private SelectionArbitrator selectionArbitrator;
52 private CheckboxElement checkbox_anamorphic;
53 private TextWithLabelElement text_breed;
54 private NumberWithLabelElement text_publicationYear;
55 private NumberWithLabelElement text_originalPublicationYear;
56 private int cursorPosition;
57
58 /**
59 * <p>Constructor for NameDetailElement.</p>
60 *
61 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
62 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
63 * @param style a int.
64 */
65 public NameDetailElement(CdmFormFactory cdmFormFactory, ICdmFormElement formElement,
66 int style) {
67 super(cdmFormFactory, formElement);
68 // register as selection listener
69 if(cdmFormFactory.getSelectionProvider() != null){
70 selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
71 }
72 }
73
74 /** {@inheritDoc} */
75 @Override
76 protected void createControls(ICdmFormElement formElement, NonViralName nonViralName, int style) {
77
78 toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
79
80 combo_rank = formFactory.createTermComboElement(TermComboType.RANK, this, "Rank", nonViralName.getRank(), style);
81
82 createGenusOrUninomialControls(this, nonViralName, style);
83 createInfragenerericEpithetControls(this, nonViralName, style);
84 createSpecificEpithetControls(this, nonViralName, style);
85 createInfraSpecificEpithetControls(this, nonViralName, style);
86
87 createSpecificNameParts(this, nonViralName, style);
88
89 text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
90
91 }
92
93 /**
94 * <p>clearCheckRankWarnings</p>
95 */
96 protected void clearCheckRankWarnings() {
97 if(getEntity().hasProblem(ParserProblem.CheckRank)){
98 getEntity().removeParsingProblem(ParserProblem.CheckRank);
99
100 // FIXME this has to be reafctored completely. See tickets #1959, #1663, #1957, #1958
101 // TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
102 // nameEditor.getSelectedContainer().getNameViewer().clearErrors();
103 }
104 }
105
106 /** {@inheritDoc} */
107 @Override
108 protected void updateContent() {
109 if(getEntity() == null){
110 setEntity(NonViralName.NewInstance(null));
111 }
112
113 super.updateContent();
114
115 if(isIrrelevant()){
116 setIrrelevant(isIrrelevant());
117 }else{
118 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
119 }
120 }
121
122
123 private void createSpecificNameParts(ICdmFormElement formElement,
124 NonViralName nonViralName, int style) {
125 switch(nonViralName.getNomenclaturalCode()){
126 case ICBN:
127 createBotanicalNameParts(formElement, nonViralName, style);
128 break;
129 case ICZN:
130 createZoologicalNameParts(formElement, nonViralName, style);
131 break;
132 }
133 }
134
135 private void createBotanicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
136 BotanicalName botanicalName = (BotanicalName) nonViralName;
137 checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", botanicalName.isAnamorphic(), style);
138 }
139
140 private void createZoologicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
141 ZoologicalName zoologicalName = (ZoologicalName) nonViralName;
142 text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
143 text_publicationYear = formFactory.createIntegerTextWithLabelElement(formElement, "Publication Year", zoologicalName.getPublicationYear(), style);
144 text_originalPublicationYear = formFactory.createIntegerTextWithLabelElement(formElement, "Orig. Publication Year", zoologicalName.getOriginalPublicationYear(), style);
145 }
146
147 private void createGenusOrUninomialControls(ICdmFormElement element, NonViralName nonViralName, int style){
148 String title = "Genus";
149 Rank rank = nonViralName.getRank();
150 if(rank != null && rank.isSupraGeneric()){
151 title = "Uninomial";
152 }
153 text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
154 }
155
156 private void createInfragenerericEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
157 if(nonViralName.getRank() != null && nonViralName.getRank().isInfraGeneric() && !nonViralName.getRank().isSpeciesAggregate()){
158 text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
159 }
160 }
161
162 private void createSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
163 if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies() || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
164 text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
165 }
166 }
167
168 private void createInfraSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
169 if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
170 text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
171 }
172 }
173
174 /** {@inheritDoc} */
175 @Override
176 public void handleEvent(Object eventSource){
177 if(eventSource == combo_rank){
178 getEntity().setRank(combo_rank.getSelection());
179 clearCheckRankWarnings();
180 }
181 else if(eventSource == text_appendedPhrase){
182 getEntity().setAppendedPhrase(text_appendedPhrase.getText());
183 }
184 else if(eventSource == toggleable_cache){
185 handleToggleableCacheField();
186 }
187 else if(eventSource == text_infragenericEpithet){
188 getEntity().setInfraGenericEpithet(text_infragenericEpithet.getText());
189 }
190 else if(eventSource == text_infraspecificEpithet){
191 getEntity().setInfraSpecificEpithet(text_infraspecificEpithet.getText());
192 }
193 else if(eventSource == text_specificEpithet){
194 getEntity().setSpecificEpithet(text_specificEpithet.getText());
195 }
196 else if(eventSource == text_uninomial){
197 getEntity().setGenusOrUninomial(text_uninomial.getText());
198 }
199 else if(eventSource == checkbox_anamorphic){
200 ((BotanicalName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
201 }
202
203
204 if(eventSource != toggleable_cache && !toggleable_cache.getLayoutComposite().isDisposed()){
205 toggleable_cache.setText(getEntity().getNameCache());
206 }
207
208 // // we have to notify the parent if this is embedded in the nonviral name section
209 // // maybe we can handle this a little bit more elegant
210 // if(getParentElement() instanceof AbstractCdmDetailSection)
211 // firePropertyChangeEvent(new CdmPropertyChangeEvent(getParentElement(), null));
212 }
213
214 /*
215 * (non-Javadoc)
216 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
217 */
218 /**
219 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
220 *
221 * @return a {@link eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator} object.
222 */
223 public SelectionArbitrator getSelectionArbitrator() {
224 return selectionArbitrator;
225 }
226
227 @Override
228 public void updateToggleableCacheField() {
229 if(! getEntity().isProtectedNameCache()){
230 toggleable_cache.setText(getEntity().getNameCache());
231 }
232 }
233 }