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