Revision 59351073
Added by Niels Hoffmann almost 13 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/forms/ImageElement.java | ||
---|---|---|
10 | 10 |
|
11 | 11 |
package eu.etaxonomy.taxeditor.ui.forms; |
12 | 12 |
|
13 |
import java.io.IOException; |
|
13 | 14 |
import java.io.InputStream; |
14 | 15 |
import java.net.URI; |
15 | 16 |
|
17 |
import org.apache.http.HttpException; |
|
16 | 18 |
import org.eclipse.core.runtime.IProgressMonitor; |
17 | 19 |
import org.eclipse.core.runtime.IStatus; |
18 | 20 |
import org.eclipse.core.runtime.Status; |
19 | 21 |
import org.eclipse.core.runtime.jobs.Job; |
20 | 22 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
21 |
import org.eclipse.swt.SWT; |
|
22 | 23 |
import org.eclipse.swt.events.PaintEvent; |
23 | 24 |
import org.eclipse.swt.events.PaintListener; |
24 | 25 |
import org.eclipse.swt.graphics.GC; |
... | ... | |
27 | 28 |
import org.eclipse.swt.widgets.Composite; |
28 | 29 |
import org.eclipse.swt.widgets.Display; |
29 | 30 |
import org.eclipse.swt.widgets.Event; |
30 |
import org.eclipse.swt.widgets.Label; |
|
31 | 31 |
import org.eclipse.ui.forms.widgets.TableWrapData; |
32 | 32 |
|
33 | 33 |
import eu.etaxonomy.cdm.common.UriUtils; |
... | ... | |
58 | 58 |
*/ |
59 | 59 |
protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) { |
60 | 60 |
super(formFactory, parentElement); |
61 |
|
|
62 |
setImageUri(imageUri); |
|
63 |
loadImage(); |
|
64 |
|
|
61 |
|
|
65 | 62 |
container = new Composite(getLayoutComposite(), style); |
66 | 63 |
container.setLayoutData(CdmFormFactory.FILL(2, 1)); |
67 | 64 |
|
... | ... | |
82 | 79 |
* <p>Setter for the field <code>imageUri</code>.</p> |
83 | 80 |
* |
84 | 81 |
* @param uri a {@link java.net.URI} object. |
82 |
* @throws HttpException |
|
83 |
* @throws IOException |
|
85 | 84 |
*/ |
86 |
public void setImageUri(URI uri) {
|
|
85 |
public void initImageUri(URI uri) throws IOException, HttpException {
|
|
87 | 86 |
this.imageUri = uri; |
87 |
InputStream imageStream = UriUtils.getInputStream(imageUri); |
|
88 |
image = new Image(Display.getCurrent(), imageStream); |
|
88 | 89 |
} |
89 | 90 |
|
90 | 91 |
|
... | ... | |
101 | 102 |
loadImage(null); |
102 | 103 |
} |
103 | 104 |
|
105 |
|
|
106 |
|
|
104 | 107 |
public void loadImage(final Runnable postRunnable){ |
105 | 108 |
if(getImageUri() != null){ |
106 | 109 |
Job job = new Job("Loading image") { |
... | ... | |
121 | 124 |
} |
122 | 125 |
} |
123 | 126 |
|
124 |
public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){ |
|
127 |
|
|
128 |
|
|
129 |
public void unloadImage(final Runnable postRunnable) { |
|
130 |
Job job = new Job("Unloading image") { |
|
131 |
|
|
132 |
@Override |
|
133 |
protected IStatus run(IProgressMonitor monitor) { |
|
134 |
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable); |
|
135 |
try { |
|
136 |
runnable.run(monitor); |
|
137 |
} catch (Exception e) { |
|
138 |
StoreUtil.errorDialog("Could not unload image", getClass(), e.getMessage() + ": " + getImageUri(), e); |
|
139 |
} |
|
140 |
|
|
141 |
return Status.OK_STATUS; |
|
142 |
} |
|
143 |
}; |
|
144 |
job.schedule(); |
|
125 | 145 |
|
146 |
} |
|
147 |
|
|
148 |
public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){ |
|
149 |
|
|
126 | 150 |
final Display display = getLayoutComposite().getDisplay(); |
127 | 151 |
|
128 | 152 |
IRunnableWithProgress runnable = new IRunnableWithProgress(){ |
129 | 153 |
|
130 | 154 |
@Override |
131 | 155 |
public void run(IProgressMonitor monitor) { |
132 |
try { |
|
133 |
monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN); |
|
134 |
InputStream imageStream = UriUtils.getInputStream(imageUri); |
|
135 |
|
|
136 |
image = new Image(Display.getCurrent(), imageStream); |
|
137 |
|
|
138 |
// redraw the image container |
|
139 |
display.asyncExec(new Runnable(){ |
|
140 |
@Override |
|
141 |
public void run() { |
|
142 |
if(! getLayoutComposite().isDisposed()){ |
|
143 |
Event untypedEvent = new Event(); |
|
144 |
untypedEvent.widget = container; |
|
145 |
PaintEvent event = new PaintEvent(untypedEvent); |
|
146 |
event.gc = new GC(container); |
|
147 |
paintControl(event); |
|
148 |
getLayoutComposite().layout(); |
|
149 |
} |
|
156 |
monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN); |
|
157 |
|
|
158 |
// redraw the image container |
|
159 |
display.asyncExec(new Runnable(){ |
|
160 |
@Override |
|
161 |
public void run() { |
|
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); |
|
167 |
paintControl(event); |
|
168 |
getLayoutComposite().layout(); |
|
150 | 169 |
} |
151 |
}); |
|
152 |
|
|
153 |
// execute the external runnable |
|
154 |
if(postRunnable != null){ |
|
155 |
display.asyncExec(postRunnable); |
|
156 | 170 |
} |
157 |
monitor.done(); |
|
158 |
}catch (Exception e) { |
|
159 |
StoreUtil.warn(getClass(), e.getMessage()); |
|
160 |
} |
|
161 |
} |
|
171 |
}); |
|
172 |
|
|
173 |
// execute the external runnable |
|
174 |
if(postRunnable != null){ |
|
175 |
display.asyncExec(postRunnable); |
|
176 |
} |
|
177 |
monitor.done(); |
|
178 |
} |
|
179 |
|
|
162 | 180 |
}; |
163 | 181 |
|
164 | 182 |
return runnable; |
165 | 183 |
} |
166 |
|
|
184 |
|
|
167 | 185 |
private void calculateImageBounds(){ |
168 | 186 |
Rectangle imageBounds = image.getBounds(); |
169 | 187 |
Rectangle containerBounds = container.getBounds(); |
... | ... | |
185 | 203 |
/** {@inheritDoc} */ |
186 | 204 |
@Override |
187 | 205 |
public void paintControl(PaintEvent e) { |
206 |
TableWrapData layoutData = CdmFormFactory.FILL(2, 1); |
|
207 |
Composite composite = (Composite) e.widget; |
|
188 | 208 |
if(image != null){ |
189 | 209 |
calculateImageBounds(); |
190 |
Composite composite = (Composite) e.widget; |
|
191 |
TableWrapData layoutData = CdmFormFactory.FILL(2, 1); |
|
192 | 210 |
layoutData.heightHint = imageMaxBounds.height; |
193 |
composite.setLayoutData(layoutData); |
|
194 | 211 |
e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height); |
195 |
e.gc.dispose(); |
|
196 |
} |
|
212 |
}else{ |
|
213 |
layoutData.heightHint = 10; |
|
214 |
e.gc.drawRectangle(0, 0, 0, 10); |
|
215 |
} |
|
216 |
composite.setLayoutData(layoutData); |
|
217 |
e.gc.dispose(); |
|
197 | 218 |
} |
219 |
|
|
198 | 220 |
} |
Also available in: Unified diff
Improved image handling (fixes #2351); Passwords can now be changed by "admin" user (CAUTION: this is done by checking if the currently logged in user is the user named "admin", this has to change once rights are implemented correctly)