Remove duplicate code for advanced media view (#4862)
authorPatrick Plitzner <p.plitzner@bgbm.org>
Sat, 27 Jun 2015 12:16:14 +0000 (14:16 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 29 Jun 2015 11:35:36 +0000 (13:35 +0200)
Refactored code and added interface and abstract class to avoid
duplicate code.

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/MediaDetailsSection.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/AbstractTogglableMediaDetailsSection.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/ITogglableMediaElement.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaMetaElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/media/MediaSpecimenGeneralDetailElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/media/MediaSpecimenGeneralDetailSection.java

index a4aca0cdea6031c80fa91da4dda52329cd7096d7..5bb0e1b4aa209a9a12b24ca5c250f4380551a0e1 100644 (file)
 
 package eu.etaxonomy.taxeditor.ui.section.description;
 
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.action.ToolBarManager;
 import org.eclipse.jface.viewers.ISelectionProvider;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Control;
 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.media.Media;
@@ -25,6 +19,7 @@ import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.media.AbstractTogglableMediaDetailsSection;
 import eu.etaxonomy.taxeditor.ui.section.media.MediaMetaElement;
 
 /**
@@ -34,90 +29,32 @@ import eu.etaxonomy.taxeditor.ui.section.media.MediaMetaElement;
  * @created Jun 15, 2010
  * @version 1.0
  */
-public class MediaDetailsSection extends AbstractCdmDetailSection<Media> {
+public class MediaDetailsSection extends AbstractTogglableMediaDetailsSection<Media> {
 
-       private MediaMetaElement element_media;
-
-       /**
-        * <p>Constructor for MediaDetailsSection.</p>
-        *
-        * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
-        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
-        * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
-        * @param style a int.
-        */
        public MediaDetailsSection(CdmFormFactory formFactory, ConversationHolder conversation,
                        ICdmFormElement parentElement,
                        ISelectionProvider selectionProvider, int style) {
                super(formFactory, conversation, parentElement, selectionProvider, style);
        }
 
-       /** {@inheritDoc} */
-       @Override
-       protected void createControls(AbstractCdmDetailSection formElement,
-                       int style) {
-               TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
-               layout.topMargin = 10;
-               layout.numColumns = 2;
-
-               getLayoutComposite().setLayout(layout);
-
-
-       }
-
-       @Override
-    protected Control createToolbar() {
-        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
-
-        //FIXME: duplicate code (see #4598)
-        final String label = "Change View Type";
-
-        Action addAction = new Action(label, IAction.AS_PUSH_BUTTON) {
-            /* (non-Javadoc)
-             * @see org.eclipse.jface.action.Action#run()
-             */
-            @Override
-            public void run() {
-                element_media.toggleAdvancedMediaView();
-            }
-        };
-        addAction.setToolTipText(label);
-
-        toolBarManager.add(addAction);
-
-        return toolBarManager.createControl(this);
-    }
-
-       /**
-        * <p>setEntity</p>
-        *
-        * @param entity a {@link eu.etaxonomy.cdm.model.media.Media} object.
-        */
        @Override
     public void setEntity(Media entity) {
-               if(element_media != null){
+               if(getTogglableMediaElement() != null){
                        this.removeElements();
                }
-               element_media = (MediaMetaElement) formFactory.createEntityCollectionElement(this, entity, null, null, ExpandableComposite.EXPANDED);
+               setTogglableMediaElement((MediaMetaElement) formFactory.createEntityCollectionElement(this, entity, null, null, ExpandableComposite.EXPANDED));
                super.setEntity(entity);
                setSectionTitle();
         setTextClient(createToolbar());
                layout();
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailSection#getHeading()
-        */
        /** {@inheritDoc} */
        @Override
        public String getHeading() {
                return "Image";
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection#createCdmDetailElement(eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection, int)
-        */
        @Override
        protected AbstractCdmDetailElement<Media> createCdmDetailElement(AbstractCdmDetailSection<Media> parentElement, int style) {
            return null;
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/AbstractTogglableMediaDetailsSection.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/AbstractTogglableMediaDetailsSection.java
new file mode 100644 (file)
index 0000000..9a78854
--- /dev/null
@@ -0,0 +1,70 @@
+// $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.
+*/
+
+package eu.etaxonomy.taxeditor.ui.section.media;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.ToolBarManager;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Control;
+
+import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
+import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
+
+/**
+ *
+ * @author pplitzner
+ * @date Jun 25, 2015
+ *
+ */
+public abstract class AbstractTogglableMediaDetailsSection<ENTITY> extends AbstractCdmDetailSection<ENTITY> {
+
+       private ITogglableMediaElement togglableMediaElement;
+
+       public AbstractTogglableMediaDetailsSection(CdmFormFactory formFactory, ConversationHolder conversation,
+                       ICdmFormElement parentElement,
+                       ISelectionProvider selectionProvider, int style) {
+               super(formFactory, conversation, parentElement, selectionProvider, style);
+       }
+
+       @Override
+    protected Control createToolbar() {
+        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
+
+        final String label = "Change View Type";
+
+        Action addAction = new Action(label, IAction.AS_PUSH_BUTTON) {
+            @Override
+            public void run() {
+                if(togglableMediaElement!=null){
+                    togglableMediaElement.toggleAdvancedMediaView();
+                }
+            }
+        };
+        addAction.setToolTipText(label);
+
+        toolBarManager.add(addAction);
+
+        return toolBarManager.createControl(this);
+    }
+
+    public void setTogglableMediaElement(ITogglableMediaElement togglableMediaElement) {
+        this.togglableMediaElement = togglableMediaElement;
+    }
+
+    public ITogglableMediaElement getTogglableMediaElement() {
+        return togglableMediaElement;
+    }
+
+}
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/ITogglableMediaElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/ITogglableMediaElement.java
new file mode 100644 (file)
index 0000000..80d664b
--- /dev/null
@@ -0,0 +1,21 @@
+// $Id$
+/**
+* Copyright (C) 2015 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.media;
+
+/**
+ * @author pplitzner
+ * @date Jun 27, 2015
+ *
+ */
+public interface ITogglableMediaElement {
+
+    public void toggleAdvancedMediaView();
+
+}
index 689ba614669830c906c656175e38ef7fb31fd7b7..67dd752ccbc37948b3f7ae09c575cb9408932704 100644 (file)
@@ -21,52 +21,23 @@ import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 
 /**
- * <p>
- * MediaElement class.
- * </p>
- *
  * @author n.hoffmann
  * @created Nov 17, 2009
  * @version 1.0
  */
-public class MediaMetaElement extends AbstractEntityCollectionElement<Media> {
+public class MediaMetaElement extends AbstractEntityCollectionElement<Media> implements ITogglableMediaElement{
 
     private EntitySelectionElement<AgentBase> selection_artist;
     private LanguageStringWithLabelElement text_description;
     private LanguageStringWithLabelElement text_title;
     private MediaDetailElement mediaDetailElement;
 
-    /**
-     * <p>
-     * Constructor for MediaElement.
-     * </p>
-     *
-     * @param formElement
-     *            a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
-     *            object.
-     * @param element
-     *            a {@link eu.etaxonomy.cdm.model.media.Media} object.
-     * @param removeListener
-     *            a {@link org.eclipse.swt.events.SelectionListener} object.
-     * @param style
-     *            a int.
-     * @param cdmFormFactory
-     *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
-     *            object.
-     */
     public MediaMetaElement(CdmFormFactory cdmFormFactory,
             AbstractFormSection<?> formElement, Media element,
             SelectionListener removeListener, boolean isChoosableEntity, int style) {
         super(cdmFormFactory, formElement, element, removeListener, isChoosableEntity, null, style);
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see
-     * eu.etaxonomy.taxeditor.forms.entitysections.AbstractEntitySetElementComposite
-     * #createControls(org.eclipse.swt.widgets.Composite, int)
-     */
     /** {@inheritDoc} */
     @Override
     public void createControls(ICdmFormElement element, int style) {
@@ -98,25 +69,16 @@ public class MediaMetaElement extends AbstractEntityCollectionElement<Media> {
         text_title.setLanguageString(title);
     }
 
-    /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionElement#selectFromDialog()
-     */
     @Override
     protected Media selectFromDialog() {
         return MediaSelectionDialog.select(AbstractUtility.getShell(), null, null);
     }
 
+    @Override
     public void toggleAdvancedMediaView(){
         mediaDetailElement.toggleAdvancedMediaView();
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see
-     * eu.etaxonomy.taxeditor.section.AbstractEntityCollectionElement#handleEvent
-     * (java.lang.Object)
-     */
     /** {@inheritDoc} */
     @Override
     public void handleEvent(Object eventSource) {
@@ -132,10 +94,4 @@ public class MediaMetaElement extends AbstractEntityCollectionElement<Media> {
         }
     }
 
-    /**
-     * @return
-     */
-    public boolean isAdvancedMediaView() {
-        return mediaDetailElement.isAdvancedMediaView();
-    }
 }
index 0f6000b34a90e585d58d10cdddfe896ddcb65be1..840631c75643c6a50e5558efa115d0a5dd977484 100644 (file)
@@ -32,6 +32,7 @@ import eu.etaxonomy.taxeditor.ui.element.LanguageStringWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.mvc.element.DateElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
+import eu.etaxonomy.taxeditor.ui.section.media.ITogglableMediaElement;
 import eu.etaxonomy.taxeditor.ui.section.media.MediaDetailElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 
@@ -40,7 +41,7 @@ import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
  * @date 16.12.2013
  *
  */
-public class MediaSpecimenGeneralDetailElement extends AbstractCdmDetailElement<MediaSpecimen> {
+public class MediaSpecimenGeneralDetailElement extends AbstractCdmDetailElement<MediaSpecimen> implements ITogglableMediaElement{
 
     private final TermVocabulary mediaSpecimenVocabulary = CdmStore.getService(IVocabularyService.class).find(VocabularyEnum.MediaSpecimenKindOfUnit.getUuid());
 
@@ -63,13 +64,6 @@ public class MediaSpecimenGeneralDetailElement extends AbstractCdmDetailElement<
         super(formFactory, formElement);
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see
-     * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#createControls
-     * (eu.etaxonomy.taxeditor.forms.ICdmFormElement, java.lang.Object, int)
-     */
     @Override
     protected void createControls(ICdmFormElement formElement, MediaSpecimen entity, int style) {
         Media media = entity.getMediaSpecimen();
@@ -101,13 +95,6 @@ public class MediaSpecimenGeneralDetailElement extends AbstractCdmDetailElement<
         }
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see
-     * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java
-     * .lang.Object)
-     */
     @Override
     public void handleEvent(Object eventSource) {
         Media media = getEntity().getMediaSpecimen();
@@ -157,7 +144,8 @@ public class MediaSpecimenGeneralDetailElement extends AbstractCdmDetailElement<
         }
     }
 
-    public void toogleAdvancedMediaView() {
+    @Override
+    public void toggleAdvancedMediaView() {
         if(mediaDetailElement!=null){
             mediaDetailElement.toggleAdvancedMediaView();
         }
index 16894a55b3c75d4c8d42492204cbf001c66c8ad2..fc9eb82dc214a61c24b4fcfab5987ef57839b8e5 100644 (file)
@@ -9,12 +9,7 @@
 */
 package eu.etaxonomy.taxeditor.ui.section.occurrence.media;
 
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.action.ToolBarManager;
 import org.eclipse.jface.viewers.ISelectionProvider;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Control;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
@@ -22,93 +17,36 @@ import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.media.AbstractTogglableMediaDetailsSection;
 
 /**
  * @author pplitzner
  * @date 12.12.2013
  *
  */
-public class MediaSpecimenGeneralDetailSection  extends AbstractCdmDetailSection<MediaSpecimen> {
+public class MediaSpecimenGeneralDetailSection extends AbstractTogglableMediaDetailsSection<MediaSpecimen> {
 
-    private MediaSpecimenGeneralDetailElement mediaSpecimenGeneralDetailElement;
-
-    /**
-     * @param formFactory
-     * @param conversation
-     * @param parentElement
-     * @param selectionProvider
-     * @param style
-     */
     public MediaSpecimenGeneralDetailSection(CdmFormFactory formFactory,
             ConversationHolder conversation, ICdmFormElement parentElement,
             ISelectionProvider selectionProvider, int style) {
         super(formFactory, conversation, parentElement, selectionProvider, style);
     }
 
-
-    /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailSection#getHeading()
-     */
     @Override
     public String getHeading() {
         return "General";
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.forms.widgets.ExpandableComposite#setText(java.lang.String)
-     */
     @Override
     public void setText(String title) {
-//        String text = null;
-//        if(getEntity()!=null && getEntity().getKindOfUnit()!=null){
-//            if(getEntity().getKindOfUnit().equals(DerivateLabelProvider.getArtworkTerm())){
-//                text = "Artwork";
-//            }
-//            else if(getEntity().getKindOfUnit().equals(DerivateLabelProvider.getLivingPlantPhotoTerm())){
-//                text = "Living Plant Photo";
-//            }
-//            else if(getEntity().getKindOfUnit().equals(DerivateLabelProvider.getSpecimenScanTerm())){
-//                text = "Specimen Scan";
-//            }
-//            else if(getEntity().getKindOfUnit().equals(DerivateLabelProvider.getDetailImageTerm())){
-//                text = "Detail Image";
-//            }
-//        }
-//        if(text!=null){
-//            super.setText(text);
-//        }
-//        else{
             super.setText(title);
-//        }
     }
 
-    @Override
-    protected Control createToolbar() {
-        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
-
-        String actionLabel = "Change View Type";
-        Action addAction = new Action(actionLabel, IAction.AS_PUSH_BUTTON) {
-            /* (non-Javadoc)
-             * @see org.eclipse.jface.action.Action#run()
-             */
-            @Override
-            public void run() {
-                mediaSpecimenGeneralDetailElement.toogleAdvancedMediaView();
-            }
-        };
-        addAction.setToolTipText(actionLabel);
-
-        toolBarManager.add(addAction);
-
-        return toolBarManager.createControl(this);
-    }
-
-    /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection#createCdmDetailElement(eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection, int)
-     */
     @Override
     protected AbstractCdmDetailElement<MediaSpecimen> createCdmDetailElement(AbstractCdmDetailSection<MediaSpecimen> parentElement, int style) {
-        mediaSpecimenGeneralDetailElement = formFactory.createMediaSpecimenGeneralDetailElement(parentElement);
+        MediaSpecimenGeneralDetailElement mediaSpecimenGeneralDetailElement = formFactory.createMediaSpecimenGeneralDetailElement(parentElement);
+        setTogglableMediaElement(mediaSpecimenGeneralDetailElement);
         return mediaSpecimenGeneralDetailElement;
     }
+
 }