Enhance error message for media details view #5587
authorp.plitzner <p.plitzner@bgbm.org>
Tue, 23 Feb 2016 07:50:57 +0000 (08:50 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 23 Feb 2016 14:50:15 +0000 (15:50 +0100)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java

index 0f04524bf052365329bf81d6fb73ebbf9b2ca54c..5e033d7802e7907b29282bc2092e1849d8f3e1fd 100644 (file)
@@ -13,9 +13,11 @@ import java.net.URI;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.sanselan.ImageReadException;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Label;
 
+import eu.etaxonomy.cdm.common.UriUtils;
 import eu.etaxonomy.cdm.common.media.ImageInfo;
 import eu.etaxonomy.cdm.model.media.ImageFile;
 import eu.etaxonomy.cdm.model.media.Media;
@@ -95,7 +97,9 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             if(uri == null){
                 return;
             }
-            imageInfo = ImageInfo.NewInstance(uri, 10000);
+            //first check if uri refers to an actual (non-image) file
+            UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
+            imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
             singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
             if(singleMediaRepresentationPart instanceof ImageFile){
                 ((ImageFile) singleMediaRepresentationPart).setHeight(imageInfo.getHeight());
@@ -106,15 +110,26 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             element_image = formFactory.createImageElement(parentFormElement, uri, style);
             element_image.initImageUri(uri);
             element_image.loadImage();
-            if(lblNoImage!=null){
-                lblNoImage.dispose();
-            }
-            lblNoImage = null;
-        } catch (Exception e) {
-            handleException();
+               disposeErrorLabel();
+        } catch (Exception e){
+               if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
+                       disposeErrorLabel();
+                       handleException(uri, "No preview available for this file type");
+               }
+               else{
+                       disposeErrorLabel();
+                       handleException(uri, "No file found");
+               }
         }
     }
 
+       private void disposeErrorLabel() {
+               if(lblNoImage!=null){
+                   lblNoImage.dispose();
+               }
+               lblNoImage = null;
+       }
+
     public void toggleAdvancedMediaView() {
         if (getEntity().getRepresentations() != null
                 && (getEntity().getRepresentations().size() > 1 ||
@@ -186,14 +201,14 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
         isAdvancedMediaView =  false;
     }
 
-    private void handleException() {
+    private void handleException(URI uri, String labelText) {
         if(element_image!=null){
             element_image.unloadImage();
             element_image.loadImage();
             element_image.dispose();
         }
         if(lblNoImage==null){
-            lblNoImage = formFactory.createLabel(getLayoutComposite(), "No Image found");
+                       lblNoImage = formFactory.createLabel(getLayoutComposite(), labelText);
             lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
             lblNoImage.setAlignment(SWT.CENTER);
         }