Only show "Change Type" when more than one type is available #5565
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / AbstractCdmDetailSection.java
index 859c892c213fb27bc752a51cf7316da57e6882a5..cf2a786c869a2fd781942d8c52231af1900b5fea 100644 (file)
@@ -1,12 +1,12 @@
 // $Id$
 /**
-* Copyright (C) 2007 EDIT
-* 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.
-*/
+ * Copyright (C) 2007 EDIT
+ * 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;
 
@@ -17,164 +17,156 @@ import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.ui.forms.events.ExpansionEvent;
 import org.eclipse.ui.forms.events.IExpansionListener;
-import org.eclipse.ui.forms.widgets.Section;
+import org.eclipse.ui.forms.widgets.ExpandableComposite;
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
-import eu.etaxonomy.taxeditor.ui.forms.AbstractFormSection;
-import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
-import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.DetailType;
-import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
-import eu.etaxonomy.taxeditor.ui.forms.IEnableableFormElement;
-import eu.etaxonomy.taxeditor.ui.forms.ISelectableElement;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+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.IEnableableFormElement;
+import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
 
 /**
- * <p>Abstract AbstractCdmDetailSection class.</p>
+ * This class visualizes an CDM entity of type ENTITY.
+ *
+ * @param <ENTITY> A CDM entity which should be visualized by this section.
  *
  * @author n.hoffmann
  * @created Feb 26, 2010
  * @version 1.0
  */
-public abstract class AbstractCdmDetailSection<ENTITY> extends AbstractFormSection<ENTITY> implements IEnableableFormElement, IExpansionListener{
-       
-       protected AbstractCdmDetailElement<ENTITY> detailElement;
-       
-       /**
-        * <p>Constructor for AbstractCdmDetailSection.</p>
-        *
-        * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
-        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
-        * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
-        * @param style a int.
-        * @param <ENTITY> a ENTITY object.
-        */
+public abstract class AbstractCdmDetailSection<ENTITY> extends AbstractFormSection<ENTITY> implements IEnableableFormElement, IExpansionListener {
+
+       protected ICdmDetailElement<ENTITY> detailElement;
+
        public AbstractCdmDetailSection(CdmFormFactory formFactory,
-                       ConversationHolder conversation, ICdmFormElement parentElement, ISelectionProvider selectionProvider, int style) {
-               super(formFactory, conversation, parentElement, selectionProvider, Section.CLIENT_INDENT | style);
-               
-               setText(getHeading());
-               
-               addExpansionListener(this);
-               
-               createControls(this, SWT.NULL);
+                       ConversationHolder conversation, ICdmFormElement parentElement,
+                       ISelectionProvider selectionProvider, int style) {
+           this(formFactory, null, conversation, parentElement, selectionProvider, style);
+       }
+
+
+       public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
+                       ConversationHolder conversation, ICdmFormElement parentElement,
+                       ISelectionProvider selectionProvider, int style) {
+               super(formFactory, parentElement, selectionProvider,
+                               ExpandableComposite.CLIENT_INDENT | style);
+
+        setText(getHeading());
+
+        addExpansionListener(this);
+
+        if(clazz==null){
+            createControls(this, style);
+        }
+        else{
+            createControlsByType(this, clazz, SWT.NULL);
+        }
        }
 
+       protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
+           TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
+           layout.topMargin = 10;
+           layout.numColumns = DEFAULT_NUM_COLUMNS;
+
+           getLayoutComposite().setLayout(layout);
+           if(entityClass==null){
+               detailElement = createCdmDetailElement(formElement, style);
+           }
+           else{
+               detailElement = createCdmDetailElementByType(formElement, entityClass, style);
+           }
+       }
 
-       /**
-        * <p>createControls</p>
-        *
-        * @param formElement a {@link eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection} object.
-        * @param style a int.
-        */
        protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
