cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / SpecimenTypeDesignationElement.java
index 1fd7f9a170234d6c4bf5dcf0ed96fa3a23281ecc..2cc7d454e9149c327d3b062644b26e88556b2864 100644 (file)
@@ -1,23 +1,31 @@
-// $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.List;
+
 import org.eclipse.swt.events.SelectionListener;
 
+import eu.etaxonomy.cdm.compare.name.TypeDesignationStatusComparator;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
-import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
-import eu.etaxonomy.cdm.model.occurrence.Specimen;
-import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
+import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
+import eu.etaxonomy.cdm.model.reference.NamedSource;
+import eu.etaxonomy.cdm.model.term.TermType;
+import eu.etaxonomy.taxeditor.l10n.Messages;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+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;
@@ -25,84 +33,82 @@ import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 
 /**
- * <p>
- * SpecimenTypeDesignationElement class.
- * </p>
- * 
  * @author n.hoffmann
  * @created May 17, 2010
- * @version 1.0
  */
 public class SpecimenTypeDesignationElement extends
                AbstractTypeDesignationElement<SpecimenTypeDesignation> {
 
        private TermComboElement<SpecimenTypeDesignationStatus> combo_typeStatus;
-       private EntitySelectionElement<DerivedUnitBase> selection_typeSpecimen;
+       private EntitySelectionElement<DerivedUnit> selection_typeSpecimen;
        private TextWithLabelElement text_specimenTypeText;
 
-       /**
-        * <p>
-        * Constructor for SpecimenTypeDesignationElement.
-        * </p>
-        * 
-        * @param formFactory
-        *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
-        *            object.
-        * @param section
-        *            a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
-        *            object.
-        * @param entity
-        *            a {@link eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation}
-        *            object.
-        * @param removeListener
-        *            a {@link org.eclipse.swt.events.SelectionListener} object.
-        * @param style
-        *            a int.
-        */
        public SpecimenTypeDesignationElement(CdmFormFactory formFactory,
                        AbstractFormSection section, SpecimenTypeDesignation entity,
                        SelectionListener removeListener, int style) {
                super(formFactory, section, entity, removeListener, style);
        }
 
-       /** {@inheritDoc} */
        @Override
        public void createControls(ICdmFormElement formElement, int style) {
                selection_typeSpecimen = formFactory
-                               .createSelectionElement(DerivedUnitBase.class,
-                                               getConversationHolder(), formElement, "Specimen", null,
+                               .createSelectionElement(DerivedUnit.class,
+                                       formElement, "Specimen", null,
                                                EntitySelectionElement.ALL, style);
-               combo_typeStatus = formFactory
-                               .createTermComboElement(SpecimenTypeDesignationStatus.class,
-                                               formElement, "Designation Status", null, style);
+               String availableTypes = PreferencesUtil.getStringValue(PreferencePredicate.SpecimenTypeDesignationStatus.getKey());
+           List<SpecimenTypeDesignationStatus> relTypes = CdmStore.getTermManager().createTermListFromString(availableTypes, TermType.SpecimenTypeDesignationStatus);
+//             combo_typeStatus = formFactory.createDefinedTermComboElement(TermType.SpecimenTypeDesignationStatus,
+//                                             formElement, "Designation Status", null, style, new TypeDesignationStatusComparator<SpecimenTypeDesignationStatus>());
+           combo_typeStatus = formFactory.createDefinedTermComboElement(relTypes, formElement, "Designation Status", null,style, new TypeDesignationStatusComparator<SpecimenTypeDesignationStatus>());
 
                super.createControls(formElement, style);
+               setEntity(entity);
        }
 
-       /** {@inheritDoc} */
        @Override
        public void setEntity(SpecimenTypeDesignation entity) {
-               super.setEntity(entity);
-               selection_typeSpecimen.setEntity(entity.getTypeSpecimen());
+           super.setEntity(entity);
+           if (selection_typeSpecimen != null){
+               if (entity.getTypeSpecimen() != null){
+                   selection_typeSpecimen.setEntity(entity.getTypeSpecimen());
+               }else{
+                   selection_typeSpecimen.setEntity(entity.getTypeSpecimen());
+               }
+
+                       SpecimenTypeDesignationStatus typeStatus = HibernateProxyHelper
+                                       .deproxy(entity.getTypeStatus(), SpecimenTypeDesignationStatus.class);
+                   combo_typeStatus.setSelection(typeStatus);
 
-               SpecimenTypeDesignationStatus typeStatus = (SpecimenTypeDesignationStatus) HibernateProxyHelper
-                               .deproxy(entity.getTypeStatus());
-               combo_typeStatus.setSelection(typeStatus);
-               checkbox_notDesignated.setSelection(entity.isNotDesignated());
+                       checkbox_notDesignated.setSelection(entity.isNotDesignated());
+           }
        }
 
-       /** {@inheritDoc} */
        @Override
        public void handleEvent(Object eventSource) {
+           super.handleEvent(eventSource);
                if (eventSource == combo_typeStatus) {
+
+                   if ((combo_typeStatus.getSelection() == null && entity.getCitation() != null) || ( entity.getTypeStatus() != null && getEntity().getTypeStatus().hasDesignationSource() && !combo_typeStatus.getSelection().hasDesignationSource() && entity.getCitation() != null)){
+                       boolean ok = MessagingUtils.confirmDialog(Messages.NameTypeDesignationElement_4, Messages.NameTypeDesignationElement_5);
+                       if (!ok){
+                           combo_typeStatus.setSelection(entity.getTypeStatus());
+                           return;
+                       }
+                   }
                        getEntity().setTypeStatus(combo_typeStatus.getSelection());
+                       if (entity.hasDesignationSource()){
+                           NamedSource source = NamedSource.NewPrimarySourceInstance(null, null);
+                           entity.setDesignationSource(source);
+                       }
+                       setEntity(entity);
+
                }
                // TODO remove this once specimens are correctly implemented
                else if (eventSource == text_specimenTypeText) {
                        // TODO this is a simple workaround to enter type specimen as text
                        // strings
                        if (getEntity().getTypeSpecimen() == null) {
-                               Specimen typeSpecimen = Specimen.NewInstance();
+                               DerivedUnit typeSpecimen = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
                                getEntity().setTypeSpecimen(typeSpecimen);
                        }
                        getEntity().getTypeSpecimen().setTitleCache(
@@ -111,14 +117,12 @@ public class SpecimenTypeDesignationElement extends
                        getEntity().setTypeSpecimen(selection_typeSpecimen.getSelection());
                } else if (eventSource == checkbox_notDesignated) {
                        getEntity().setNotDesignated(checkbox_notDesignated.getSelection());
-               } 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());
+//             }
        }
-}
+}
\ No newline at end of file