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
.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
.swt
.SWTException
;
23 import org
.eclipse
.swt
.events
.ControlEvent
;
24 import org
.eclipse
.swt
.events
.ControlListener
;
25 import org
.eclipse
.swt
.events
.PaintEvent
;
26 import org
.eclipse
.swt
.events
.PaintListener
;
27 import org
.eclipse
.swt
.graphics
.Image
;
28 import org
.eclipse
.swt
.graphics
.Rectangle
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
33 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
34 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
37 * <p>ImageElement class.</p>
40 * @created Sep 24, 2010
43 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
, ControlListener
{
47 private Rectangle imageMaxBounds
;
49 private Composite container
;
52 * <p>Constructor for ImageElement.</p>
54 * @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.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
);
64 container
= new Composite(getLayoutComposite(), style
);
65 container
.setLayoutData(CdmFormFactory
.FILL(2, 1));
67 container
.addControlListener(this);
68 container
.addPaintListener(this);
73 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
77 public void setSelected(boolean selected
) {
78 // TODO Auto-generated method stub
83 * <p>Setter for the field <code>imageUri</code>.</p>
85 * @param uri a {@link java.net.URI} object.
87 public void setImageUri(URI uri
) {
92 final Display display
= Display
.getCurrent();
93 Job job
= new Job("Loading image: " + imageUri
){
95 protected IStatus
run(IProgressMonitor monitor
) {
97 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
99 image
= new Image(Display
.getCurrent(), imageStream
);
101 display
.asyncExec(new Runnable(){
104 EditorUtil
.reflowDetailsViewer();
105 // getLayoutComposite().layout();
108 return Status
.OK_STATUS
;
109 }catch (Exception e
) {
110 EditorUtil
.errorDialog("Could not load image", getClass(), e
.getMessage(), e
);
112 return Status
.CANCEL_STATUS
;
120 private void calculateImageBounds(){
121 Rectangle imageBounds
= image
.getBounds();
122 Rectangle containerBounds
= container
.getBounds();
124 Integer imgWidth
= imageBounds
.width
;
125 Integer imgHeight
= imageBounds
.height
;
127 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
128 Integer width
= containerBounds
.width
;
129 Float height
= width
* ratio
;
131 imageMaxBounds
= new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
.intValue());
135 * <p>Getter for the field <code>imageUri</code>.</p>
137 * @return the imageUri
139 public URI
getImageUri() {
144 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
148 public void paintControl(PaintEvent e
) {
150 calculateImageBounds();
151 Composite composite
= (Composite
) e
.widget
;
152 TableWrapData layoutData
= CdmFormFactory
.FILL(2, 1);
153 layoutData
.heightHint
= imageMaxBounds
.height
;
154 composite
.setLayoutData(layoutData
);
155 e
.gc
.drawImage(image
, 0, 0, image
.getBounds().width
, image
.getBounds().height
, 0, 0, imageMaxBounds
.width
, imageMaxBounds
.height
);
161 * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
165 public void controlMoved(ControlEvent e
) {
166 // TODO Auto-generated method stub
172 * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
176 public void controlResized(ControlEvent e
) {
177 EditorUtil
.reflowDetailsViewer();