92ab62f7f7b61e0d6a505bd9aa7beba3fd552549
[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 * @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 protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
50 super(formFactory, parentElement);
51
52 container = new Composite(getLayoutComposite(), style);
53 container.setLayoutData(LayoutConstants.FILL(2, 1));
54
55 container.addPaintListener(this);
56 }
57
58 public void initImageUri(URI uri) throws IOException, HttpException {
59 this.imageUri = uri;
60 InputStream imageStream = UriUtils.getInputStream(imageUri);
61 image = new Image(Display.getCurrent(), imageStream);
62 }
63
64
65 public URI getImageUri() {
66 return imageUri;
67 }
68
69 public void loadImage(){
70 loadImage(null);
71 }
72
73 public void loadImage(final Runnable postRunnable){
74 if(getImageUri() != null){
75 Job job = new Job("Loading image") {
76
77 @Override
78 protected IStatus run(IProgressMonitor monitor) {
79 IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
80 try {
81 runnable.run(monitor);
82 } catch (Exception e) {
83 MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + getImageUri(), e);
84 }
85
86 return Status.OK_STATUS;
87 }
88 };
89 job.schedule();
90 }
91 }
92
93 public void unloadImage(final Runnable postRunnable) {
94 Job job = new Job("Unloading image") {
95
96 @Override
97 protected IStatus run(IProgressMonitor monitor) {
98 IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
99 try {
100 runnable.run(monitor);
101 } catch (Exception e) {
102 MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage() + ": " + getImageUri(), e);
103 }
104
105 return Status.OK_STATUS;
106 }
107 };
108 job.schedule();
109
110 }
111
112 public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
113
114 final Display display = getLayoutComposite().getDisplay();
115
116 IRunnableWithProgress runnable = new IRunnableWithProgress(){
117
118 @Override
119 public void run(IProgressMonitor monitor) {
120 monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
121
122 // redraw the image container
123 display.asyncExec(new Runnable(){
124 @Override
125 public void run() {
126 if(! getLayoutComposite().isDisposed()){
127 Event untypedEvent = new Event();
128 untypedEvent.widget = container;
129 PaintEvent event = new PaintEvent(untypedEvent);
130 event.gc = new GC(container);
131 paintControl(event);
132 getLayoutComposite().layout();
133 }
134 }
135 });
136
137 // execute the external runnable
138 if(postRunnable != null){
139 display.asyncExec(postRunnable);
140 }
141 monitor.done();
142 }
143
144 };
145
146 return runnable;
147 }
148
149 private Rectangle calculateImageBounds(Image image, Control control){
150 Rectangle imageBounds = image.getBounds();
151 Rectangle containerBounds = control.getBounds();
152
153 Integer imgWidth = imageBounds.width;
154 Integer imgHeight = imageBounds.height;
155
156 Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
157 Integer width = containerBounds.width;
158 Integer height = ((Float) (width * ratio)).intValue();
159
160 return new Rectangle(containerBounds.x, containerBounds.y, width, height);
161 }
162
163 /** {@inheritDoc} */
164 @Override
165 public void paintControl(PaintEvent e) {
166 TableWrapData layoutData = LayoutConstants.FILL(2, 1);
167 Control control = (Control) e.widget;
168 if(image != null){
169 Rectangle imageMaxBounds = calculateImageBounds(image, control);
170 layoutData.heightHint = imageMaxBounds.height;
171 e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
172 }else{
173 layoutData.heightHint = 10;
174 e.gc.drawRectangle(0, 0, 0, 10);
175 }
176 control.setLayoutData(layoutData);
177 e.gc.dispose();
178 }
179
180 }