Handle image loading
authorPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 29 Oct 2015 10:02:19 +0000 (11:02 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 29 Oct 2015 10:04:49 +0000 (11:04 +0100)
- add label to indicate that image was not found
- refactored code

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/ImageFileElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java

index 698665eae539eedd6b4894b3ded7297a64699a97..c041c7a236f72e651d1fa530b4921cde33c7375e 100644 (file)
@@ -32,6 +32,7 @@ import org.eclipse.swt.widgets.Event;
 import org.eclipse.ui.forms.widgets.TableWrapData;
 
 import eu.etaxonomy.cdm.common.UriUtils;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
@@ -46,6 +47,14 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
 
        private Composite container;
 
+       private final Runnable postRunnable = new Runnable(){
+        @Override
+        public void run() {
+            AbstractUtility.reflowDetailsViewer();
+            AbstractUtility.reflowSupplementalViewer();
+        }
+    };
+
        protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
                super(formFactory, parentElement);
 
@@ -67,10 +76,6 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
        }
 
        public void loadImage(){
-               loadImage(null);
-       }
-
-       public void loadImage(final Runnable postRunnable){
                if(getImageUri() != null){
                        Job job = new Job("Loading image") {
 
@@ -90,7 +95,7 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
                }
        }
 
-       public void unloadImage(final Runnable postRunnable) {
+       public void unloadImage() {
                Job job = new Job("Unloading image") {
 
                        @Override
@@ -161,11 +166,15 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
        }
 
        public void dispose(){
-           image.dispose();
-           image = null;
+           if(image!=null){
+               image.dispose();
+               image = null;
+           }
            imageUri = null;
-           container.dispose();
-           container = null;
+           if(container!=null){
+               container.dispose();
+               container = null;
+           }
        }
 
        /** {@inheritDoc} */
index 7a5618b27e2fa1d5f9f73012d710bc414a6a4a48..f260d1b8ee4e19f4e4140595c70e63267b09161d 100644 (file)
@@ -19,7 +19,6 @@ import org.eclipse.swt.events.SelectionListener;
 
 import eu.etaxonomy.cdm.common.media.ImageInfo;
 import eu.etaxonomy.cdm.model.media.ImageFile;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
@@ -40,16 +39,6 @@ public class ImageFileElement extends MediaRepresentationPartElement<ImageFile>
        private KeyValueViewerElement element_keyValue;
        private ImageElement element_image;
 
-
-       private final Runnable postRunnable = new Runnable(){
-               @Override
-        public void run() {
-                       AbstractUtility.reflowDetailsViewer();
-                       AbstractUtility.reflowSupplementalViewer();
-               }
-       };
-
-
        public ImageFileElement(CdmFormFactory cdmFormFactory,
                        AbstractFormSection section, ImageFile element,
                        SelectionListener removeListener, int style) {
@@ -85,7 +74,7 @@ public class ImageFileElement extends MediaRepresentationPartElement<ImageFile>
        private void loadImage(URI uri, boolean updateDimensions) throws IOException, HttpException{
                element_image.initImageUri(uri);
 
-               element_image.loadImage(postRunnable );
+               element_image.loadImage();
 
 
                if(uri == null){
@@ -139,16 +128,10 @@ public class ImageFileElement extends MediaRepresentationPartElement<ImageFile>
        }
 
        protected void handleException(Exception e) {
-               element_image.unloadImage(postRunnable);
+               element_image.unloadImage();
                text_height.setNumber(0);
                text_width.setNumber(0);
 
-               element_image.loadImage(new Runnable(){
-                       @Override
-            public void run() {
-                               AbstractUtility.reflowDetailsViewer();
-                               AbstractUtility.reflowSupplementalViewer();
-                       }
-               });
+               element_image.loadImage();
        }
 }
index cf5abd37782ee4ecc6bf02ec3d259da656e7b300..a2a6de62061cf58a6979d624fa55a0e59315df85 100644 (file)
@@ -15,12 +15,14 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.http.HttpException;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Label;
 
+import eu.etaxonomy.cdm.common.media.ImageInfo;
 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;
@@ -51,14 +53,7 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
      */
     private String uriBuffer;
     private ImageElement element_image;
-
-    private final Runnable postRunnable = new Runnable(){
-        @Override
-        public void run() {
-            AbstractUtility.reflowDetailsViewer();
-            AbstractUtility.reflowSupplementalViewer();
-        }
-    };
+    private Label lblNoImage;
 
     public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
         super(formFactory, formElement);
@@ -89,10 +84,18 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             if(uri==null){
                 uriBuffer=textUri.getText();
             }
-            try {
-                loadImage(singleMediaRepresentationPart.getUri(), true);
-            } catch (Exception e) {
-                handleException(e);
+            else{
+
+                ImageInfo imageInfo;
+                try {
+                    imageInfo = ImageInfo.NewInstance(uri, 10000);
+                    singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
+                    singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
+                    element_image = formFactory.createImageElement(parentFormElement, uri, style);
+                    loadImage(singleMediaRepresentationPart.getUri());
+                } catch (Exception e) {
+                    handleException();
+                }
             }
         }
     }
@@ -146,9 +149,9 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
 
             element_image = formFactory.createImageElement(parentFormElement, null, style);
             try {
-                loadImage(singleMediaRepresentationPart.getUri(), false);
+                loadImage(singleMediaRepresentationPart.getUri());
             } catch (Exception e) {
-                handleException(e);
+                handleException();
             }
         }
     }
@@ -173,24 +176,27 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
         isAdvancedMediaView =  false;
     }
 
-    private void loadImage(URI uri, boolean updateDimensions) throws IOException, HttpException{
+    private void loadImage(URI uri) throws IOException, HttpException{
         element_image.initImageUri(uri);
-        element_image.loadImage(postRunnable);
+        element_image.loadImage();
         if(uri == null){
             return;
         }
+        lblNoImage.dispose();
+        lblNoImage = null;
     }
 
-    private void handleException(Exception e) {
-        element_image.unloadImage(postRunnable);
-        element_image.loadImage(new Runnable(){
-            @Override
-            public void run() {
-                AbstractUtility.reflowDetailsViewer();
-                AbstractUtility.reflowSupplementalViewer();
-            }
-        });
+    private void handleException() {
+        element_image.unloadImage();
+        element_image.loadImage();
+        element_image.dispose();
+        if(lblNoImage==null){
+            lblNoImage = formFactory.createLabel(getLayoutComposite(), "No Image found");
+            lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+            lblNoImage.setAlignment(SWT.CENTER);
+        }
     }
+
     public boolean isAdvancedMediaView() {
         return isAdvancedMediaView;
     }