-               TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
-               layout.topMargin = 10;
-               layout.numColumns = 2;
-               
-               getLayoutComposite().setLayout(layout);
-               detailElement = formFactory.createCdmDetailElement(getDetailType(), formElement, style);
+           createControlsByType(formElement, null, style);
+       }
+
+       protected abstract ICdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
+
+       protected ICdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
+           return createCdmDetailElement(parentElement, style);
        }
-       
-       /**
-        * <p>getDetailType</p>
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.DetailType} object.
-        */
-       protected abstract DetailType getDetailType();
-
-       /**
-        * <p>getHeading</p>
-        *
-        * @return the heading for this section
-        */
+
        public abstract String getHeading();
-       
+
        /** {@inheritDoc} */
        @Override
        public void dispose() {
-               if(detailElement instanceof ISelectableElement){
+               if (detailElement instanceof ISelectableElement) {
                        ISelectableElement selectableElement = (ISelectableElement) detailElement;
-                       if(selectableElement.getSelectionArbitrator() != null){
-                               formFactory.destroySelectionArbitrator(selectableElement.getSelectionArbitrator());
+                       if (selectableElement.getSelectionArbitrator() != null) {
+                               formFactory.destroySelectionArbitrator(selectableElement
+                                               .getSelectionArbitrator());
                        }
                }
                super.dispose();
        }
-       
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
-        */
+
        /** {@inheritDoc} */
        @Override
        public void setBackground(Color color) {
-               if(detailElement != null){
-//                     detailComposite.setBackground(color);
+               if (detailElement != null) {
+                       // detailComposite.setBackground(color);
                }
                super.setBackground(color);
        }
-       
-       /**
-        * <p>setEntity</p>
-        *
-        * @param entity a ENTITY object.
-        */
+
        @Override
        public void setEntity(ENTITY entity) {
-               if(detailElement != null){
+               if (detailElement != null) {
                        detailElement.setEntity(entity);
                }
                super.setEntity(entity);
                setSectionTitle();
                layout();
        }
-       
-       /**
-        * <p>setSectionTitle</p>
-        */
-       protected void setSectionTitle(){
+
+       protected void setSectionTitle() {
                String title = "";
-               if(getEntity() != null && (getEntity() instanceof IdentifiableEntity)){
-                        title = ": " + ((IdentifiableEntity) getEntity()).getTitleCache();
+               if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
+                       title = ": " + ((IdentifiableEntity) getEntity()).getTitleCache();
+                       // we have to duplicate ampersands otherwise they are treated as
+                       // mnenomic (see Label.setText() documentation)
+                       // see also #4302
+                       title = title.replace("&", "&&");
                }
-               this.setText(getHeading() + title);
+               this.setText(String.format("%s%s", getHeading(), title));
                setTextClient(createToolbar());
        }
-       
 
-       /**
-        * @return
-        */
        protected Control createToolbar() {
                ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
                return toolBarManager.createControl(this);
        }
-       
-       /**
-        * <p>updateTitle</p>
-        */
-       public void updateTitle(){
-               if(!isDisposed()){
+
+       public void updateTitle() {
+               if (!isDisposed()) {
                        setSectionTitle();
                        layout();
                }
        }
-       
+
        /** {@inheritDoc} */
-       public void setIrrelevant(boolean irrelevant) {
-               if(detailElement != null){
+       @Override
+    public void setIrrelevant(boolean irrelevant) {
+               if (detailElement != null) {
                        detailElement.setIrrelevant(irrelevant);
                }
        }
-       
+
        /** {@inheritDoc} */
-       public void expansionStateChanging(ExpansionEvent e) {
-//             logger.warn("Expansion State Changing");
+       @Override
+    public void expansionStateChanging(ExpansionEvent e) {
+               // logger.warn("Expansion State Changing");
        }
-       
+
        /** {@inheritDoc} */
-       public void expansionStateChanged(ExpansionEvent e) {
-//             logger.warn("Expansion State Changed");
+       @Override
+    public void expansionStateChanged(ExpansionEvent e) {
+               // logger.warn("Expansion State Changed");
        }
 }