Merge branch 'release/5.8.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / detail / CdmSectionPart.java
index 207e1e5edb7636da4a668fd9d9e6f4dcf049f5ab..950d2d94b8106bd2b4d204698c021f1e7d85dd4a 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2007 EDIT
  * European Distributed Institute of Taxonomy
@@ -10,8 +9,11 @@
 
 package eu.etaxonomy.taxeditor.view.detail;
 
+import java.util.EventObject;
+
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.PropertyChangeEvent;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.ui.forms.SectionPart;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
@@ -19,18 +21,27 @@ import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
 import eu.etaxonomy.taxeditor.model.IElementHasDetails;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.model.PolytomousKeyRelationship;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
-import eu.etaxonomy.taxeditor.ui.section.key.GeographicalScopeDetailSection;
 import eu.etaxonomy.taxeditor.ui.section.key.PolytomousKeyDetailSection;
 import eu.etaxonomy.taxeditor.ui.section.key.ScopeRestrictionSection;
 import eu.etaxonomy.taxeditor.ui.section.key.TaxonomicScopeSection;
+import eu.etaxonomy.taxeditor.ui.section.name.NameRelationshipDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.name.NomenclaturalStatusSection;
+import eu.etaxonomy.taxeditor.ui.section.name.NonViralNameDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.name.ProtologueSection;
+import eu.etaxonomy.taxeditor.ui.section.name.TypeDesignationSection;
+import eu.etaxonomy.taxeditor.ui.section.occurrence.GeoScopeDetailSection;
 import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalReferenceDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailSection;
 
 /**
  * <p>
@@ -44,8 +55,16 @@ import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSect
 public class CdmSectionPart<T> extends SectionPart implements
                IPropertyChangeListener {
 
+    public static String EXPANDED = "expanded";
+    public static String COLLAPSED = "collapsed";
+
        private final AbstractFormSection<T> formSection;
 
+       /**
+        * Initial input
+        */
+       private Object rootInput;
+
        /**
         * <p>
         * Constructor for CdmSectionPart.
@@ -65,6 +84,7 @@ public class CdmSectionPart<T> extends SectionPart implements
        /** {@inheritDoc} */
        @Override
        public boolean setFormInput(Object input) {
+           this.rootInput = input;
            //FIXME (CM): Need to fix this part of the design.
            //The design seems to be locked to the idea that only one
            // entity (either from the navigator or the editor) drives
@@ -95,11 +115,46 @@ public class CdmSectionPart<T> extends SectionPart implements
 
                else if ((input instanceof PolytomousKeyNode) &&
                    (formSection instanceof PolytomousKeyDetailSection ||
-                           formSection instanceof GeographicalScopeDetailSection ||
+                           formSection instanceof GeoScopeDetailSection ||
                            formSection instanceof ScopeRestrictionSection ||
                            formSection instanceof TaxonomicScopeSection)) {
                input = ((PolytomousKeyNode)input).getKey();
-           }
+           } else if ((input instanceof TaxonRelationship)
+                               && (formSection instanceof TaxonDetailSection)) {
+                       ((TaxonDetailSection) formSection)
+                                       .setTaxon( ((TaxonRelationship)input).getFromTaxon());
+                       return true;
+               }else if ((input instanceof TaxonRelationship)
+                               && (formSection instanceof NonViralNameDetailSection)) {
+                       ((NonViralNameDetailSection) formSection)
+                                       .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
+                       return true;
+               }else if ((input instanceof TaxonRelationship)
+                               && (formSection instanceof NomenclaturalReferenceDetailSection)) {
+                       ((NomenclaturalReferenceDetailSection) formSection)
+                                       .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
+                       return true;
+               }else if ((input instanceof TaxonRelationship)
+                               && (formSection instanceof NomenclaturalStatusSection)) {
+                       ((NomenclaturalStatusSection) formSection)
+                                       .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
+                       return true;
+               }else if ((input instanceof TaxonRelationship)
+                               && (formSection instanceof ProtologueSection)) {
+                       ((ProtologueSection) formSection)
+                                       .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
+                       return true;
+               }else if ((input instanceof TaxonRelationship)
+                               && (formSection instanceof TypeDesignationSection)) {
+                       ((TypeDesignationSection) formSection)
+                                       .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
+                       return true;
+               }else if ((input instanceof TaxonRelationship)
+                               && (formSection instanceof NameRelationshipDetailSection)) {
+                       ((NameRelationshipDetailSection) formSection)
+                                       .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
+                       return true;
+               }
 
 
                formSection.setEntity((T) input);
@@ -120,17 +175,34 @@ public class CdmSectionPart<T> extends SectionPart implements
                if (event != null) {
 
                        Object eventSource = event.getSource();
+                       if (eventSource instanceof EventObject){
+                               eventSource = ((EventObject)eventSource).getSource();
+                       }
+                       Control[] children = formSection.getLayoutComposite().getChildren();
+                       boolean containsElement = false;
+                       for (Control control : children) {
+                           if(eventSource.equals(control)){
+                               containsElement = true;
+                               break;
+                           }
+            }
+                       if(containsElement){
+                           markDirty();
+                       }
 
-                       if (formSection.equals(eventSource)){
+                       else if (formSection.equals(eventSource)){
                                markDirty();
                        }
-                       if((
-                                               (eventSource instanceof ICdmFormElement)
-                                               && formSection.containsFormElement((ICdmFormElement) eventSource))
-                                               ) {
-                               markDirty();
+                       else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
+                           markDirty();
                        }
                }
        }
 
+       @Override
+       protected void expansionStateChanged(boolean expanded) {
+           super.expansionStateChanged(expanded);
+           PreferencesUtil.setStringValue(StoreUtil.getPrefKey(formSection.getClass(), rootInput.getClass().getCanonicalName()), expanded?EXPANDED:COLLAPSED);
+       }
+
 }