Project

General

Profile

Download (5.34 KB) Statistics
| Branch: | Tag: | Revision:
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.AbstractUtility;
36
import eu.etaxonomy.taxeditor.model.MessagingUtils;
37

    
38
/**
39
 * @author n.hoffmann
40
 * @created Sep 24, 2010
41
 * @version 1.0
42
 */
43
public class ImageElement extends AbstractCdmFormElement implements PaintListener{
44

    
45
	private URI imageUri;
46
	private Image image;
47

    
48
	private Composite container;
49

    
50
	private final Runnable postRunnable = new Runnable(){
51
        @Override
52
        public void run() {
53
            AbstractUtility.reflowDetailsViewer();
54
            AbstractUtility.reflowSupplementalViewer();
55
        }
56
    };
57

    
58
	protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
59
		super(formFactory, parentElement);
60

    
61
		container = new Composite(getLayoutComposite(), style);
62
		container.setLayoutData(LayoutConstants.FILL(2, 1));
63

    
64
		container.addPaintListener(this);
65
	}
66

    
67
	public void initImageUri(URI uri) throws IOException, HttpException {
68
		this.imageUri = uri;
69
		InputStream imageStream = UriUtils.getInputStream(imageUri);
70
		image = new Image(Display.getCurrent(), imageStream);
71
	}
72

    
73

    
74
	public URI getImageUri() {
75
		return imageUri;
76
	}
77

    
78
	public void loadImage(){
79
		if(getImageUri() != null){
80
			Job job = new Job("Loading image") {
81

    
82
				@Override
83
				protected IStatus run(IProgressMonitor monitor) {
84
					IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
85
					try {
86
						runnable.run(monitor);
87
					} catch (Exception e) {
88
						MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
89
					}
90

    
91
					return Status.OK_STATUS;
92
				}
93
			};
94
			job.schedule();
95
		}
96
	}
97

    
98
	public void unloadImage() {
99
		Job job = new Job("Unloading 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 unload image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
108
				}
109

    
110
				return Status.OK_STATUS;
111
			}
112
		};
113
		job.schedule();
114

    
115
	}
116

    
117
	public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
118

    
119
		final Display display = getLayoutComposite().getDisplay();
120

    
121
		IRunnableWithProgress runnable = new IRunnableWithProgress(){
122

    
123
			@Override
124
			public void run(IProgressMonitor monitor) {
125
				monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
126

    
127
				// redraw the image container
128
				display.asyncExec(new Runnable(){
129
					@Override
130
					public void run() {
131
						if(! getLayoutComposite().isDisposed() && container!=null){
132
							Event untypedEvent = new Event();
133
							untypedEvent.widget = container;
134
							PaintEvent event = new PaintEvent(untypedEvent);
135
							event.gc = new GC(container);
136
							paintControl(event);
137
							getLayoutComposite().layout();
138
						}
139
					}
140
				});
141

    
142
				// execute the external runnable
143
				if(postRunnable != null){
144
					display.asyncExec(postRunnable);
145
				}
146
				monitor.done();
147
			}
148

    
149
		};
150

    
151
		return runnable;
152
	}
153

    
154
	private Rectangle calculateImageBounds(Image image, Control control){
155
		Rectangle imageBounds = image.getBounds();
156
		Rectangle containerBounds = control.getBounds();
157

    
158
		Integer imgWidth = imageBounds.width;
159
		Integer imgHeight = imageBounds.height;
160

    
161
		Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
162
		Integer width = containerBounds.width;
163
		Integer height = ((Float) (width * ratio)).intValue();
164

    
165
		return new Rectangle(containerBounds.x, containerBounds.y, width, height);
166
	}
167

    
168
	public void dispose(){
169
	    if(image!=null){
170
	        image.dispose();
171
	        image = null;
172
	    }
173
	    imageUri = null;
174
	    if(container!=null){
175
	        container.dispose();
176
	        container = null;
177
	    }
178
	}
179

    
180
	/** {@inheritDoc} */
181
	@Override
182
	public void paintControl(PaintEvent e) {
183
		TableWrapData layoutData = LayoutConstants.FILL(2, 1);
184
		Control control = (Control) e.widget;
185
		if(image != null){
186
			Rectangle imageMaxBounds = calculateImageBounds(image, control);
187
			layoutData.heightHint = imageMaxBounds.height;
188
			e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
189
		}else{
190
			layoutData.heightHint = 10;
191
			e.gc.drawRectangle(0, 0, 0, 10);
192
		}
193
		control.setLayoutData(layoutData);
194
		e.gc.dispose();
195
	}
196

    
197
}
(23-23/44)