Add image preview to media simple view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / ImageElement.java
index 6cd7677b4c1c944e18d32ca260baf149b03f0b8f..698665eae539eedd6b4894b3ded7297a64699a97 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -32,12 +32,9 @@ import org.eclipse.swt.widgets.Event;
 import org.eclipse.ui.forms.widgets.TableWrapData;
 
 import eu.etaxonomy.cdm.common.UriUtils;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
- * <p>ImageElement class.</p>
- *
  * @author n.hoffmann
  * @created Sep 24, 2010
  * @version 1.0
@@ -46,111 +43,87 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
 
        private URI imageUri;
        private Image image;
-       
+
        private Composite container;
 
-       /**
-        * <p>Constructor for ImageElement.</p>
-        *
-        * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
-        * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
-        * @param imageUri a {@link java.net.URI} object.
-        * @param style a int.
-        */
        protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
                super(formFactory, parentElement);
-                               
+
                container = new Composite(getLayoutComposite(), style);
                container.setLayoutData(LayoutConstants.FILL(2, 1));
-               
+
                container.addPaintListener(this);
        }
 
-       /**
-        * <p>Setter for the field <code>imageUri</code>.</p>
-        *
-        * @param uri a {@link java.net.URI} object.
-        * @throws HttpException 
-        * @throws IOException 
-        */
        public void initImageUri(URI uri) throws IOException, HttpException {
                this.imageUri = uri;
                InputStream imageStream = UriUtils.getInputStream(imageUri);
                image = new Image(Display.getCurrent(), imageStream);
        }
-       
-       
-       /**
-        * <p>Getter for the field <code>imageUri</code>.</p>
-        *
-        * @return the imageUri
-        */
+
+
        public URI getImageUri() {
                return imageUri;
        }
-       
+
        public void loadImage(){
                loadImage(null);
        }
-       
-       
-       
+
        public void loadImage(final Runnable postRunnable){
                if(getImageUri() != null){
                        Job job = new Job("Loading image") {
-                               
+
                                @Override
                                protected IStatus run(IProgressMonitor monitor) {
                                        IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
                                        try {
                                                runnable.run(monitor);
                                        } catch (Exception e) {
-                                               StoreUtil.errorDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
+                                               MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
                                        }
-                                       
+
                                        return Status.OK_STATUS;
                                }
                        };
                        job.schedule();
                }
        }
-       
-
 
        public void unloadImage(final Runnable postRunnable) {
                Job job = new Job("Unloading image") {
-                       
+
                        @Override
                        protected IStatus run(IProgressMonitor monitor) {
                                IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
                                try {
                                        runnable.run(monitor);
                                } catch (Exception e) {
-                                       StoreUtil.errorDialog("Could not unload image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
+                                       MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
                                }
-                               
+
                                return Status.OK_STATUS;
                        }
                };
                job.schedule();
-               
+
        }
-       
+
        public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
-                       
-               final Display display = getLayoutComposite().getDisplay(); 
-               
+
+               final Display display = getLayoutComposite().getDisplay();
+
                IRunnableWithProgress runnable = new IRunnableWithProgress(){
-                       
+
                        @Override
                        public void run(IProgressMonitor monitor) {
                                monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
-                                                               
+
                                // redraw the image container
                                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,37 +133,41 @@ public class ImageElement extends AbstractCdmFormElement implements PaintListene
                                                }
                                        }
                                });
-                               
+
                                // execute the external runnable
                                if(postRunnable != null){
                                        display.asyncExec(postRunnable);
                                }
-                               monitor.done();         
+                               monitor.done();
                        }
 
                };
-               
+
                return runnable;
        }
-       
+
        private Rectangle calculateImageBounds(Image image, Control control){
                Rectangle imageBounds = image.getBounds();
                Rectangle containerBounds = control.getBounds();
-               
+
                Integer imgWidth = imageBounds.width;
                Integer imgHeight = imageBounds.height;
-               
+
                Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
                Integer width = containerBounds.width;
                Integer height = ((Float) (width * ratio)).intValue();
-               
+
                return new Rectangle(containerBounds.x, containerBounds.y, width, height);
        }
 
+       public void dispose(){
+           image.dispose();
+           image = null;
+           imageUri = null;
+           container.dispose();
+           container = null;
+       }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
-        */
        /** {@inheritDoc} */
        @Override
        public void paintControl(PaintEvent e) {