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