Merge branch 'release/5.32.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / AbstractTypeDesignationElement.java
index 2a397b9b15290ed5285c35aa14f8ddfb58d2023a..1598219640bdde67ba855b8c3ae0a9e7037eaa65 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.Iterator;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.eclipse.swt.events.SelectionListener;
 
+import eu.etaxonomy.cdm.model.common.IdentifiableSource;
 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
-import eu.etaxonomy.taxeditor.ui.section.supplemental.AbstractReferencedEntityElement;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
+import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
+import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
+import eu.etaxonomy.taxeditor.ui.section.reference.OriginalSourceElement;
+import eu.etaxonomy.taxeditor.ui.section.supplemental.AbstractSourcedEntityBaseElement;
+import eu.etaxonomy.taxeditor.ui.section.supplemental.SourceSection;
 
 /**
- * <p>Abstract AbstractTypeDesignationElement class.</p>
- *
  * @author n.hoffmann
  * @created May 17, 2010
- * @version 1.0
  */
-public abstract class AbstractTypeDesignationElement<T extends TypeDesignationBase> extends AbstractReferencedEntityElement<T> {
+public abstract class AbstractTypeDesignationElement<T extends TypeDesignationBase>
+        extends AbstractSourcedEntityBaseElement<T> {
+
+    @SuppressWarnings("unused")
+    private static final Logger logger = LogManager.getLogger(AbstractTypeDesignationElement.class);
+
+    protected CheckboxElement checkbox_notDesignated;
+//     protected EntitySelectionElement<Reference> selection_reference;
+//    protected TextWithLabelElement text_referenceDetail;
+       protected OriginalSourceElement singleSourceElement;
+    protected SourceSection sourceSection;
+    protected ICdmFormElement formElement;
+    protected int style;
 
-       protected CheckboxElement checkbox_notDesignated;
-       
 
-       /**
-        * <p>Constructor for AbstractTypeDesignationElement.</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 T object.
-        * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
-        * @param style a int.
-        * @param <T> a T object.
-        */
        public AbstractTypeDesignationElement(CdmFormFactory formFactory,
                        AbstractFormSection section, T entity,
                        SelectionListener removeListener, int style) {
                super(formFactory, section, entity, removeListener, style);
        }
-       
-       /** {@inheritDoc} */
+
        @Override
        public void createControls(ICdmFormElement formElement, int style) {
-               checkbox_notDesignated = formFactory.createCheckbox(formElement, "Not Designated", false, style);
-               super.createControls(formElement, style);
+           checkbox_notDesignated = formFactory.createCheckbox(formElement, "Not Designated", false, style);
+
+        sourceSection = formFactory.createSourceSection(getConversationHolder(), formElement, StoreUtil.getSectionStyle(SourceSection.class, IdentifiableSource.class.getCanonicalName()));
+        sourceSection.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+               this.formElement = formElement;
+               this.style = style;
+               if (this.entity != null){
+                       this.setEntity(entity);
+               }
        }
-       
+
+       @Override
+       public void setEntity(T entity){
+           super.setEntity(entity);
+           if (this.getElements() == null || this.getElements().isEmpty()){
+               return;
+           }
+           removeSourcesControls();
+
+           if (this.entity != null && this.entity.hasDesignationSource()){
+               if (singleSourceElement == null){
+                   removeNotDesignatedControls();
+                   singleSourceElement = formFactory.createOriginalSourceElement(formElement, entity, "Status Reference");
+//             selection_reference = formFactory
+//                        .createSelectionElement(Reference.class, formElement, "Status Reference",
+//                                entity.getCitation(), EntitySelectionElement.ALL, style);
+//
+//                text_referenceDetail = formFactory.createTextWithLabelElement(
+//                    formElement, "Reference Detail", entity.getCitationMicroReference(), style);
+                checkbox_notDesignated = formFactory.createCheckbox(formElement, "Not Designated", entity.isNotDesignated(), style);
+               }
+        }else{
+            if (singleSourceElement != null){
+                removeReferenceControls();
+                //check if there is still a reference
+                if (this.entity.getCitation() != null){
+                    this.entity.setCitation(null);
+                    this.entity.setCitationMicroReference(null);
+                }
+            }
+        }
+           if (entity.hasDesignationSource()){
+            singleSourceElement.setEntity(entity.getDesignationSource(true));
+        }
+
+           sourceSection = formFactory.createSourceSection(getConversationHolder(), formElement, StoreUtil.getSectionStyle(SourceSection.class, IdentifiableSource.class.getCanonicalName()));
+        sourceSection.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+        sourceSection.setEntity(entity);
+        StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
+       }
+
+       /**
+     * Updates all widgets to display the latest data
+     */
+    protected void updateContent() {
+      // if (this.getParentElement() instanceof NameDetailSection
+    }
+
+    public void removeReferenceControls(){
+        for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
+            ICdmFormElement childElement = iterator.next();
+            // recursion
+//           if (childElement instanceof EntitySelectionElement && ((EntitySelectionElement)childElement).equals(selection_reference)) {
+//                childElement.removeElements();
+//
+//                // unregister selection arbitrator
+//                if(childElement instanceof ISelectableElement){
+//                    SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
+//                    if(selectionArbitrator != null){
+//                        formFactory.destroySelectionArbitrator(selectionArbitrator);
+//                    }
+//                }
+//
+//                // unregister from property changes
+//                formFactory.removePropertyChangeListener(childElement);
+//                singleSourceElement = null;
+//           }
+            if (childElement instanceof OriginalSourceElement && ((OriginalSourceElement)childElement).equals(singleSourceElement)) {
+
+                childElement.removeElements();
+
+                // unregister selection arbitrator
+                if(childElement instanceof ISelectableElement){
+                    SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
+                    if(selectionArbitrator != null){
+                        formFactory.destroySelectionArbitrator(selectionArbitrator);
+                    }
+                }
+
+                // unregister from property changes
+                formFactory.removePropertyChangeListener(childElement);
+                singleSourceElement = null;
+            }
+        }
+    }
+
+    public void removeNotDesignatedControls(){
+        for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
+            ICdmFormElement childElement = iterator.next();
+            // recursion
+           if (childElement instanceof CheckboxElement && ((CheckboxElement)childElement).equals(this.checkbox_notDesignated)) {
+                childElement.removeElements();
+
+                // unregister selection arbitrator
+                if(childElement instanceof ISelectableElement){
+                    SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
+                    if(selectionArbitrator != null){
+                        formFactory.destroySelectionArbitrator(selectionArbitrator);
+                    }
+                }
+
+                // unregister from property changes
+                formFactory.removePropertyChangeListener(childElement);
+                checkbox_notDesignated = null;
+                break;
+           }
+        }
+    }
+
+    public void removeSourcesControls(){
+        for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
+            ICdmFormElement childElement = iterator.next();
+            // recursion
+           if (childElement instanceof SourceSection) {
+                childElement.removeElements();
+
+                getElements().remove(childElement);
+
+                // unregister selection arbitrator
+                if(childElement instanceof ISelectableElement){
+                    SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
+                    if(selectionArbitrator != null){
+                        formFactory.destroySelectionArbitrator(selectionArbitrator);
+                    }
+                }
+
+                // unregister from property changes
+                formFactory.removePropertyChangeListener(childElement);
+                ((SourceSection)childElement).dispose();
+                sourceSection = null;
+                break;
+           }
+        }
+    }
+
+    @Override
+    public void handleEvent(Object eventSource) {
+//        if (eventSource == selection_reference) {
+//            getEntity().setCitation(selection_reference.getEntity());
+//            setEntity(entity);
+//        }
+//        if (eventSource == text_referenceDetail) {
+//            getEntity().setCitationMicroReference(text_referenceDetail.getText());
+//            setEntity(entity);
+//        }
+        if (eventSource == checkbox_notDesignated) {
+            getEntity().setNotDesignated(checkbox_notDesignated.getSelection());
+        }
+    }
+
 }