2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.section
.name
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Comparator
;
13 import java
.util
.List
;
15 import org
.apache
.logging
.log4j
.LogManager
;import org
.apache
.logging
.log4j
.Logger
;
16 import org
.eclipse
.swt
.SWT
;
18 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferencePredicate
;
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
.DefinedTermBase
;
28 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
29 import eu
.etaxonomy
.cdm
.strategy
.parser
.ParserProblem
;
30 import eu
.etaxonomy
.taxeditor
.preference
.NameDetailsConfigurator
;
31 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
32 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
33 import eu
.etaxonomy
.taxeditor
.ui
.combo
.term
.TermComboElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.CheckboxElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
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
;
43 * <p>NameDetailElement class.</p>
46 * @created Feb 26, 2010
48 public class NameDetailElement
49 extends AbstractIdentifiableEntityDetailElement
<INonViralName
>
50 implements ISelectableElement
{
52 private final Logger logger
= LogManager
.getLogger(NameDetailElement
.class);
54 private SelectionArbitrator selectionArbitrator
;
56 private TermComboElement
<Rank
> combo_rank
;
57 private TextWithLabelElement text_appendedPhrase
;
58 private TextWithLabelElement text_uninomial
;
59 private TextWithLabelElement text_infragenericEpithet
;
60 private TextWithLabelElement text_specificEpithet
;
61 private TextWithLabelElement text_infraspecificEpithet
;
62 private CheckboxElement checkbox_anamorphic
;
63 private TextWithLabelElement text_cultivarName
;
64 private TextWithLabelElement text_cultivarGroup
;
65 private TextWithLabelElement text_subGenusAuthorship
;
66 private TextWithLabelElement text_breed
;
68 private NameDetailsConfigurator config
;
71 * <p>Constructor for NameDetailElement.</p>
73 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
74 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
77 public NameDetailElement(CdmFormFactory cdmFormFactory
, ICdmFormElement formElement
,
79 super(cdmFormFactory
, formElement
);
80 // make this element selectable
84 protected void createControls(ICdmFormElement formElement
, INonViralName nonViralName
, int style
) {
85 Comparator
<Rank
> comparator
= null;
86 config
= PreferencesUtil
.getPreferredNameDetailsConfiguration();
88 if(PreferencesUtil
.getSortRanksHierarchichally()){
89 comparator
= new Comparator
<Rank
>(){
91 public int compare(Rank o1
, Rank o2
) {
95 return -1*o1
.compareTo(o2
);
99 if (nonViralName
.getRank() != null && !(nonViralName
.getRank().isCultivar() == nonViralName
.getNameType().equals(NomenclaturalCode
.ICNCP
))){
100 nonViralName
.setRank(null);
103 toggleable_cache
= formFactory
.createToggleableTextField(this, "Name Cache", nonViralName
.getNameCache(), nonViralName
.isProtectedNameCache(), style
);
104 List
<Rank
> ranks
= getRanks(nonViralName
, nonViralName
.getNameType());
106 combo_rank
= formFactory
.createDefinedTermComboElement(ranks
, this, "Rank", nonViralName
.getRank(),style
, comparator
);
108 createGenusOrUninomialControls(this, nonViralName
, style
);
109 createInfragenerericEpithetControls(this, nonViralName
, style
);
110 createSpecificEpithetControls(this, nonViralName
, style
);
111 createInfraSpecificEpithetControls(this, nonViralName
, style
);
112 createSpecificNameParts(this, nonViralName
, style
);
113 text_appendedPhrase
= formFactory
.createTextWithLabelElement(formElement
, "Appended Phrase", nonViralName
.getAppendedPhrase(), style
);
115 if (config
.isNameCacheActivated()){
116 toggleable_cache
= formFactory
.createToggleableTextField(this, "Name Cache", nonViralName
.getNameCache(), nonViralName
.isProtectedNameCache(), style
);
119 if (config
.isRankActivated()){
120 List
<Rank
> ranks
= getRanks(nonViralName
, nonViralName
.getNameType());
121 combo_rank
= formFactory
.createDefinedTermComboElement(ranks
, this, "Rank", nonViralName
.getRank(),style
, comparator
);
124 if (config
.isAtomisedEpithetsActivated()){
125 createGenusOrUninomialControls(this, nonViralName
, style
);
126 createInfragenerericEpithetControls(this, nonViralName
, style
);
127 createSpecificEpithetControls(this, nonViralName
, style
);
128 createInfraSpecificEpithetControls(this, nonViralName
, style
);
129 createSpecificNameParts(this, nonViralName
, style
);
131 if (config
.isAppendedPhraseActivated()){
132 text_appendedPhrase
= formFactory
.createTextWithLabelElement(formElement
, "Appended Phrase", nonViralName
.getAppendedPhrase(), style
);
135 if(this.getFormFactory().getSelectionProvider() != null){
136 selectionArbitrator
= this.getFormFactory().createSelectionArbitrator(this);
138 for (ICdmFormElement element
: this.getElements()){
139 element
.setBackground(getPersistentBackground());
141 ((NameDetailSection
)this.getParentElement()).layout();
143 registerCacheRelevance(combo_rank
);
144 //TODO decide if relevant
145 registerCacheRelevance(text_appendedPhrase
);
146 registerCacheRelevance(text_uninomial
);
147 registerCacheRelevance(text_infragenericEpithet
);
148 registerCacheRelevance(text_specificEpithet
);
149 registerCacheRelevance(text_infraspecificEpithet
);
150 registerCacheRelevance(text_cultivarName
);
151 registerCacheRelevance(text_subGenusAuthorship
);
152 registerCacheRelevance(text_breed
);
154 registerCacheRelevance(toggleable_cache
, cacheRelevanceHelper
.getDependenciesArray());
158 * clearCheckRankWarnings
160 protected void clearCheckRankWarnings() {
161 if(getEntity().hasProblem(ParserProblem
.CheckRank
)){
162 getEntity().removeParsingProblem(ParserProblem
.CheckRank
);
164 // FIXME this has to be refactored completely. See tickets #1959, #1663, #1957, #1958
165 // TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
166 // nameEditor.getSelectedContainer().getNameViewer().clearErrors();
171 public void updateContent() {
172 if(getEntity() == null){
173 setEntity(TaxonNameFactory
.NewNonViralInstance(null));
176 super.updateContent();
177 if (toggleable_cache
!= null){
178 toggleable_cache
.setCacheEnabled(getEntity().isProtectedNameCache());
181 updateCacheRelevance();
186 private void createSpecificNameParts(ICdmFormElement formElement
,
187 INonViralName nonViralName
, int style
) {
188 NomenclaturalCode code
= nonViralName
.getNameType();
193 if (nonViralName
.getRank() != null && nonViralName
.getRank().isCultivar()){
194 nonViralName
.setRank(null);
196 createBotanicalNameParts(formElement
, nonViralName
, style
);
201 createFungusNameParts(formElement
, nonViralName
, style
);
205 createZoologicalNameParts(formElement
, nonViralName
, style
);
209 logger
.warn("ICVCN not yet implemented");
212 createBacterialNameParts(formElement
, nonViralName
, style
);
215 if (nonViralName
.getRank() != null && !nonViralName
.getRank().isCultivar()){
216 nonViralName
.setRank(null);
218 createCultivarNameParts(formElement
, nonViralName
, style
);
222 logger
.warn("NonViral not yet implemented");
230 private void createBacterialNameParts(ICdmFormElement formElement
, INonViralName nonViralName
, int style
) {
231 IBacterialName bacterialName
= (IBacterialName
) nonViralName
;
232 text_subGenusAuthorship
= formFactory
.createTextWithLabelElement(formElement
, "Subgenus Authorship", bacterialName
.getSubGenusAuthorship(), style
);
233 // text_nameApprobation = formFactory.createTextWithLabelElement(formElement, "Name Approbation", bacterialName.getNameApprobation(), style);
237 private void createCultivarNameParts(ICdmFormElement formElement
, INonViralName nonViralName
, int style
) {
238 ICultivarPlantName cultivarName
= (ICultivarPlantName
)nonViralName
;
240 List
<Rank
> cultivarRanks
= getRanks(cultivarName
, nonViralName
.getNameType());
241 combo_rank
.setTerms(cultivarRanks
);
242 combo_rank
.setSelection(cultivarName
.getRank());
244 Rank rank
= cultivarName
.getRank();
245 if (rank
!= null && (rank
.equals(Rank
.CULTIVAR()) || rank
.equals(Rank
.CULTIVARGROUP()) || rank
.equals(Rank
.GREX_ICNCP()))){
246 text_cultivarGroup
= formFactory
.createTextWithLabelElement(formElement
, "Cultivar Group Epi", cultivarName
.getCultivarGroupEpithet(), style
);
248 if(rank
!= null && rank
.equals(Rank
.CULTIVAR())){
249 text_cultivarName
= formFactory
.createTextWithLabelElement(formElement
, "Cultivar Epithet", cultivarName
.getCultivarEpithet(), style
);
254 private List
<Rank
> getRanks(INonViralName name
, NomenclaturalCode code
) {
255 List
<Rank
> ranks
= new ArrayList
<>();
256 String availableRanks
= PreferencesUtil
.getStringValue(PreferencePredicate
.AvailableRanks
.getKey());
257 List
<DefinedTermBase
> allRanks
= CdmStore
.getTermManager().createTermListFromString(availableRanks
, TermType
.Rank
);
258 for(DefinedTermBase term
: allRanks
){
259 Rank rank
= (Rank
)term
;
260 if (rank
.isCultivar() == code
.equals(NomenclaturalCode
.ICNCP
)){
267 private void createBotanicalNameParts(ICdmFormElement formElement
, INonViralName nonViralName
, int style
){
271 private void createFungusNameParts(ICdmFormElement formElement
, INonViralName nonViralName
, int style
){
272 IFungusName fungusName
= (IFungusName
)nonViralName
;
273 checkbox_anamorphic
= formFactory
.createCheckbox(formElement
, "Anamorphic", fungusName
.isAnamorphic(), style
);
276 private void createZoologicalNameParts(ICdmFormElement formElement
, INonViralName nonViralName
, int style
){
277 IZoologicalName zoologicalName
= (IZoologicalName
) nonViralName
;
280 text_breed
= formFactory
.createTextWithLabelElement(formElement
, "Breed", zoologicalName
.getBreed(), style
);
284 private void createGenusOrUninomialControls(ICdmFormElement element
, INonViralName nonViralName
, int style
){
285 String title
= "Genus";
286 if(nonViralName
.isSupraGeneric()){
289 text_uninomial
= formFactory
.createTextWithLabelElement(element
, title
, nonViralName
.getGenusOrUninomial(), style
);
292 private void createInfragenerericEpithetControls(ICdmFormElement element
, INonViralName nonViralName
, int style
){
293 boolean isIcznOrNull
= nonViralName
.getNameType() == null || nonViralName
.getNameType().equals(NomenclaturalCode
.ICZN
);
294 if (nonViralName
.isInfraGeneric() || nonViralName
.isSpecies()
295 || isIcznOrNull
&& (nonViralName
.isInfraSpecific() || nonViralName
.isSpeciesAggregate()) //for iczn all subgeneric names may have infrageneric part, but unclear if speciesAggregates exist at all
297 text_infragenericEpithet
= formFactory
.createTextWithLabelElement(element
, "Infrageneric Epithet", nonViralName
.getInfraGenericEpithet(), style
);
301 private void createSpecificEpithetControls(ICdmFormElement element
, INonViralName nonViralName
, int style
){
302 if(nonViralName
.isSpecies() || nonViralName
.isInfraSpecific() || nonViralName
.isSpeciesAggregate()){
303 text_specificEpithet
= formFactory
.createTextWithLabelElement(element
, "Specific Epithet", nonViralName
.getSpecificEpithet(), SWT
.NULL
);
307 private void createInfraSpecificEpithetControls(ICdmFormElement element
, INonViralName nonViralName
, int style
){
308 if(nonViralName
.isInfraSpecific()){
309 text_infraspecificEpithet
= formFactory
.createTextWithLabelElement(element
, "Infraspecific Epithet", nonViralName
.getInfraSpecificEpithet(), SWT
.NULL
);
314 public void handleEvent(Object eventSource
){
315 if (eventSource
== toggleable_cache
) {
316 getEntity().setNameCache(toggleable_cache
.getText(),
317 toggleable_cache
.getState());
318 updateCacheRelevance();
320 else if(eventSource
== combo_rank
){
321 getEntity().setRank(combo_rank
.getSelection());
322 clearCheckRankWarnings();
325 // ((NameDetailSection)this.getParentElement()).layout(true, true);
326 //this is needed for the "new" wizards in order to redraw correctly
329 else if(eventSource
== text_appendedPhrase
){
330 getEntity().setAppendedPhrase(text_appendedPhrase
.getText());
332 else if(eventSource
== text_infragenericEpithet
){
333 getEntity().setInfraGenericEpithet(text_infragenericEpithet
.getText());
335 else if(eventSource
== text_infraspecificEpithet
){
336 getEntity().setInfraSpecificEpithet(text_infraspecificEpithet
.getText());
338 else if(eventSource
== text_specificEpithet
){
339 getEntity().setSpecificEpithet(text_specificEpithet
.getText());
341 else if(eventSource
== text_uninomial
){
342 getEntity().setGenusOrUninomial(text_uninomial
.getText());
344 else if(eventSource
== checkbox_anamorphic
){
345 ((IFungusName
)getEntity()).setAnamorphic(checkbox_anamorphic
.getSelection());
346 }else if(eventSource
== text_breed
){
347 ((IZoologicalName
)getEntity()).setBreed(text_breed
.getText());
348 }else if(eventSource
== this.text_cultivarName
){
349 ((ICultivarPlantName
)getEntity()).setCultivarEpithet(text_cultivarName
.getText());
350 }else if(eventSource
== this.text_cultivarGroup
){
351 ((ICultivarPlantName
)getEntity()).setCultivarGroupEpithet(text_cultivarGroup
.getText());
352 }else if(eventSource
== this.text_subGenusAuthorship
){
353 ((IBacterialName
)getEntity()).setSubGenusAuthorship(text_subGenusAuthorship
.getText());
358 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
361 public SelectionArbitrator
getSelectionArbitrator() {
362 return selectionArbitrator
;
366 public void updateToggleableCacheField() {
367 if (toggleable_cache
!= null){
368 if(! getEntity().isProtectedNameCache()){
369 toggleable_cache
.setText(getEntity().getNameCache());
375 protected void handleToggleableCacheField() {
376 getEntity().setTitleCache(toggleable_cache
.getText(), toggleable_cache
.getState());
377 updateCacheRelevance();
378 updateToggleableCacheField();
382 public void fillFields() {
384 if (combo_rank
!= null && (combo_rank
.getSelection() == null || !combo_rank
.getSelection().equals(getEntity().getRank())) ) {
385 combo_rank
.setSelection(getEntity().getRank());
387 if(text_appendedPhrase
!= null && (text_appendedPhrase
.getText() == null || !text_appendedPhrase
.getText().equals(getEntity().getAppendedPhrase()))) {
388 text_appendedPhrase
.setText(getEntity().getAppendedPhrase());
390 if (text_uninomial
!= null && (text_uninomial
.getText() == null || !text_uninomial
.getText().equals(getEntity().getGenusOrUninomial()))) {
391 text_uninomial
.setText(getEntity().getGenusOrUninomial());
393 if (text_infragenericEpithet
!= null && (text_infragenericEpithet
.getText() == null || !text_infragenericEpithet
.getText().equals(getEntity().getInfraGenericEpithet()))) {
394 text_infragenericEpithet
.setText(getEntity().getInfraGenericEpithet());
396 if (text_specificEpithet
!= null && (text_specificEpithet
.getText() == null || !text_specificEpithet
.getText().equals(getEntity().getSpecificEpithet()))) {
397 text_specificEpithet
.setText(getEntity().getSpecificEpithet());
399 if (text_infraspecificEpithet
!= null && getEntity().getInfraSpecificEpithet() != null && !text_infraspecificEpithet
.getText().equals(getEntity().getInfraSpecificEpithet())) {
400 text_infraspecificEpithet
.setText(getEntity().getInfraSpecificEpithet());
402 if (checkbox_anamorphic
!= null && (checkbox_anamorphic
.getSelection() != ((IFungusName
)getEntity()).isAnamorphic())) {
403 checkbox_anamorphic
.setSelection(((IFungusName
)getEntity()).isAnamorphic());
405 if (text_cultivarName
!= null && ((ICultivarPlantName
)getEntity()).getCultivarEpithet() != null && !text_cultivarName
.getText().equals(((ICultivarPlantName
)getEntity()).getCultivarEpithet())) {
406 text_cultivarName
.setText(((ICultivarPlantName
)getEntity()).getCultivarEpithet());
408 if (text_cultivarGroup
!= null && ((ICultivarPlantName
)getEntity()).getCultivarGroupEpithet() != null && !text_cultivarGroup
.getText().equals(((ICultivarPlantName
)getEntity()).getCultivarGroupEpithet())) {
409 text_cultivarGroup
.setText(((ICultivarPlantName
)getEntity()).getCultivarGroupEpithet());
411 if (text_subGenusAuthorship
!= null && ((IBacterialName
)getEntity()).getSubGenusAuthorship() != null && !text_subGenusAuthorship
.getText().equals(((IBacterialName
)getEntity()).getSubGenusAuthorship())) {
412 text_subGenusAuthorship
.setText(((IBacterialName
)getEntity()).getSubGenusAuthorship());
414 if (text_breed
!= null && ((IZoologicalName
)getEntity()).getBreed() != null && !text_breed
.getText().equals(((IZoologicalName
)getEntity()).getBreed())) {
415 text_breed
.setText(((IZoologicalName
)getEntity()).getBreed());
417 ((NameDetailSection
)this.getParentElement()).setSectionTitle();