Add image preview to media simple view
authorPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 29 Oct 2015 06:17:22 +0000 (07:17 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 29 Oct 2015 10:04:49 +0000 (11:04 +0100)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/ImageElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java

index 92ab62f7f7b61e0d6a505bd9aa7beba3fd552549..698665eae539eedd6b4894b3ded7297a64699a97 100644 (file)
@@ -123,7 +123,7 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
                                display.asyncExec(new Runnable(){
                                        @Override
                                        public void run() {
-                                               if(! getLayoutComposite().isDisposed()){
+                                               if(! getLayoutComposite().isDisposed() && container!=null){
                                                        Event untypedEvent = new Event();
                                                        untypedEvent.widget = container;
                                                        PaintEvent event = new PaintEvent(untypedEvent);
@@ -160,6 +160,14 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
                return new Rectangle(containerBounds.x, containerBounds.y, width, height);
        }
 
+       public void dispose(){
+           image.dispose();
+           image = null;
+           imageUri = null;
+           container.dispose();
+           container = null;
+       }
+
        /** {@inheritDoc} */
        @Override
        public void paintControl(PaintEvent e) {
index 750fc27cef907a0342620fea4c5718d49c8ea6f3..cf5abd37782ee4ecc6bf02ec3d259da656e7b300 100644 (file)
@@ -9,19 +9,24 @@
 */
 package eu.etaxonomy.taxeditor.ui.section.media;
 
+import java.io.IOException;
 import java.net.URI;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.http.HttpException;
+
 import eu.etaxonomy.cdm.model.media.Media;
 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
 import eu.etaxonomy.cdm.model.media.MediaUtils;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.ImageElement;
 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
 import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
@@ -45,6 +50,15 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
      * Used to store the URI even if it is invalid and thus cannot be stored in CDM
      */
     private String uriBuffer;
+    private ImageElement element_image;
+
+    private final Runnable postRunnable = new Runnable(){
+        @Override
+        public void run() {
+            AbstractUtility.reflowDetailsViewer();
+            AbstractUtility.reflowSupplementalViewer();
+        }
+    };
 
     public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
         super(formFactory, formElement);
@@ -75,6 +89,11 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             if(uri==null){
                 uriBuffer=textUri.getText();
             }
+            try {
+                loadImage(singleMediaRepresentationPart.getUri(), true);
+            } catch (Exception e) {
+                handleException(e);
+            }
         }
     }
 
@@ -99,6 +118,10 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             if(textUri!=null){
                 removeElementsAndControls(textUri);
             }
+            if(element_image!=null){
+                element_image.dispose();
+                element_image = null;
+            }
             section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
             section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
             section_mediaRepresentation.setEntity(getEntity());
@@ -120,6 +143,13 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
                 textUri.parseText();
             }
             textUri.getLayoutComposite().layout();
+
+            element_image = formFactory.createImageElement(parentFormElement, null, style);
+            try {
+                loadImage(singleMediaRepresentationPart.getUri(), false);
+            } catch (Exception e) {
+                handleException(e);
+            }
         }
     }
 
@@ -143,6 +173,24 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
         isAdvancedMediaView =  false;
     }
 
+    private void loadImage(URI uri, boolean updateDimensions) throws IOException, HttpException{
+        element_image.initImageUri(uri);
+        element_image.loadImage(postRunnable);
+        if(uri == null){
+            return;
+        }
+    }
+
+    private void handleException(Exception e) {
+        element_image.unloadImage(postRunnable);
+        element_image.loadImage(new Runnable(){
+            @Override
+            public void run() {
+                AbstractUtility.reflowDetailsViewer();
+                AbstractUtility.reflowSupplementalViewer();
+            }
+        });
+    }
     public boolean isAdvancedMediaView() {
         return isAdvancedMediaView;
     }