Refactored image loading
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 7 Dec 2015 15:11:49 +0000 (16:11 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 7 Dec 2015 15:19:58 +0000 (16:19 +0100)
 - fix some NPEs

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java

index a2a6de6..9ae9ade 100644 (file)
@@ -9,12 +9,10 @@
 */
 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 org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Label;
 
@@ -85,18 +83,29 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
                 uriBuffer=textUri.getText();
             }
             else{
+                createImageElement(uri);
+            }
+        }
+    }
 
-                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();
-                }
+    private void createImageElement(URI uri) {
+        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);
+            element_image.initImageUri(uri);
+            element_image.loadImage();
+            if(uri == null){
+                return;
+            }
+            if(lblNoImage!=null){
+                lblNoImage.dispose();
             }
+            lblNoImage = null;
+        } catch (Exception e) {
+            handleException();
         }
     }
 
@@ -147,12 +156,7 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             }
             textUri.getLayoutComposite().layout();
 
-            element_image = formFactory.createImageElement(parentFormElement, null, style);
-            try {
-                loadImage(singleMediaRepresentationPart.getUri());
-            } catch (Exception e) {
-                handleException();
-            }
+            createImageElement(singleMediaRepresentationPart.getUri());
         }
     }
 
@@ -176,20 +180,12 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
         isAdvancedMediaView =  false;
     }
 
-    private void loadImage(URI uri) throws IOException, HttpException{
-        element_image.initImageUri(uri);
-        element_image.loadImage();
-        if(uri == null){
-            return;
-        }
-        lblNoImage.dispose();
-        lblNoImage = null;
-    }
-
     private void handleException() {
-        element_image.unloadImage();
-        element_image.loadImage();
-        element_image.dispose();
+        if(element_image!=null){
+            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));