1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.forms;
|
12
|
|
13
|
import java.io.IOException;
|
14
|
import java.io.InputStream;
|
15
|
import java.net.URI;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.Map;
|
18
|
|
19
|
import org.apache.http.HttpEntity;
|
20
|
import org.apache.http.HttpException;
|
21
|
import org.apache.http.HttpResponse;
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.eclipse.core.runtime.IProgressMonitor;
|
24
|
import org.eclipse.core.runtime.IStatus;
|
25
|
import org.eclipse.core.runtime.Status;
|
26
|
import org.eclipse.core.runtime.jobs.Job;
|
27
|
import org.eclipse.swt.events.ControlEvent;
|
28
|
import org.eclipse.swt.events.ControlListener;
|
29
|
import org.eclipse.swt.events.PaintEvent;
|
30
|
import org.eclipse.swt.events.PaintListener;
|
31
|
import org.eclipse.swt.graphics.Image;
|
32
|
import org.eclipse.swt.graphics.Rectangle;
|
33
|
import org.eclipse.swt.widgets.Composite;
|
34
|
import org.eclipse.swt.widgets.Display;
|
35
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
36
|
|
37
|
import eu.etaxonomy.cdm.common.UriUtils;
|
38
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
39
|
|
40
|
/**
|
41
|
* <p>ImageElement class.</p>
|
42
|
*
|
43
|
* @author n.hoffmann
|
44
|
* @created Sep 24, 2010
|
45
|
* @version 1.0
|
46
|
*/
|
47
|
public class ImageElement extends AbstractCdmFormElement implements PaintListener, ControlListener{
|
48
|
|
49
|
private static final Logger logger = Logger.getLogger(ImageElement.class);
|
50
|
|
51
|
private URI imageUri;
|
52
|
private Image image;
|
53
|
private Rectangle imageMaxBounds;
|
54
|
|
55
|
private Composite container;
|
56
|
|
57
|
/**
|
58
|
* <p>Constructor for ImageElement.</p>
|
59
|
*
|
60
|
* @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
|
61
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
|
62
|
* @param imageUri a {@link java.net.URI} object.
|
63
|
* @param style a int.
|
64
|
*/
|
65
|
protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
|
66
|
super(formFactory, parentElement);
|
67
|
|
68
|
setImageUri(imageUri);
|
69
|
|
70
|
container = new Composite(getLayoutComposite(), style);
|
71
|
container.setLayoutData(CdmFormFactory.FILL(2, 1));
|
72
|
|
73
|
container.addControlListener(this);
|
74
|
container.addPaintListener(this);
|
75
|
}
|
76
|
|
77
|
|
78
|
/* (non-Javadoc)
|
79
|
* @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
|
80
|
*/
|
81
|
/** {@inheritDoc} */
|
82
|
@Override
|
83
|
public void setSelected(boolean selected) {
|
84
|
// TODO Auto-generated method stub
|
85
|
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* <p>Setter for the field <code>imageUri</code>.</p>
|
90
|
*
|
91
|
* @param uri a {@link java.net.URI} object.
|
92
|
*/
|
93
|
public void setImageUri(URI uri) {
|
94
|
this.imageUri = uri;
|
95
|
|
96
|
logger.warn("Setting image uri" + imageUri);
|
97
|
|
98
|
if(imageUri != null){
|
99
|
|
100
|
final Display display = Display.getCurrent();
|
101
|
Job job = new Job("Loading image: " + imageUri){
|
102
|
@Override
|
103
|
protected IStatus run(IProgressMonitor monitor) {
|
104
|
try {
|
105
|
InputStream imageStream = UriUtils.getInputStream(imageUri);
|
106
|
|
107
|
image = new Image(Display.getCurrent(), imageStream);
|
108
|
|
109
|
display.asyncExec(new Runnable(){
|
110
|
@Override
|
111
|
public void run() {
|
112
|
// EditorUtil.reflowDetailsViewer();
|
113
|
getLayoutComposite().layout();
|
114
|
}
|
115
|
});
|
116
|
return Status.OK_STATUS;
|
117
|
}catch (IOException e) {
|
118
|
logger.error("Error reading image", e);
|
119
|
} catch (HttpException e) {
|
120
|
// TODO Auto-generated catch block
|
121
|
e.printStackTrace();
|
122
|
}
|
123
|
return Status.CANCEL_STATUS;
|
124
|
}
|
125
|
};
|
126
|
|
127
|
job.schedule();
|
128
|
}
|
129
|
}
|
130
|
|
131
|
private void calculateImageBounds(){
|
132
|
Rectangle imageBounds = image.getBounds();
|
133
|
Rectangle containerBounds = container.getBounds();
|
134
|
|
135
|
Integer imgWidth = imageBounds.width;
|
136
|
Integer imgHeight = imageBounds.height;
|
137
|
|
138
|
Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
|
139
|
Integer width = containerBounds.width;
|
140
|
Float height = width * ratio;
|
141
|
|
142
|
imageMaxBounds = new Rectangle(containerBounds.x, containerBounds.y, width, height.intValue());
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* <p>Getter for the field <code>imageUri</code>.</p>
|
147
|
*
|
148
|
* @return the imageUri
|
149
|
*/
|
150
|
public URI getImageUri() {
|
151
|
return imageUri;
|
152
|
}
|
153
|
|
154
|
/* (non-Javadoc)
|
155
|
* @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
|
156
|
*/
|
157
|
/** {@inheritDoc} */
|
158
|
@Override
|
159
|
public void paintControl(PaintEvent e) {
|
160
|
if(image != null){
|
161
|
calculateImageBounds();
|
162
|
Composite composite = (Composite) e.widget;
|
163
|
TableWrapData layoutData = CdmFormFactory.FILL(2, 1);
|
164
|
layoutData.heightHint = imageMaxBounds.height;
|
165
|
composite.setLayoutData(layoutData);
|
166
|
e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
|
167
|
}
|
168
|
}
|
169
|
|
170
|
|
171
|
/* (non-Javadoc)
|
172
|
* @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
|
173
|
*/
|
174
|
/** {@inheritDoc} */
|
175
|
@Override
|
176
|
public void controlMoved(ControlEvent e) {
|
177
|
// TODO Auto-generated method stub
|
178
|
|
179
|
}
|
180
|
|
181
|
|
182
|
/* (non-Javadoc)
|
183
|
* @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
|
184
|
*/
|
185
|
/** {@inheritDoc} */
|
186
|
@Override
|
187
|
public void controlResized(ControlEvent e) {
|
188
|
// EditorUtil.reflowDetailsViewer();
|
189
|
}
|
190
|
}
|