1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.ui.element;
|
11
|
|
12
|
import java.io.IOException;
|
13
|
import java.io.InputStream;
|
14
|
import java.net.URI;
|
15
|
|
16
|
import org.apache.http.HttpException;
|
17
|
import org.eclipse.core.runtime.IProgressMonitor;
|
18
|
import org.eclipse.core.runtime.IStatus;
|
19
|
import org.eclipse.core.runtime.Status;
|
20
|
import org.eclipse.core.runtime.jobs.Job;
|
21
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
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.Control;
|
29
|
import org.eclipse.swt.widgets.Display;
|
30
|
import org.eclipse.swt.widgets.Event;
|
31
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
32
|
|
33
|
import eu.etaxonomy.cdm.common.UriUtils;
|
34
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
35
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
36
|
|
37
|
/**
|
38
|
* @author n.hoffmann
|
39
|
* @created Sep 24, 2010
|
40
|
* @version 1.0
|
41
|
*/
|
42
|
public class ImageElement extends AbstractCdmFormElement implements PaintListener{
|
43
|
|
44
|
private URI imageUri;
|
45
|
private Image image;
|
46
|
|
47
|
private Composite container;
|
48
|
|
49
|
private final Runnable postRunnable = new Runnable(){
|
50
|
@Override
|
51
|
public void run() {
|
52
|
AbstractUtility.reflowDetailsViewer();
|
53
|
AbstractUtility.reflowSupplementalViewer();
|
54
|
}
|
55
|
};
|
56
|
|
57
|
protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
|
58
|
super(formFactory, parentElement);
|
59
|
|
60
|
container = new Composite(getLayoutComposite(), style);
|
61
|
container.setLayoutData(LayoutConstants.FILL(2, 1));
|
62
|
|
63
|
container.addPaintListener(this);
|
64
|
}
|
65
|
|
66
|
public void initImageUri(URI uri) throws IOException, HttpException {
|
67
|
this.imageUri = uri;
|
68
|
InputStream imageStream = UriUtils.getInputStream(imageUri);
|
69
|
image = new Image(Display.getCurrent(), imageStream);
|
70
|
}
|
71
|
|
72
|
|
73
|
public URI getImageUri() {
|
74
|
return imageUri;
|
75
|
}
|
76
|
|
77
|
public void loadImage(){
|
78
|
if(getImageUri() != null){
|
79
|
Job job = new Job("Loading image") {
|
80
|
|
81
|
@Override
|
82
|
protected IStatus run(IProgressMonitor monitor) {
|
83
|
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
|
84
|
try {
|
85
|
runnable.run(monitor);
|
86
|
} catch (Exception e) {
|
87
|
MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + getImageUri(), e);
|
88
|
}
|
89
|
|
90
|
return Status.OK_STATUS;
|
91
|
}
|
92
|
};
|
93
|
job.schedule();
|
94
|
}
|
95
|
}
|
96
|
|
97
|
public void unloadImage() {
|
98
|
Job job = new Job("Unloading image") {
|
99
|
|
100
|
@Override
|
101
|
protected IStatus run(IProgressMonitor monitor) {
|
102
|
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
|
103
|
try {
|
104
|
runnable.run(monitor);
|
105
|
} catch (Exception e) {
|
106
|
MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage() + ": " + getImageUri(), e);
|
107
|
}
|
108
|
|
109
|
return Status.OK_STATUS;
|
110
|
}
|
111
|
};
|
112
|
job.schedule();
|
113
|
|
114
|
}
|
115
|
|
116
|
public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
|
117
|
|
118
|
final Display display = getLayoutComposite().getDisplay();
|
119
|
|
120
|
IRunnableWithProgress runnable = new IRunnableWithProgress(){
|
121
|
|
122
|
@Override
|
123
|
public void run(IProgressMonitor monitor) {
|
124
|
monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
|
125
|
|
126
|
// redraw the image container
|
127
|
display.asyncExec(new Runnable(){
|
128
|
@Override
|
129
|
public void run() {
|
130
|
if(! getLayoutComposite().isDisposed() && container!=null){
|
131
|
Event untypedEvent = new Event();
|
132
|
untypedEvent.widget = container;
|
133
|
PaintEvent event = new PaintEvent(untypedEvent);
|
134
|
event.gc = new GC(container);
|
135
|
paintControl(event);
|
136
|
getLayoutComposite().layout();
|
137
|
}
|
138
|
}
|
139
|
});
|
140
|
|
141
|
// execute the external runnable
|
142
|
if(postRunnable != null){
|
143
|
display.asyncExec(postRunnable);
|
144
|
}
|
145
|
monitor.done();
|
146
|
}
|
147
|
|
148
|
};
|
149
|
|
150
|
return runnable;
|
151
|
}
|
152
|
|
153
|
private Rectangle calculateImageBounds(Image image, Control control){
|
154
|
Rectangle imageBounds = image.getBounds();
|
155
|
Rectangle containerBounds = control.getBounds();
|
156
|
|
157
|
Integer imgWidth = imageBounds.width;
|
158
|
Integer imgHeight = imageBounds.height;
|
159
|
|
160
|
Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
|
161
|
Integer width = containerBounds.width;
|
162
|
Integer height = ((Float) (width * ratio)).intValue();
|
163
|
|
164
|
return new Rectangle(containerBounds.x, containerBounds.y, width, height);
|
165
|
}
|
166
|
|
167
|
public void dispose(){
|
168
|
if(image!=null){
|
169
|
image.dispose();
|
170
|
image = null;
|
171
|
}
|
172
|
imageUri = null;
|
173
|
if(container!=null){
|
174
|
container.dispose();
|
175
|
container = null;
|
176
|
}
|
177
|
}
|
178
|
|
179
|
/** {@inheritDoc} */
|
180
|
@Override
|
181
|
public void paintControl(PaintEvent e) {
|
182
|
TableWrapData layoutData = LayoutConstants.FILL(2, 1);
|
183
|
Control control = (Control) e.widget;
|
184
|
if(image != null){
|
185
|
Rectangle imageMaxBounds = calculateImageBounds(image, control);
|
186
|
layoutData.heightHint = imageMaxBounds.height;
|
187
|
e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
|
188
|
}else{
|
189
|
layoutData.heightHint = 10;
|
190
|
e.gc.drawRectangle(0, 0, 0, 10);
|
191
|
}
|
192
|
control.setLayoutData(layoutData);
|
193
|
e.gc.dispose();
|
194
|
}
|
195
|
|
196
|
}
|