2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.ui
.element
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStream
;
16 import org
.apache
.http
.HttpException
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.core
.runtime
.jobs
.Job
;
21 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
22 import org
.eclipse
.swt
.events
.PaintEvent
;
23 import org
.eclipse
.swt
.events
.PaintListener
;
24 import org
.eclipse
.swt
.graphics
.GC
;
25 import org
.eclipse
.swt
.graphics
.Image
;
26 import org
.eclipse
.swt
.graphics
.Rectangle
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Control
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.swt
.widgets
.Event
;
31 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
33 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
34 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
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 private final Runnable postRunnable
= new Runnable(){
52 AbstractUtility
.reflowDetailsViewer();
53 AbstractUtility
.reflowSupplementalViewer();
57 protected ImageElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, URI imageUri
, int style
) {
58 super(formFactory
, parentElement
);
60 container
= new Composite(getLayoutComposite(), style
);
61 container
.setLayoutData(LayoutConstants
.FILL(2, 1));
63 container
.addPaintListener(this);
66 public void initImageUri(URI uri
) throws IOException
, HttpException
{
68 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
69 image
= new Image(Display
.getCurrent(), imageStream
);
73 public URI
getImageUri() {
77 public void loadImage(){
78 if(getImageUri() != null){
79 Job job
= new Job("Loading image") {
82 protected IStatus
run(IProgressMonitor monitor
) {
83 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
85 runnable
.run(monitor
);
86 } catch (Exception e
) {
87 MessagingUtils
.messageDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
90 return Status
.OK_STATUS
;
97 public void unloadImage() {
98 Job job
= new Job("Unloading image") {
101 protected IStatus
run(IProgressMonitor monitor
) {
102 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
104 runnable
.run(monitor
);
105 } catch (Exception e
) {
106 MessagingUtils
.messageDialog("Could not unload image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
109 return Status
.OK_STATUS
;
116 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
118 final Display display
= getLayoutComposite().getDisplay();
120 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
123 public void run(IProgressMonitor monitor
) {
124 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
126 // redraw the image container
127 display
.asyncExec(new Runnable(){
130 if(! getLayoutComposite().isDisposed() && container
!=null){
131 Event untypedEvent
= new Event();
132 untypedEvent
.widget
= container
;
133 PaintEvent event
= new PaintEvent(untypedEvent
);
134 event
.gc
= new GC(container
);
136 getLayoutComposite().layout();
141 // execute the external runnable
142 if(postRunnable
!= null){
143 display
.asyncExec(postRunnable
);
153 private Rectangle
calculateImageBounds(Image image
, Control control
){
154 Rectangle imageBounds
= image
.getBounds();
155 Rectangle containerBounds
= control
.getBounds();
157 Integer imgWidth
= imageBounds
.width
;
158 Integer imgHeight
= imageBounds
.height
;
160 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
161 Integer width
= containerBounds
.width
;
162 Integer height
= ((Float
) (width
* ratio
)).intValue();
164 return new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
);
167 public void dispose(){
181 public void paintControl(PaintEvent e
) {
182 TableWrapData layoutData
= LayoutConstants
.FILL(2, 1);
183 Control control
= (Control
) e
.widget
;
185 Rectangle imageMaxBounds
= calculateImageBounds(image
, control
);
186 layoutData
.heightHint
= imageMaxBounds
.height
;
187 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);
189 layoutData
.heightHint
= 10;
190 e
.gc
.drawRectangle(0, 0, 0, 10);
192 control
.setLayoutData(layoutData
);