ref #8129 Load images asynchronously in media details view
authorPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 11 Jul 2019 10:48:34 +0000 (12:48 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 11 Jul 2019 10:48:34 +0000 (12:48 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java

index a4088c68ca8566ab16947cb02de5be9b98424c97..bb090fec15570974d9cf114f3f572a74395bc03e 100644 (file)
@@ -8,11 +8,17 @@
 */
 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.apache.sanselan.ImageReadException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Label;
 
@@ -42,6 +48,51 @@ import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
  */
 public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
 
+    /**
+     * @author pplitzner
+     * @since Jul 11, 2019
+     *
+     */
+    private final class LoadImageJob extends Job {
+        private URI uri;
+        private LoadImageJob(URI uri, String name) {
+            super(name);
+            this.uri = uri;
+        }
+
+        @Override
+        protected IStatus run(IProgressMonitor monitor) {
+            ImageInfo imageInfo;
+            try {
+                //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
+                MediaDetailElement.this.getLayoutComposite().getDisplay().asyncExec(()->{
+                    singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
+                    if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
+                        ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
+                        image.setHeight(imageInfo.getHeight());
+                        image.setWidth(imageInfo.getWidth());
+                    }
+                    singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
+                    singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
+                    element_image = formFactory.createImageElement(parentFormElement, uri, style);
+                    StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
+                    try {
+                        element_image.initImageUri(uri);
+                    } catch (IOException | HttpException e) {
+                        exception(e, uri);
+                    }
+                    element_image.loadImage();
+                    disposeErrorLabel();
+                });
+            } catch (IOException|HttpException e){
+               exception(e, uri);
+            }
+            return Status.OK_STATUS;
+        }
+    }
+
     private UriWithLabelElement textUri;
     private MediaRepresentationSection section_mediaRepresentation;
 
@@ -89,42 +140,22 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
                 uriBuffer=textUri.getText();
             }
             else{
-                createImageElement(uri);
+                new LoadImageJob(uri, "loadImage").schedule();
             }
         }
     }
 
-    private void createImageElement(URI uri) {
-        ImageInfo imageInfo;
-        try {
-            if(uri == null){
-                return;
+    private void exception(Exception e, URI uri){
+        getLayoutComposite().getDisplay().asyncExec(()->{
+            if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
+                disposeErrorLabel();
+                handleException(uri, "No preview available for this file type");
             }
-            //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.isInstanceOf(ImageFile.class)){
-               ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
-                image.setHeight(imageInfo.getHeight());
-                image.setWidth(imageInfo.getWidth());
+            else{
+                disposeErrorLabel();
+                handleException(uri, "No file found");
             }
-            singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
-            singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
-            element_image = formFactory.createImageElement(parentFormElement, uri, style);
-            element_image.initImageUri(uri);
-            element_image.loadImage();
-               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() {
@@ -182,7 +213,7 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             }
             textUri.getLayoutComposite().layout();
 
-            createImageElement(singleMediaRepresentationPart.getUri());
+            new LoadImageJob(singleMediaRepresentationPart.getUri(), "loadImage").schedule();
         }
     }