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