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
.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
.swt
.events
.ControlEvent
;
21 import org
.eclipse
.swt
.events
.ControlListener
;
22 import org
.eclipse
.swt
.events
.PaintEvent
;
23 import org
.eclipse
.swt
.events
.PaintListener
;
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
.ui
.forms
.widgets
.TableWrapData
;
30 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
31 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
34 * <p>ImageElement class.</p>
37 * @created Sep 24, 2010
40 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
, ControlListener
{
44 private Rectangle imageMaxBounds
;
46 private Composite container
;
49 * <p>Constructor for ImageElement.</p>
51 * @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
52 * @param parentElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
53 * @param imageUri a {@link java.net.URI} object.
56 protected ImageElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, URI imageUri
, int style
) {
57 super(formFactory
, parentElement
);
59 setImageUri(imageUri
);
61 container
= new Composite(getLayoutComposite(), style
);
62 container
.setLayoutData(CdmFormFactory
.FILL(2, 1));
64 container
.addControlListener(this);
65 container
.addPaintListener(this);
70 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
74 public void setSelected(boolean selected
) {
75 // TODO Auto-generated method stub
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
) {
89 final Display display
= Display
.getCurrent();
90 Job job
= new Job("Loading image: " + imageUri
){
92 protected IStatus
run(IProgressMonitor monitor
) {
94 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
96 image
= new Image(Display
.getCurrent(), imageStream
);
98 display
.asyncExec(new Runnable(){
101 EditorUtil
.reflowDetailsViewer();
102 // getLayoutComposite().layout();
105 return Status
.OK_STATUS
;
106 }catch (Exception e
) {
107 EditorUtil
.errorDialog("Could not load image", getClass(), e
.getMessage(), e
);
109 return Status
.CANCEL_STATUS
;
117 private void calculateImageBounds(){
118 Rectangle imageBounds
= image
.getBounds();
119 Rectangle containerBounds
= container
.getBounds();
121 Integer imgWidth
= imageBounds
.width
;
122 Integer imgHeight
= imageBounds
.height
;
124 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
125 Integer width
= containerBounds
.width
;
126 Float height
= width
* ratio
;
128 imageMaxBounds
= new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
.intValue());
132 * <p>Getter for the field <code>imageUri</code>.</p>
134 * @return the imageUri
136 public URI
getImageUri() {
141 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
145 public void paintControl(PaintEvent e
) {
147 calculateImageBounds();
148 Composite composite
= (Composite
) e
.widget
;
149 TableWrapData layoutData
= CdmFormFactory
.FILL(2, 1);
150 layoutData
.heightHint
= imageMaxBounds
.height
;
151 composite
.setLayoutData(layoutData
);
152 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);
158 * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
162 public void controlMoved(ControlEvent e
) {
163 // TODO Auto-generated method stub
169 * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
173 public void controlResized(ControlEvent e
) {
174 EditorUtil
.reflowDetailsViewer();