ref #5821: fix term handling after adapting local preferences
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / NomenclaturalStatusElement.java
index 438f90da4f5c7f7c5c228d53c2ce49e8eac3b5df..fa852eed7b36ae90e94fef94d38f4f437541adc4 100644 (file)
-// $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.Comparator;
+import java.util.List;
+
 import org.eclipse.swt.events.SelectionListener;
 
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
-import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.model.term.TermType;
+import eu.etaxonomy.taxeditor.preference.NameDetailsConfigurator;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.ui.combo.term.TermComboElement;
 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
+import eu.etaxonomy.taxeditor.ui.element.LabelElement;
+import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
-import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
-import eu.etaxonomy.taxeditor.ui.section.supplemental.AbstractReferencedEntityElement;
+import eu.etaxonomy.taxeditor.ui.section.supplemental.AbstractSourcedEntityElement;
 
 /**
- * <p>NomenclaturalStatusElement class.</p>
- *
  * @author n.hoffmann
  * @created Nov 5, 2009
- * @version 1.0
  */
-public class NomenclaturalStatusElement extends AbstractReferencedEntityElement<NomenclaturalStatus> implements IEntityElement<NomenclaturalStatus>, ISelectableElement{
+public class NomenclaturalStatusElement extends AbstractSourcedEntityElement<NomenclaturalStatus> implements IEntityElement<NomenclaturalStatus>, ISelectableElement{
 
        private TermComboElement<NomenclaturalStatusType> nomenclaturalStatusTypeCombo;
-       
-       private TextWithLabelElement ruleConsideredText;
-       
-       private SelectionArbitrator selectionArbitrator;
 
-       /**
-        * <p>Constructor for NomenclaturalStatusElement.</p>
-        *
-        * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection} object.
-        * @param element a {@link eu.etaxonomy.cdm.model.name.NomenclaturalStatus} object.
-        * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
-        * @param style a int.
-        * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
-        */
+       private RuleConsideredElement ruleConsidered;
+
+
+
+       private final SelectionArbitrator selectionArbitrator;
+
        public NomenclaturalStatusElement(CdmFormFactory cdmFormFactory, AbstractFormSection formElement,
                        NomenclaturalStatus element, SelectionListener removeListener, int style) {
-               super(cdmFormFactory, formElement, element, removeListener, style);
+               super(cdmFormFactory, formElement, element, removeListener, "Source", style);
                selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.forms.entitysections.AbstractEntitySetElementComposite#createControls(org.eclipse.swt.widgets.Composite, int)
-        */
        /** {@inheritDoc} */
        @Override
        public void createControls(ICdmFormElement element, int style) {
-               nomenclaturalStatusTypeCombo = formFactory.createTermComboElement(NomenclaturalStatusType.class, this, "Nomenclatural Status Type", null, style);
-               ruleConsideredText = formFactory.createTextWithLabelElement(this, "Rule Considered", "", style);
-               
+           //TermVocabulary vocabulary = CdmStore.getService(IVocabularyService.class).find(VocabularyEnum.NomenclaturalStatusType.getUuid());
+           NameDetailsConfigurator config = PreferencesUtil.getPreferredNameDetailsConfiguration();
+           Comparator<NomenclaturalStatusType> termComparator= new Comparator<NomenclaturalStatusType>() {
+
+            @Override
+            public int compare(NomenclaturalStatusType t1, NomenclaturalStatusType t2) {
+                if (t1 == t2){
+                    return 0;
+                }
+                if (t1 == null){
+                    return -1;
+                }
+                if (t2 == null){
+                    return 1;
+                }
+                if (t1.getIdInVocabulary() == t2.getIdInVocabulary()){
+                    return 0;
+                }
+                if (t1.getIdInVocabulary() == null ){
+                    return t1.getLabel().compareTo(t2.getIdInVocabulary());
+                }
+                if (t2.getIdInVocabulary() == null){
+                    return t1.getIdInVocabulary().compareTo(t2.getLabel());
+                }
+                return t1.getIdInVocabulary().compareTo(t2.getIdInVocabulary());
+
+            }
+        };
+
+        String availableStatus = PreferencesUtil.getStringValue(PreferencePredicate.AvailableNomenclaturalStatus.getKey());
+        List<NomenclaturalStatusType> allStatus = CdmStore.getTermManager().createTermListFromString(availableStatus, TermType.NomenclaturalStatusType);
+
+               nomenclaturalStatusTypeCombo = formFactory.createDefinedTermComboElement(allStatus, this, "Status", null,style, termComparator);
+
+               if (config == null || config.isNomenclaturalStatusRuleConsideredActivated()){
+                   ruleConsidered = formFactory.createRuleConsideredElement(this, "Rule Considered", config == null || config.isNomenclaturalStatusRuleConsideredCodeEditionActivated(), style);
+               }
+               setSourceLabel("Source");
+               LabelElement spacer = formFactory.createLabel(this, null);
+        spacer.getLayoutComposite().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+        addElement(spacer);
                super.createControls(element, style);
+               if (entity != null){
+                       setEntity(entity);
+               }
+
+
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.forms.entitysections.AbstractEntitySetElementComposite#setElement(eu.etaxonomy.cdm.model.common.VersionableEntity)
-        */
        /** {@inheritDoc} */
        @Override
        public void setEntity(NomenclaturalStatus entity) {
                super.setEntity(entity);
-               nomenclaturalStatusTypeCombo.setSelection(entity.getType());
-               ruleConsideredText.setText(entity.getRuleConsidered());
+               if (nomenclaturalStatusTypeCombo != null){
+                       if (entity.getType() != null){
+                           nomenclaturalStatusTypeCombo.setSelection(entity.getType());
+                           nomenclaturalStatusTypeCombo.removeEmptyElement();
+                       }
+                       if (ruleConsidered != null){
+                           ruleConsidered.setElement(entity);
+                       }
+               }
        }
 
        /**
@@ -82,31 +125,23 @@ public class NomenclaturalStatusElement extends AbstractReferencedEntityElement<
         *
         * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator} object.
         */
-       public SelectionArbitrator getSelectionArbitrator() {
+       @Override
+    public SelectionArbitrator getSelectionArbitrator() {
                return selectionArbitrator;
        }
-       
-       /*
-        * (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionElement#handleEvent(java.lang.Object)
-        */
+
        /** {@inheritDoc} */
        @Override
        public void handleEvent(Object eventSource) {
                if(eventSource == nomenclaturalStatusTypeCombo){
                        getEntity().setType(nomenclaturalStatusTypeCombo.getSelection());
                }
-               else if(eventSource == ruleConsideredText){
-                       getEntity().setRuleConsidered(ruleConsideredText.getText());
-               }
-               else if(eventSource == selection_reference){
-                       getEntity().setCitation(selection_reference.getSelection());
-               }
-               else if(eventSource == text_referenceDetail){
-                       getEntity().setCitationMicroReference(text_referenceDetail.getText());
-               }
-               else if(eventSource == text_originaleNameString){
-                       getEntity().setOriginalNameString(text_originaleNameString.getText());
-               }
+
+//             else if(eventSource == selection_reference){
+//                     getEntity().setCitation(selection_reference.getSelection());
+//             }
+//             else if(eventSource == text_referenceDetail){
+//                     getEntity().setCitationMicroReference(text_referenceDetail.getText());
+//             }
        }
 }