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