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
.model
.MessagingUtils
;
39 * @created Sep 24, 2010
42 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
{
47 private Composite container
;
49 protected ImageElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, URI imageUri
, int style
) {
50 super(formFactory
, parentElement
);
52 container
= new Composite(getLayoutComposite(), style
);
53 container
.setLayoutData(LayoutConstants
.FILL(2, 1));
55 container
.addPaintListener(this);
58 public void initImageUri(URI uri
) throws IOException
, HttpException
{
60 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
61 image
= new Image(Display
.getCurrent(), imageStream
);
65 public URI
getImageUri() {
69 public void loadImage(){
73 public void loadImage(final Runnable postRunnable
){
74 if(getImageUri() != null){
75 Job job
= new Job("Loading image") {
78 protected IStatus
run(IProgressMonitor monitor
) {
79 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
81 runnable
.run(monitor
);
82 } catch (Exception e
) {
83 MessagingUtils
.messageDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
86 return Status
.OK_STATUS
;
93 public void unloadImage(final Runnable postRunnable
) {
94 Job job
= new Job("Unloading image") {
97 protected IStatus
run(IProgressMonitor monitor
) {
98 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
100 runnable
.run(monitor
);
101 } catch (Exception e
) {
102 MessagingUtils
.messageDialog("Could not unload image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
105 return Status
.OK_STATUS
;
112 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
114 final Display display
= getLayoutComposite().getDisplay();
116 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
119 public void run(IProgressMonitor monitor
) {
120 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
122 // redraw the image container
123 display
.asyncExec(new Runnable(){
126 if(! getLayoutComposite().isDisposed()){
127 Event untypedEvent
= new Event();
128 untypedEvent
.widget
= container
;
129 PaintEvent event
= new PaintEvent(untypedEvent
);
130 event
.gc
= new GC(container
);
132 getLayoutComposite().layout();
137 // execute the external runnable
138 if(postRunnable
!= null){
139 display
.asyncExec(postRunnable
);
149 private Rectangle
calculateImageBounds(Image image
, Control control
){
150 Rectangle imageBounds
= image
.getBounds();
151 Rectangle containerBounds
= control
.getBounds();
153 Integer imgWidth
= imageBounds
.width
;
154 Integer imgHeight
= imageBounds
.height
;
156 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
157 Integer width
= containerBounds
.width
;
158 Integer height
= ((Float
) (width
* ratio
)).intValue();
160 return new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
);
165 public void paintControl(PaintEvent e
) {
166 TableWrapData layoutData
= LayoutConstants
.FILL(2, 1);
167 Control control
= (Control
) e
.widget
;
169 Rectangle imageMaxBounds
= calculateImageBounds(image
, control
);
170 layoutData
.heightHint
= imageMaxBounds
.height
;
171 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);
173 layoutData
.heightHint
= 10;
174 e
.gc
.drawRectangle(0, 0, 0, 10);
176 control
.setLayoutData(layoutData
);