Project

General

Profile

Download (5.86 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.forms;
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.store.StoreUtil;
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.forms.CdmFormFactory} object.
55
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.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(CdmFormFactory.FILL(2, 1));
64
		
65
		container.addPaintListener(this);
66
	}
67
	
68

    
69
	/* (non-Javadoc)
70
	 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
71
	 */
72
	/** {@inheritDoc} */
73
	@Override
74
	public void setSelected(boolean selected) {
75

    
76
	}
77

    
78
	/**
79
	 * <p>Setter for the field <code>imageUri</code>.</p>
80
	 *
81
	 * @param uri a {@link java.net.URI} object.
82
	 * @throws HttpException 
83
	 * @throws IOException 
84
	 */
85
	public void initImageUri(URI uri) throws IOException, HttpException {
86
		this.imageUri = uri;
87
		InputStream imageStream = UriUtils.getInputStream(imageUri);
88
		image = new Image(Display.getCurrent(), imageStream);
89
	}
90
	
91
	
92
	/**
93
	 * <p>Getter for the field <code>imageUri</code>.</p>
94
	 *
95
	 * @return the imageUri
96
	 */
97
	public URI getImageUri() {
98
		return imageUri;
99
	}
100
	
101
	public void loadImage(){
102
		loadImage(null);
103
	}
104
	
105
	
106
	
107
	public void loadImage(final Runnable postRunnable){
108
		if(getImageUri() != null){
109
			Job job = new Job("Loading image") {
110
				
111
				@Override
112
				protected IStatus run(IProgressMonitor monitor) {
113
					IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
114
					try {
115
						runnable.run(monitor);
116
					} catch (Exception e) {
117
						StoreUtil.errorDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
118
					}
119
					
120
					return Status.OK_STATUS;
121
				}
122
			};
123
			job.schedule();
124
		}
125
	}
126
	
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();
145
		
146
	}
147
	
148
	public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
149
			
150
		final Display display = getLayoutComposite().getDisplay(); 
151
		
152
		IRunnableWithProgress runnable = new IRunnableWithProgress(){
153
			
154
			@Override
155
			public void run(IProgressMonitor monitor) {
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();
169
						}
170
					}
171
				});
172
				
173
				// execute the external runnable
174
				if(postRunnable != null){
175
					display.asyncExec(postRunnable);
176
				}
177
				monitor.done();		
178
			}
179

    
180
		};
181
		
182
		return runnable;
183
	}
184
	
185
	private Rectangle calculateImageBounds(Image image, Control control){
186
		Rectangle imageBounds = image.getBounds();
187
		Rectangle containerBounds = control.getBounds();
188
		
189
		Integer imgWidth = imageBounds.width;
190
		Integer imgHeight = imageBounds.height;
191
		
192
		Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
193
		Integer width = containerBounds.width;
194
		Integer height = ((Float) (width * ratio)).intValue();
195
		
196
		return new Rectangle(containerBounds.x, containerBounds.y, width, height);
197
	}
198

    
199

    
200
	/* (non-Javadoc)
201
	 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
202
	 */
203
	/** {@inheritDoc} */
204
	@Override
205
	public void paintControl(PaintEvent e) {
206
		TableWrapData layoutData = CdmFormFactory.FILL(2, 1);
207
		Control control = (Control) e.widget;
208
		if(image != null){
209
			Rectangle imageMaxBounds = calculateImageBounds(image, control);
210
			layoutData.heightHint = imageMaxBounds.height;
211
			e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
212
		}else{
213
			layoutData.heightHint = 10;
214
			e.gc.drawRectangle(0, 0, 0, 10);
215
		}
216
		control.setLayoutData(layoutData);
217
		e.gc.dispose();
218
	}
219

    
220
}
(20-20/35)