ref #10222 adapt taxeditor to changes (DerivedUnitFacade and CondensedDistributionRec...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / detail / CdmSectionPart.java
index 596d7f9da133377ba3564067b73159094f1c3605..0def76556d9eabe8306d7a6249969de4cfada5a4 100644 (file)
@@ -6,10 +6,8 @@
  * 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.view.detail;
 
-import java.awt.Event;
 import java.util.EventObject;
 
 import org.eclipse.jface.util.IPropertyChangeListener;
@@ -17,32 +15,35 @@ 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;
-import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
+import eu.etaxonomy.cdm.facade.DerivedUnitFacade;
+import eu.etaxonomy.cdm.facade.DerivedUnitFacadeNotSupportedException;
 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 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.PolytomousKeyDetailSection;
 import eu.etaxonomy.taxeditor.ui.section.key.ScopeRestrictionSection;
 import eu.etaxonomy.taxeditor.ui.section.key.TaxonomicScopeSection;
-import eu.etaxonomy.taxeditor.ui.section.name.NameDetailSection;
 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.DeterminationDetailSection;
 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.TaxonRelationshipDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.occurrence.dna.SampleDesignationDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalSourceDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.supplemental.RightsSection;
+import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailSection;
 
 /**
  * <p>
@@ -51,32 +52,32 @@ import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonRelationshipDetailSection;
  *
  * @author n.hoffmann
  * @created Feb 8, 2010
- * @version 1.0
  */
-public class CdmSectionPart<T> extends SectionPart implements
-               IPropertyChangeListener {
+public class CdmSectionPart<T> extends SectionPart
+        implements IPropertyChangeListener {
+
+    public static String EXPANDED = "expanded";
+    public static String COLLAPSED = "collapsed";
 
        private final AbstractFormSection<T> formSection;
 
        /**
-        * <p>
-        * Constructor for CdmSectionPart.
-        * </p>
-        *
-        * @param section
-        *            a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
-        *            object.
-        * @param <T>
-        *            a T object.
+        * Initial input
         */
+       private Object rootInput;
+
        public CdmSectionPart(AbstractFormSection<T> section) {
                super(section);
                formSection = section;
        }
 
-       /** {@inheritDoc} */
        @Override
        public boolean setFormInput(Object input) {
+        if (formSection.isDisposed()){
+            return false;
+        }
+
+           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
@@ -85,10 +86,11 @@ public class CdmSectionPart<T> extends SectionPart implements
                if (input instanceof IElementHasDetails) {
                        input = ((IElementHasDetails) input).getData();
                }
+
                if ((input instanceof TaxonBase)
                                && (formSection instanceof ITaxonBaseDetailSection)) {
                        ((ITaxonBaseDetailSection) formSection)
-                                       .setTaxonBase((TaxonBase) input);
+                                       .setTaxonBase((TaxonBase<?>) input);
                        return true;
                }
                else if (input.getClass().equals(DerivedUnit.class)
@@ -103,6 +105,13 @@ public class CdmSectionPart<T> extends SectionPart implements
                }
                else if (input instanceof PolytomousKeyRelationship) {
                        input = ((PolytomousKeyRelationship) input).getDestination();
+                       if ((input instanceof PolytomousKeyNode) &&
+                       (formSection instanceof PolytomousKeyDetailSection ||
+                               formSection instanceof GeoScopeDetailSection ||
+                               formSection instanceof ScopeRestrictionSection ||
+                               formSection instanceof TaxonomicScopeSection)) {
+                   input = ((PolytomousKeyNode)input).getKey();
+                       }
                }
 
                else if ((input instanceof PolytomousKeyNode) &&
@@ -112,13 +121,18 @@ public class CdmSectionPart<T> extends SectionPart implements
                            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)
+                               && (formSection instanceof NomenclaturalSourceDetailSection)) {
+                       ((NomenclaturalSourceDetailSection) formSection)
                                        .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
                        return true;
                }else if ((input instanceof TaxonRelationship)
@@ -126,37 +140,75 @@ public class CdmSectionPart<T> extends SectionPart implements
                        ((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());
+//                     .setEntity( ((TaxonRelationship)input).getFromTaxon().getName());
                        return true;
                }else if ((input instanceof TaxonRelationship)
                                && (formSection instanceof NameRelationshipDetailSection)) {
                        ((NameRelationshipDetailSection) formSection)
                                        .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
                        return true;
+               }else if ((input instanceof DerivedUnitFacade)
+                       && (formSection instanceof RightsSection)){
+                   ((RightsSection)formSection).setEntity(((DerivedUnitFacade)input).innerDerivedUnit());
+                   return true;
+               }else if ((input instanceof DerivedUnitFacade)
+                && (formSection instanceof DeterminationDetailSection)){
+                   DerivedUnitFacade facade = ((DerivedUnitFacade)input);
+                   SpecimenOrObservationBase<?> sob = facade.innerDerivedUnit() != null? facade.innerDerivedUnit(): facade.innerFieldUnit();
+            ((DeterminationDetailSection)formSection).setEntity(sob);
+            return true;
+               }else if ((input instanceof DerivedUnitFacade)
+                && (formSection instanceof SampleDesignationDetailSection)){
+            ((SampleDesignationDetailSection)formSection).setEntity(((DerivedUnitFacade)input).innerDerivedUnit());
+            return true;
                }
+               //check whether this is needed
+//     }else if ((input instanceof TaxonBase)
+//            && (formSection instanceof TypeDesignationSection)){
+//        ((TypeDesignationSection)formSection).setEntity(((TaxonBase)input).getName());
+//        return true;
+//    }
 
-               
                formSection.setEntity((T) input);
 
+
                return true;
        }
 
-       /*
-        * (non-Javadoc)
-        *
-        * @see
-        * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
-        * .jface.util.PropertyChangeEvent)
-        */
-       /** {@inheritDoc} */
+       public boolean setFormInputWithoutUpdate(Object input) {
+        if (formSection.isDisposed()){
+            return false;
+        }
+
+           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
+           // the detail view. In the case of multiple inputs the only workaround
+           // is checking the type and extracting relevant objects.
+               if (input instanceof IElementHasDetails) {
+                       input = ((IElementHasDetails) input).getData();
+               }
+
+               if ((input instanceof TaxonBase)
+                               && (formSection instanceof ITaxonBaseDetailSection )) {
+
+                       //if (!((TaxonBase<?>) input).getTitleCache().equals(((ITaxonBaseDetailSection) formSection).getTaxonBase().getTitleCache())) {
+                               ((ITaxonBaseDetailSection) formSection)
+                                       .setTaxonBaseWithoutUpdate((TaxonBase<?>) input);
+
+                       //}
+
+                       return true;
+               }
+               //actually only for Taxon Details...
+               return this.setFormInput( input);
+       }
+
        @Override
        public void propertyChange(PropertyChangeEvent event) {
                if (event != null) {
@@ -186,4 +238,9 @@ public class CdmSectionPart<T> extends SectionPart implements
                }
        }
 
-}
+       @Override
+       protected void expansionStateChanged(boolean expanded) {
+           super.expansionStateChanged(expanded);
+           PreferencesUtil.setStringValue(StoreUtil.getPrefKey(formSection.getClass(), rootInput.getClass().getCanonicalName()), expanded?EXPANDED:COLLAPSED);
+       }
+}
\ No newline at end of file