3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.element
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
17 import org
.apache
.http
.HttpException
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.core
.runtime
.jobs
.Job
;
22 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
23 import org
.eclipse
.swt
.events
.PaintEvent
;
24 import org
.eclipse
.swt
.events
.PaintListener
;
25 import org
.eclipse
.swt
.graphics
.GC
;
26 import org
.eclipse
.swt
.graphics
.Image
;
27 import org
.eclipse
.swt
.graphics
.Rectangle
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Event
;
32 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
34 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
35 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
38 * <p>ImageElement class.</p>
41 * @created Sep 24, 2010
44 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
{
49 private Composite container
;
52 * <p>Constructor for ImageElement.</p>
54 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
56 * @param imageUri a {@link java.net.URI} object.
59 protected ImageElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, URI imageUri
, int style
) {
60 super(formFactory
, parentElement
);
62 container
= new Composite(getLayoutComposite(), style
);
63 container
.setLayoutData(CdmFormFactory
.FILL(2, 1));
65 container
.addPaintListener(this);
69 * <p>Setter for the field <code>imageUri</code>.</p>
71 * @param uri a {@link java.net.URI} object.
72 * @throws HttpException
75 public void initImageUri(URI uri
) throws IOException
, HttpException
{
77 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
78 image
= new Image(Display
.getCurrent(), imageStream
);
83 * <p>Getter for the field <code>imageUri</code>.</p>
85 * @return the imageUri
87 public URI
getImageUri() {
91 public void loadImage(){
97 public void loadImage(final Runnable postRunnable
){
98 if(getImageUri() != null){
99 Job job
= new Job("Loading image") {
102 protected IStatus
run(IProgressMonitor monitor
) {
103 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
105 runnable
.run(monitor
);
106 } catch (Exception e
) {
107 StoreUtil
.errorDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
110 return Status
.OK_STATUS
;
119 public void unloadImage(final Runnable postRunnable
) {
120 Job job
= new Job("Unloading image") {
123 protected IStatus
run(IProgressMonitor monitor
) {
124 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
126 runnable
.run(monitor
);
127 } catch (Exception e
) {
128 StoreUtil
.errorDialog("Could not unload image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
131 return Status
.OK_STATUS
;
138 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
140 final Display display
= getLayoutComposite().getDisplay();
142 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
145 public void run(IProgressMonitor monitor
) {
146 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
148 // redraw the image container
149 display
.asyncExec(new Runnable(){
152 if(! getLayoutComposite().isDisposed()){
153 Event untypedEvent
= new Event();
154 untypedEvent
.widget
= container
;
155 PaintEvent event
= new PaintEvent(untypedEvent
);
156 event
.gc
= new GC(container
);
158 getLayoutComposite().layout();
163 // execute the external runnable
164 if(postRunnable
!= null){
165 display
.asyncExec(postRunnable
);
175 private Rectangle
calculateImageBounds(Image image
, Control control
){
176 Rectangle imageBounds
= image
.getBounds();
177 Rectangle containerBounds
= control
.getBounds();
179 Integer imgWidth
= imageBounds
.width
;
180 Integer imgHeight
= imageBounds
.height
;
182 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
183 Integer width
= containerBounds
.width
;
184 Integer height
= ((Float
) (width
* ratio
)).intValue();
186 return new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
);
191 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
195 public void paintControl(PaintEvent e
) {
196 TableWrapData layoutData
= CdmFormFactory
.FILL(2, 1);
197 Control control
= (Control
) e
.widget
;
199 Rectangle imageMaxBounds
= calculateImageBounds(image
, control
);
200 layoutData
.heightHint
= imageMaxBounds
.height
;
201 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);
203 layoutData
.heightHint
= 10;
204 e
.gc
.drawRectangle(0, 0, 0, 10);
206 control
.setLayoutData(layoutData
);