Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / NameDetailElement.java
index d68e8fcaec14c75bc3c020f34b8a853563b01581..4e1cf03a463377ee78873a494b288772cfe1577f 100644 (file)
@@ -1,9 +1,8 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
 package eu.etaxonomy.taxeditor.ui.section.name;
 
 import java.util.Arrays;
+import java.util.Collection;
 
+import org.apache.log4j.Logger;
 import org.eclipse.swt.SWT;
 
+import eu.etaxonomy.cdm.model.common.TermType;
 import eu.etaxonomy.cdm.model.name.BotanicalName;
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
 import eu.etaxonomy.cdm.model.name.NonViralName;
 import eu.etaxonomy.cdm.model.name.Rank;
+import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
 import eu.etaxonomy.cdm.model.name.ZoologicalName;
 import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
+import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
-import eu.etaxonomy.taxeditor.ui.element.NumberWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
@@ -39,6 +44,8 @@ import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement
  */
 public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<NonViralName> implements ISelectableElement, IEnableableFormElement{
 
+    private final Logger logger = Logger.getLogger(NameDetailElement.class);
+
        private TermComboElement<Rank> combo_rank;
        private TextWithLabelElement text_appendedPhrase;
        private TextWithLabelElement text_uninomial;
@@ -48,10 +55,10 @@ public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<N
        private SelectionArbitrator selectionArbitrator;
        private CheckboxElement checkbox_anamorphic;
        private TextWithLabelElement text_breed;
-       private NumberWithLabelElement text_publicationYear;
-       private NumberWithLabelElement text_originalPublicationYear;
+       //private NumberWithLabelElement text_publicationYear;
+       //private NumberWithLabelElement text_originalPublicationYear;
        private int cursorPosition;
-       
+
        /**
         * <p>Constructor for NameDetailElement.</p>
         *
@@ -62,7 +69,7 @@ public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<N
        public NameDetailElement(CdmFormFactory cdmFormFactory, ICdmFormElement formElement,
                        int style) {
                super(cdmFormFactory, formElement);
-               // register as selection listener
+               // make this element selectable
                if(cdmFormFactory.getSelectionProvider() != null){
                        selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
                }
@@ -71,20 +78,37 @@ public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<N
        /** {@inheritDoc} */
        @Override
        protected void createControls(ICdmFormElement formElement, NonViralName nonViralName, int style) {
-               
-               toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
-               
-               combo_rank = formFactory.createTermComboElement(Rank.class, this, "Rank", nonViralName.getRank(), style);
-               
-               createGenusOrUninomialControls(this, nonViralName, style);
-               createInfragenerericEpithetControls(this, nonViralName, style);
-               createSpecificEpithetControls(this, nonViralName, style);
-               createInfraSpecificEpithetControls(this, nonViralName, style);
-               
-               createSpecificNameParts(this, nonViralName, style);
-               
-               text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
-               
+
+           if (isAdvancedView){
+               toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
+               combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style);
+               createGenusOrUninomialControls(this, nonViralName, style);
+            createInfragenerericEpithetControls(this, nonViralName, style);
+            createSpecificEpithetControls(this, nonViralName, style);
+            createInfraSpecificEpithetControls(this, nonViralName, style);
+            createSpecificNameParts(this, nonViralName, style);
+            text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
+           }else{
+               if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE)){
+                       toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
+               }
+
+           if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK)){
+               combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style);
+           }
+
+               if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS)){
+                       createGenusOrUninomialControls(this, nonViralName, style);
+                       createInfragenerericEpithetControls(this, nonViralName, style);
+                       createSpecificEpithetControls(this, nonViralName, style);
+                       createInfraSpecificEpithetControls(this, nonViralName, style);
+                       createSpecificNameParts(this, nonViralName, style);
+               }
+               if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE)){
+                               text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
+               }
+           }
+
        }
 
        /**
@@ -93,42 +117,66 @@ public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<N
        protected void clearCheckRankWarnings() {
                if(getEntity().hasProblem(ParserProblem.CheckRank)){
                        getEntity().removeParsingProblem(ParserProblem.CheckRank);
-                       
+
                        // FIXME this has to be reafctored completely. See tickets #1959, #1663, #1957, #1958
 //                     TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
 //                     nameEditor.getSelectedContainer().getNameViewer().clearErrors();
                }
        }
-       
+
        /** {@inheritDoc} */
        @Override
        protected void updateContent() {
                if(getEntity() == null){
-                       setEntity(NonViralName.NewInstance(null));
+                       setEntity(TaxonNameFactory.NewNonViralInstance(null));
                }
-               
+
                super.updateContent();
-               
-               if(isIrrelevant()){
-                       setIrrelevant(isIrrelevant());
-               }else{
-                       setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
+               if (toggleable_cache != null){
+                   toggleable_cache.setEnabled(getEntity().isProtectedNameCache());
+
+               if (isAdvancedView){
+                       if(isIrrelevant()){
+                               setIrrelevant(isIrrelevant());
+                       }else{
+                               setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
+                       }
+               }
                }
        }
 
 
        private void createSpecificNameParts(ICdmFormElement formElement,
                        NonViralName nonViralName, int style) {
-               switch(nonViralName.getNomenclaturalCode()){
-               case ICBN:
-                       createBotanicalNameParts(formElement, nonViralName, style);
-                       break;
-               case ICZN:
-                       createZoologicalNameParts(formElement, nonViralName, style);
-                       break;
+               NomenclaturalCode code = nonViralName.getNomenclaturalCode();
+               if (code != null){
+                       switch(nonViralName.getNomenclaturalCode()){
+                       case ICNAFP :
+                               // TODO RL
+                               if(isAdvancedView)
+                               {
+                                       createBotanicalNameParts(formElement, nonViralName, style);
+                               }
+                               break;
+                       case ICZN:
+                               createZoologicalNameParts(formElement, nonViralName, style);
+                               break;
+            case ICVCN:
+                //TODO implement
+                logger.warn("ICVCN not yet implemented");
+                break;
+            case ICNB:
+                //TODO implement
+                logger.warn("ICNB not yet implemented");
+                break;
+            case ICNCP:
+                //TODO implement
+                logger.warn("ICNCP not yet implemented");
+                break;
+            }
                }
        }
-       
+
        private void createBotanicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
                BotanicalName botanicalName = (BotanicalName) nonViralName;
                checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", botanicalName.isAnamorphic(), style);
@@ -136,53 +184,61 @@ public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<N
 
        private void createZoologicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
                ZoologicalName zoologicalName = (ZoologicalName) nonViralName;
-               text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
-               text_publicationYear = formFactory.createIntegerTextWithLabelElement(formElement, "Publication Year", zoologicalName.getPublicationYear(), style);
-               text_originalPublicationYear = formFactory.createIntegerTextWithLabelElement(formElement, "Orig. Publication Year", zoologicalName.getOriginalPublicationYear(), style);
+
+               if (isAdvancedView){
+                   text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
+               }
        }
-       
-       private void createGenusOrUninomialControls(ICdmFormElement element, NonViralName nonViralName, int style){             
+
+       private void createGenusOrUninomialControls(ICdmFormElement element, NonViralName nonViralName, int style){
                String title = "Genus";
                Rank rank = nonViralName.getRank();
                if(rank != null && rank.isSupraGeneric()){
-                       title = "Uninomial";                    
+                       title = "Uninomial";
                }
                text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
        }
-       
+
        private void createInfragenerericEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
-               if(nonViralName.getRank() != null && nonViralName.getRank().isInfraGeneric() && !nonViralName.getRank().isSpeciesAggregate()){
-                       text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
+               if (nonViralName.getNomenclaturalCode() == null || nonViralName.getNomenclaturalCode().equals(NomenclaturalCode.ICZN)){
+                       if(nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS()) ){
+                               text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
+                       }
+               } else{
+                       if((nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS())) && !nonViralName.getRank().isLower(Rank.SPECIES()) && !nonViralName.getRank().isSpeciesAggregate()){
+                               text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
+                       }
                }
        }
