Moved all logging and dialog functionality to the new class MessagingUtils.
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / ImageElement.java
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.ui.element;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.URI;
16
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.jface.operation.IRunnableWithProgress;
23 import org.eclipse.swt.events.PaintEvent;
24 import org.eclipse.swt.events.PaintListener;
25 import org.eclipse.swt.graphics.GC;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Event;
32 import org.eclipse.ui.forms.widgets.TableWrapData;
33
34 import eu.etaxonomy.cdm.common.UriUtils;
35 import eu.etaxonomy.taxeditor.model.MessagingUtils;
36
37 /**
38 * <p>ImageElement class.</p>
39 *
40 * @author n.hoffmann
41 * @created Sep 24, 2010
42 * @version 1.0
43 */
44 public class ImageElement extends AbstractCdmFormElement implements PaintListener{
45
46 private URI imageUri;
47 private Image image;
48
49 private Composite container;
50
51 /**
52 * <p>Constructor for ImageElement.</p>
53 *
54 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
56 * @param imageUri a {@link java.net.URI} object.
57 * @param style a int.
58 */
59 protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
60 super(formFactory, parentElement);
61
62 container = new Composite(getLayoutComposite(), style);
63 container.setLayoutData(LayoutConstants.FILL(2, 1));
64
65 container.addPaintListener(this);
66 }
67
68 /**
69 * <p>Setter for the field <code>imageUri</code>.</p>
70 *
71 * @param uri a {@link java.net.URI} object.
72 * @throws HttpException
73 * @throws IOException
74 */
75 public void initImageUri(URI uri) throws IOException, HttpException {
76 this.imageUri = uri;
77 InputStream imageStream = UriUtils.getInputStream(imageUri);
78 image = new Image(Display.getCurrent(), imageStream);
79 }
80
81
82 /**
83 * <p>Getter for the field <code>imageUri</code>.</p>
84 *
85 * @return the imageUri
86 */
87 public URI getImageUri() {
88 return imageUri;
89 }
90
91 public void loadImage(){
92 loadImage(null);
93 }
94
95
96
97 public void loadImage(final Runnable postRunnable){
98 if(getImageUri() != null){
99 Job job = new Job("Loading image") {
100
101 @Override
102 protected IStatus run(IProgressMonitor monitor) {
103 IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
104 try {
105 runnable.run(monitor);
106 } catch (Exception e) {
107 MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + getImageUri(), e);
108 }
109
110 return Status.OK_STATUS;
111 }
112 };
113 job.schedule();
114 }
115 }
116
117
118
119 public void unloadImage(final Runnable postRunnable) {
120 Job job = new Job("Unloading image") {
121
122 @Override
123 protected IStatus run(IProgressMonitor monitor) {
124 IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
125 try {
126 runnable.run(monitor);
127 } catch (Exception e) {
128 MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage() + ": " + getImageUri(), e);
129 }
130
131 return Status.OK_STATUS;
132 }
133 };
134 job.schedule();
135
136 }
137
138 public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
139
140 final Display display = getLayoutComposite().getDisplay();
141
142 IRunnableWithProgress runnable = new IRunnableWithProgress(){
143
144 @Override
145 public void run(IProgressMonitor monitor) {
146 monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
147
148 // redraw the image container
149 display.asyncExec(new Runnable(){
150 @Override
151 public void run() {
152 if(! getLayoutComposite().isDisposed()){
153 Event untypedEvent = new Event();
154 untypedEvent.widget = container;
155 PaintEvent event = new PaintEvent(untypedEvent);
156 event.gc = new GC(container);
157 paintControl(event);
158 getLayoutComposite().layout();
159 }
160 }
161 });
162
163 // execute the external runnable
164 if(postRunnable != null){
165 display.asyncExec(postRunnable);
166 }
167 monitor.done();
168 }
169
170 };
171
172 return runnable;
173 }
174
175 private Rectangle calculateImageBounds(Image image, Control control){
176 Rectangle imageBounds = image.getBounds();
177 Rectangle containerBounds = control.getBounds();
178
179 Integer imgWidth = imageBounds.width;
180 Integer imgHeight = imageBounds.height;
181
182 Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
183 Integer width = containerBounds.width;
184 Integer height = ((Float) (width * ratio)).intValue();
185
186 return new Rectangle(containerBounds.x, containerBounds.y, width, height);
187 }
188
189
190 /* (non-Javadoc)
191 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
192 */
193 /** {@inheritDoc} */
194 @Override
195 public void paintControl(PaintEvent e) {
196 TableWrapData layoutData = LayoutConstants.FILL(2, 1);
197 Control control = (Control) e.widget;
198 if(image != null){
199 Rectangle imageMaxBounds = calculateImageBounds(image, control);
200 layoutData.heightHint = imageMaxBounds.height;
201 e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
202 }else{
203 layoutData.heightHint = 10;
204 e.gc.drawRectangle(0, 0, 0, 10);
205 }
206 control.setLayoutData(layoutData);
207 e.gc.dispose();
208 }
209
210 }