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
.forms
;
13 import java
.io
.InputStream
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
21 import org
.eclipse
.swt
.events
.PaintEvent
;
22 import org
.eclipse
.swt
.events
.PaintListener
;
23 import org
.eclipse
.swt
.graphics
.GC
;
24 import org
.eclipse
.swt
.graphics
.Image
;
25 import org
.eclipse
.swt
.graphics
.Rectangle
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Display
;
28 import org
.eclipse
.swt
.widgets
.Event
;
29 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
31 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
32 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
35 * <p>ImageElement class.</p>
38 * @created Sep 24, 2010
41 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
{
45 private Rectangle imageMaxBounds
;
47 private Composite container
;
50 * <p>Constructor for ImageElement.</p>
52 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
53 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
54 * @param imageUri a {@link java.net.URI} object.
57 protected ImageElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, URI imageUri
, int style
) {
58 super(formFactory
, parentElement
);
60 setImageUri(imageUri
);
63 container
= new Composite(getLayoutComposite(), style
);
64 container
.setLayoutData(CdmFormFactory
.FILL(2, 1));
66 container
.addPaintListener(this);
71 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
75 public void setSelected(boolean selected
) {
80 * <p>Setter for the field <code>imageUri</code>.</p>
82 * @param uri a {@link java.net.URI} object.
84 public void setImageUri(URI uri
) {
90 * <p>Getter for the field <code>imageUri</code>.</p>
92 * @return the imageUri
94 public URI
getImageUri() {
98 public void loadImage(){
102 public void loadImage(final Runnable postRunnable
){
103 if(getImageUri() != null){
104 Job job
= new Job("Loading image") {
107 protected IStatus
run(IProgressMonitor monitor
) {
108 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
110 runnable
.run(monitor
);
111 } catch (Exception e
) {
112 StoreUtil
.errorDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
115 return Status
.OK_STATUS
;
122 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
124 final Display display
= getLayoutComposite().getDisplay();
126 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
129 public void run(IProgressMonitor monitor
) {
131 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
132 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
134 image
= new Image(Display
.getCurrent(), imageStream
);
136 // redraw the image container
137 display
.asyncExec(new Runnable(){
140 if(! getLayoutComposite().isDisposed()){
141 Event untypedEvent
= new Event();
142 untypedEvent
.widget
= container
;
143 PaintEvent event
= new PaintEvent(untypedEvent
);
144 event
.gc
= new GC(container
);
146 getLayoutComposite().layout();
151 // execute the external runnable
152 if(postRunnable
!= null){
153 display
.asyncExec(postRunnable
);
156 }catch (Exception e
) {
157 StoreUtil
.errorDialog("Could not load image", getClass(), e
.getMessage() + ": " + imageUri
, e
);
165 private void calculateImageBounds(){
166 Rectangle imageBounds
= image
.getBounds();
167 Rectangle containerBounds
= container
.getBounds();
169 Integer imgWidth
= imageBounds
.width
;
170 Integer imgHeight
= imageBounds
.height
;
172 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
173 Integer width
= containerBounds
.width
;
174 Float height
= width
* ratio
;
176 imageMaxBounds
= new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
.intValue());
181 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
185 public void paintControl(PaintEvent e
) {
187 calculateImageBounds();
188 Composite composite
= (Composite
) e
.widget
;
189 TableWrapData layoutData
= CdmFormFactory
.FILL(2, 1);
190 layoutData
.heightHint
= imageMaxBounds
.height
;
191 composite
.setLayoutData(layoutData
);
192 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);