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