Add missing switch statement
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / NameDetailElement.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.ui.section.name;
11
12 import java.util.Arrays;
13 import java.util.Collection;
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.IFungusName;
20 import eu.etaxonomy.cdm.model.name.INonViralName;
21 import eu.etaxonomy.cdm.model.name.IZoologicalName;
22 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
23 import eu.etaxonomy.cdm.model.name.Rank;
24 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
25 import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
26 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
27 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28 import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
29 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
31 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
33 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
34 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
35 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
36 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
37
38 /**
39 * <p>NameDetailElement class.</p>
40 *
41 * @author n.hoffmann
42 * @created Feb 26, 2010
43 */
44 public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<INonViralName> implements ISelectableElement, IEnableableFormElement{
45
46 private final Logger logger = Logger.getLogger(NameDetailElement.class);
47
48 private TermComboElement<Rank> combo_rank;
49 private TextWithLabelElement text_appendedPhrase;
50 private TextWithLabelElement text_uninomial;
51 private TextWithLabelElement text_infragenericEpithet;
52 private TextWithLabelElement text_specificEpithet;
53 private TextWithLabelElement text_infraspecificEpithet;
54 private SelectionArbitrator selectionArbitrator;
55 private CheckboxElement checkbox_anamorphic;
56 private TextWithLabelElement text_breed;
57 //private NumberWithLabelElement text_publicationYear;
58 //private NumberWithLabelElement text_originalPublicationYear;
59 private int cursorPosition;
60
61 /**
62 * <p>Constructor for NameDetailElement.</p>
63 *
64 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
65 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
66 * @param style a int.
67 */
68 public NameDetailElement(CdmFormFactory cdmFormFactory, ICdmFormElement formElement,
69 int style) {
70 super(cdmFormFactory, formElement);
71 // make this element selectable
72 if(cdmFormFactory.getSelectionProvider() != null){
73 selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
74 }
75 }
76
77 /** {@inheritDoc} */
78 @Override
79 protected void createControls(ICdmFormElement formElement, INonViralName nonViralName, int style) {
80
81 if (isAdvancedView){
82 toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
83 combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style);
84 createGenusOrUninomialControls(this, nonViralName, style);
85 createInfragenerericEpithetControls(this, nonViralName, style);
86 createSpecificEpithetControls(this, nonViralName, style);
87 createInfraSpecificEpithetControls(this, nonViralName, style);
88 createSpecificNameParts(this, nonViralName, style);
89 text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
90 }else{
91 if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE)){
92 toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
93 }
94
95 if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK)){
96 combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style);
97 }
98
99 if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS)){
100 createGenusOrUninomialControls(this, nonViralName, style);
101 createInfragenerericEpithetControls(this, nonViralName, style);
102 createSpecificEpithetControls(this, nonViralName, style);
103 createInfraSpecificEpithetControls(this, nonViralName, style);
104 createSpecificNameParts(this, nonViralName, style);
105 }
106 if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE)){
107 text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
108 }
109 }
110
111 }
112
113 /**
114 * <p>clearCheckRankWarnings</p>
115 */
116 protected void clearCheckRankWarnings() {
117 if(getEntity().hasProblem(ParserProblem.CheckRank)){
118 getEntity().removeParsingProblem(ParserProblem.CheckRank);
119
120 // FIXME this has to be reafctored completely. See tickets #1959, #1663, #1957, #1958
121 // TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
122 // nameEditor.getSelectedContainer().getNameViewer().clearErrors();
123 }
124 }
125
126 /** {@inheritDoc} */
127 @Override
128 protected void updateContent() {
129 if(getEntity() == null){
130 setEntity(TaxonNameFactory.NewNonViralInstance(null));
131 }
132
133 super.updateContent();
134 if (toggleable_cache != null){
135 toggleable_cache.setEnabled(getEntity().isProtectedNameCache());
136
137 if (isAdvancedView){
138 if(isIrrelevant()){
139 setIrrelevant(isIrrelevant());
140 }else{
141 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
142 }
143 }
144 }
145 }
146
147
148 private void createSpecificNameParts(ICdmFormElement formElement,
149 INonViralName nonViralName, int style) {
150 NomenclaturalCode code = nonViralName.getNomenclaturalCode();
151 if (code != null){
152 switch(nonViralName.getNomenclaturalCode()){
153 case ICNAFP :
154 //nothing do add
155 createBotanicalNameParts(formElement, nonViralName, style);
156 break;
157 case Fungi :
158 if (isAdvancedView){
159 createFungusNameParts(formElement, nonViralName, style);
160 }
161 break;
162 case ICZN:
163 createZoologicalNameParts(formElement, nonViralName, style);
164 break;
165 case ICVCN:
166 //TODO implement
167 logger.warn("ICVCN not yet implemented");
168 break;
169 case ICNB:
170 //TODO implement
171 logger.warn("ICNB not yet implemented");
172 break;
173 case ICNCP:
174 //TODO implement
175 logger.warn("ICNCP not yet implemented");
176 break;
177 case NonViral:
178 //TODO implement
179 logger.warn("NonViral not yet implemented");
180 break;
181 }
182 }
183 }
184
185
186 private void createBotanicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
187 //nothing to do
188 }
189
190 private void createFungusNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
191 IFungusName fungusName = (IFungusName)nonViralName;
192 checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", fungusName.isAnamorphic(), style);
193 }
194
195 private void createZoologicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
196 IZoologicalName zoologicalName = (IZoologicalName) nonViralName;
197
198 if (isAdvancedView){
199 text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
200 }
201 }
202
203 private void createGenusOrUninomialControls(ICdmFormElement element, INonViralName nonViralName, int style){
204 String title = "Genus";
205 Rank rank = nonViralName.getRank();
206 if(rank != null && rank.isSupraGeneric()){
207 title = "Uninomial";
208 }
209 text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
210 }
211
212 private void createInfragenerericEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
213 if (nonViralName.getNomenclaturalCode() == null || nonViralName.getNomenclaturalCode().equals(NomenclaturalCode.ICZN)){
214 if(nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS()) ){
215 text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
216 }
217 } else{
218 if((nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS())) && !nonViralName.getRank().isLower(Rank.SPECIES()) && !nonViralName.getRank().isSpeciesAggregate()){
219 text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
220 }
221 }
222 }
223
224 private void createSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
225 if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies() || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
226 text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
227 }
228 }
229
230 private void createInfraSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
231 if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
232 text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
233 }
234 }
235
236 /** {@inheritDoc} */
237 @Override
238 public void handleEvent(Object eventSource){
239 if (eventSource == toggleable_cache) {
240 getEntity().setNameCache(toggleable_cache.getText(),
241 toggleable_cache.getState());
242 //if (!isIrrelevant()) {
243 setIrrelevant(toggleable_cache.getState(),
244 Arrays.asList(new Object[] { toggleable_cache }));
245 //}
246 }
247 else if(eventSource == combo_rank){
248 getEntity().setRank(combo_rank.getSelection());
249 clearCheckRankWarnings();
250 updateContent();
251 }
252 else if(eventSource == text_appendedPhrase){
253 getEntity().setAppendedPhrase(text_appendedPhrase.getText());
254 }
255 else if(eventSource == text_infragenericEpithet){
256 getEntity().setInfraGenericEpithet(text_infragenericEpithet.getText());
257 }
258 else if(eventSource == text_infraspecificEpithet){
259 getEntity().setInfraSpecificEpithet(text_infraspecificEpithet.getText());
260 }
261 else if(eventSource == text_specificEpithet){
262 getEntity().setSpecificEpithet(text_specificEpithet.getText());
263 }
264 else if(eventSource == text_uninomial){
265 getEntity().setGenusOrUninomial(text_uninomial.getText());
266 }
267 else if(eventSource == checkbox_anamorphic){
268 ((IFungusName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
269 }else if(eventSource == text_breed){
270 ((IZoologicalName)getEntity()).setBreed(text_breed.getText());
271 }
272
273 }
274
275 /*
276 * (non-Javadoc)
277 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
278 */
279 /**
280 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
281 *
282 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator} object.
283 */
284 @Override
285 public SelectionArbitrator getSelectionArbitrator() {
286 return selectionArbitrator;
287 }
288
289 @Override
290 public void updateToggleableCacheField() {
291 if (toggleable_cache != null){
292 if(! getEntity().isProtectedNameCache()){
293 toggleable_cache.setText(getEntity().getNameCache());
294 }
295 }
296 }
297
298 @Override
299 protected void handleToggleableCacheField() {
300 boolean pushedState = toggleable_cache.getState();
301
302 getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
303 setIrrelevant(pushedState, Arrays.asList(new Object[] { toggleable_cache, text_appendedPhrase, checkbox_anamorphic }));
304 updateToggleableCacheField();
305 }
306
307 @Override
308 public void setIrrelevant(boolean irrelevant, Collection<Object> except){
309 if (toggleable_cache != null){
310 if (toggleable_cache.getState() == irrelevant){
311 super.setIrrelevant(irrelevant, except);
312 }
313 if (except == null || except.isEmpty()){
314 toggleable_cache.setIrrelevant(irrelevant);
315 }
316 }
317
318 }
319
320
321
322
323 }