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
.SWT
;
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
.Display
;
29 import org
.eclipse
.swt
.widgets
.Event
;
30 import org
.eclipse
.swt
.widgets
.Label
;
31 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
33 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
34 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
37 * <p>ImageElement class.</p>
40 * @created Sep 24, 2010
43 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
{
47 private Rectangle imageMaxBounds
;
49 private Composite container
;
52 * <p>Constructor for ImageElement.</p>
54 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.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 setImageUri(imageUri
);
65 container
= new Composite(getLayoutComposite(), style
);
66 container
.setLayoutData(CdmFormFactory
.FILL(2, 1));
68 container
.addPaintListener(this);
73 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
77 public void setSelected(boolean selected
) {
82 * <p>Setter for the field <code>imageUri</code>.</p>
84 * @param uri a {@link java.net.URI} object.
86 public void setImageUri(URI uri
) {
92 * <p>Getter for the field <code>imageUri</code>.</p>
94 * @return the imageUri
96 public URI
getImageUri() {
100 public void loadImage(){
104 public void loadImage(final Runnable postRunnable
){
105 if(getImageUri() != null){
106 Job job
= new Job("Loading image") {
109 protected IStatus
run(IProgressMonitor monitor
) {
110 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
112 runnable
.run(monitor
);
113 } catch (Exception e
) {
114 StoreUtil
.errorDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
117 return Status
.OK_STATUS
;
124 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
126 final Display display
= getLayoutComposite().getDisplay();
128 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
131 public void run(IProgressMonitor monitor
) {
133 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
134 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
136 image
= new Image(Display
.getCurrent(), imageStream
);
138 // redraw the image container
139 display
.asyncExec(new Runnable(){
142 if(! getLayoutComposite().isDisposed()){
143 Event untypedEvent
= new Event();
144 untypedEvent
.widget
= container
;
145 PaintEvent event
= new PaintEvent(untypedEvent
);
146 event
.gc
= new GC(container
);
148 getLayoutComposite().layout();
153 // execute the external runnable
154 if(postRunnable
!= null){
155 display
.asyncExec(postRunnable
);
158 }catch (Exception e
) {
159 StoreUtil
.warn(getClass(), e
.getMessage());
167 private void calculateImageBounds(){
168 Rectangle imageBounds
= image
.getBounds();
169 Rectangle containerBounds
= container
.getBounds();
171 Integer imgWidth
= imageBounds
.width
;
172 Integer imgHeight
= imageBounds
.height
;
174 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
175 Integer width
= containerBounds
.width
;
176 Float height
= width
* ratio
;
178 imageMaxBounds
= new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
.intValue());
183 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
187 public void paintControl(PaintEvent e
) {
189 calculateImageBounds();
190 Composite composite
= (Composite
) e
.widget
;
191 TableWrapData layoutData
= CdmFormFactory
.FILL(2, 1);
192 layoutData
.heightHint
= imageMaxBounds
.height
;
193 composite
.setLayoutData(layoutData
);
194 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);