-       
+
        private void createSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
                if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies()  || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
                        text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
                }
        }
-       
+
        private void createInfraSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
                if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
                        text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
                }
        }
-       
+
        /** {@inheritDoc} */
        @Override
        public void handleEvent(Object eventSource){
                if (eventSource == toggleable_cache) {
                        getEntity().setNameCache(toggleable_cache.getText(),
                                        toggleable_cache.getState());
-                       if (!isIrrelevant())
-                               setIrrelevant(toggleable_cache.getState(),
+                       //if (!isIrrelevant()) {
+                setIrrelevant(toggleable_cache.getState(),
                                                Arrays.asList(new Object[] { toggleable_cache }));
+            //}
                }
                else if(eventSource == combo_rank){
                        getEntity().setRank(combo_rank.getSelection());
                        clearCheckRankWarnings();
                        updateContent();
-               }               
+               }
                else if(eventSource == text_appendedPhrase){
                        getEntity().setAppendedPhrase(text_appendedPhrase.getText());
                }
@@ -197,12 +253,15 @@ public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<N
                }
                else if(eventSource == text_uninomial){
                        getEntity().setGenusOrUninomial(text_uninomial.getText());
-               }               
+               }
                else if(eventSource == checkbox_anamorphic){
                        ((BotanicalName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
-               }
+               }else if(eventSource == text_breed){
+            ((ZoologicalName)getEntity()).setBreed(text_breed.getText());
+        }
+
        }
-               
+
        /*
         * (non-Javadoc)
         * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
@@ -212,14 +271,43 @@ public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<N
         *
         * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator} object.
         */
-       public SelectionArbitrator getSelectionArbitrator() {
+       @Override
+    public SelectionArbitrator getSelectionArbitrator() {
                return selectionArbitrator;
        }
-       
+
        @Override
        public void updateToggleableCacheField() {
-               if(! getEntity().isProtectedNameCache()){
-                       toggleable_cache.setText(getEntity().getNameCache());
-               }
+           if (toggleable_cache != null){
+               if(! getEntity().isProtectedNameCache()){
+                       toggleable_cache.setText(getEntity().getNameCache());
+               }
+           }
+       }
+
+       @Override
+       protected void handleToggleableCacheField() {
+        boolean pushedState = toggleable_cache.getState();
+
+        getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
+        setIrrelevant(pushedState, Arrays.asList(new Object[] { toggleable_cache, text_appendedPhrase, checkbox_anamorphic }));
+        updateToggleableCacheField();
+    }
+
+       @Override
+       public void setIrrelevant(boolean irrelevant, Collection<Object> except){
+           if (toggleable_cache != null){
+           if (toggleable_cache.getState() == irrelevant){
+               super.setIrrelevant(irrelevant, except);
+           }
+           if (except == null || except.isEmpty()){
+               toggleable_cache.setIrrelevant(irrelevant);
+           }
+           }
+
        }
+
+
+
+
 }