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
;
36 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
39 * <p>ImageElement class.</p>
42 * @created Sep 24, 2010
45 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
{
50 private Composite container
;
53 * <p>Constructor for ImageElement.</p>
55 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
56 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
57 * @param imageUri a {@link java.net.URI} object.
60 protected ImageElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, URI imageUri
, int style
) {
61 super(formFactory
, parentElement
);
63 container
= new Composite(getLayoutComposite(), style
);
64 container
.setLayoutData(LayoutConstants
.FILL(2, 1));
66 container
.addPaintListener(this);
70 * <p>Setter for the field <code>imageUri</code>.</p>
72 * @param uri a {@link java.net.URI} object.
73 * @throws HttpException
76 public void initImageUri(URI uri
) throws IOException
, HttpException
{
78 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
79 image
= new Image(Display
.getCurrent(), imageStream
);
84 * <p>Getter for the field <code>imageUri</code>.</p>
86 * @return the imageUri
88 public URI
getImageUri() {
92 public void loadImage(){
98 public void loadImage(final Runnable postRunnable
){
99 if(getImageUri() != null){
100 Job job
= new Job("Loading image") {
103 protected IStatus
run(IProgressMonitor monitor
) {
104 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
106 runnable
.run(monitor
);
107 } catch (Exception e
) {
108 StoreUtil
.errorDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
111 return Status
.OK_STATUS
;
120 public void unloadImage(final Runnable postRunnable
) {
121 Job job
= new Job("Unloading image") {
124 protected IStatus
run(IProgressMonitor monitor
) {
125 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
127 runnable
.run(monitor
);
128 } catch (Exception e
) {
129 StoreUtil
.errorDialog("Could not unload image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
132 return Status
.OK_STATUS
;
139 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
141 final Display display
= getLayoutComposite().getDisplay();
143 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
146 public void run(IProgressMonitor monitor
) {
147 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
149 // redraw the image container
150 display
.asyncExec(new Runnable(){
153 if(! getLayoutComposite().isDisposed()){
154 Event untypedEvent
= new Event();
155 untypedEvent
.widget
= container
;
156 PaintEvent event
= new PaintEvent(untypedEvent
);
157 event
.gc
= new GC(container
);
159 getLayoutComposite().layout();
164 // execute the external runnable
165 if(postRunnable
!= null){
166 display
.asyncExec(postRunnable
);
176 private Rectangle
calculateImageBounds(Image image
, Control control
){
177 Rectangle imageBounds
= image
.getBounds();
178 Rectangle containerBounds
= control
.getBounds();
180 Integer imgWidth
= imageBounds
.width
;
181 Integer imgHeight
= imageBounds
.height
;
183 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
184 Integer width
= containerBounds
.width
;
185 Integer height
= ((Float
) (width
* ratio
)).intValue();
187 return new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
);
192 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
196 public void paintControl(PaintEvent e
) {
197 TableWrapData layoutData
= LayoutConstants
.FILL(2, 1);
198 Control control
= (Control
) e
.widget
;
200 Rectangle imageMaxBounds
= calculateImageBounds(image
, control
);
201 layoutData
.heightHint
= imageMaxBounds
.height
;
202 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);
204 layoutData
.heightHint
= 10;
205 e
.gc
.drawRectangle(0, 0, 0, 10);
207 control
.setLayoutData(layoutData
);