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
.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
.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
.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 container
= new Composite(getLayoutComposite(), style
);
63 container
.setLayoutData(CdmFormFactory
.FILL(2, 1));
65 container
.addPaintListener(this);
70 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
74 public void setSelected(boolean selected
) {
79 * <p>Setter for the field <code>imageUri</code>.</p>
81 * @param uri a {@link java.net.URI} object.
82 * @throws HttpException
85 public void initImageUri(URI uri
) throws IOException
, HttpException
{
87 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
88 image
= new Image(Display
.getCurrent(), imageStream
);
93 * <p>Getter for the field <code>imageUri</code>.</p>
95 * @return the imageUri
97 public URI
getImageUri() {
101 public void loadImage(){
107 public void loadImage(final Runnable postRunnable
){
108 if(getImageUri() != null){
109 Job job
= new Job("Loading image") {
112 protected IStatus
run(IProgressMonitor monitor
) {
113 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
115 runnable
.run(monitor
);
116 } catch (Exception e
) {
117 StoreUtil
.errorDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
120 return Status
.OK_STATUS
;
129 public void unloadImage(final Runnable postRunnable
) {
130 Job job
= new Job("Unloading image") {
133 protected IStatus
run(IProgressMonitor monitor
) {
134 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
136 runnable
.run(monitor
);
137 } catch (Exception e
) {
138 StoreUtil
.errorDialog("Could not unload image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
141 return Status
.OK_STATUS
;
148 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
150 final Display display
= getLayoutComposite().getDisplay();
152 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
155 public void run(IProgressMonitor monitor
) {
156 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
158 // redraw the image container
159 display
.asyncExec(new Runnable(){
162 if(! getLayoutComposite().isDisposed()){
163 Event untypedEvent
= new Event();
164 untypedEvent
.widget
= container
;
165 PaintEvent event
= new PaintEvent(untypedEvent
);
166 event
.gc
= new GC(container
);
168 getLayoutComposite().layout();
173 // execute the external runnable
174 if(postRunnable
!= null){
175 display
.asyncExec(postRunnable
);
185 private void calculateImageBounds(){
186 Rectangle imageBounds
= image
.getBounds();
187 Rectangle containerBounds
= container
.getBounds();
189 Integer imgWidth
= imageBounds
.width
;
190 Integer imgHeight
= imageBounds
.height
;
192 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
193 Integer width
= containerBounds
.width
;
194 Float height
= width
* ratio
;
196 imageMaxBounds
= new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
.intValue());
201 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
205 public void paintControl(PaintEvent e
) {
206 TableWrapData layoutData
= CdmFormFactory
.FILL(2, 1);
207 Composite composite
= (Composite
) e
.widget
;
209 calculateImageBounds();
210 layoutData
.heightHint
= imageMaxBounds
.height
;
211 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);
213 layoutData
.heightHint
= 10;
214 e
.gc
.drawRectangle(0, 0, 0, 10);
216 composite
.setLayoutData(layoutData
);