Project

General

Profile

Download (6.32 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.Canvas;
29
import org.eclipse.swt.widgets.Composite;
30
import org.eclipse.swt.widgets.Control;
31
import org.eclipse.swt.widgets.Display;
32
import org.eclipse.swt.widgets.Event;
33
import org.eclipse.ui.forms.widgets.TableWrapData;
34

    
35
import eu.etaxonomy.cdm.common.UriUtils;
36
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.swt.widgets.DisplayProxy;
37
import eu.etaxonomy.taxeditor.singlesource.ui.forms.CdmFormFactoryFacade;
38
import eu.etaxonomy.taxeditor.store.StoreUtil;
39

    
40
/**
41
 * <p>ImageElement class.</p>
42
 *
43
 * @author n.hoffmann
44
 * @created Sep 24, 2010
45
 * @version 1.0
46
 */
47
public class ImageElement extends AbstractCdmFormElement implements PaintListener{
48

    
49
	private URI imageUri;
50
	private Image image;
51

    
52
/** RAPSS:Modify Must be Canvas in order to support GC drawing in RAP
53
 *	private Composite container;
54
 */
55
	private Canvas container;
56
	/**
57
	 * <p>Constructor for ImageElement.</p>
58
	 *
59
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.singlesource.ui.forms.CdmFormFactoryFacade} object.
60
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
61
	 * @param imageUri a {@link java.net.URI} object.
62
	 * @param style a int.
63
	 */
64
	public ImageElement(CdmFormFactoryFacade formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
65
		super(formFactory, parentElement);
66
			
67
		/** RAPSS:Modify Must be Canvas in order to support GC drawing in RAP
68
		container = new Composite(getLayoutComposite(), style);
69
		*/
70
		container = new Canvas(getLayoutComposite(), style);
71
		container.setLayoutData(CdmFormFactoryFacade.FILL(2, 1));
72
		
73
		container.addPaintListener(this);
74
	}
75
	
76

    
77
	/* (non-Javadoc)
78
	 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
79
	 */
80
	/** {@inheritDoc} */
81
	@Override
82
	public void setSelected(boolean selected) {
83

    
84
	}
85

    
86
	/**
87
	 * <p>Setter for the field <code>imageUri</code>.</p>
88
	 *
89
	 * @param uri a {@link java.net.URI} object.
90
	 * @throws HttpException 
91
	 * @throws IOException 
92
	 */
93
	public void initImageUri(URI uri) throws IOException, HttpException {
94
		this.imageUri = uri;
95
		InputStream imageStream = UriUtils.getInputStream(imageUri);
96
		image = new Image(DisplayProxy.getDefault(), imageStream);
97
	}
98
	
99
	
100
	/**
101
	 * <p>Getter for the field <code>imageUri</code>.</p>
102
	 *
103
	 * @return the imageUri
104
	 */
105
	public URI getImageUri() {
106
		return imageUri;
107
	}
108
	
109
	public void loadImage(){
110
		loadImage(null);
111
	}
112
	
113
	
114
	
115
	public void loadImage(final Runnable postRunnable){
116
		if(getImageUri() != null){
117
			Job job = new Job("Loading image") {
118
				
119
				@Override
120
				protected IStatus run(IProgressMonitor monitor) {
121
					IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
122
					try {
123
						runnable.run(monitor);
124
					} catch (Exception e) {
125
						StoreUtil.errorDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
126
					}
127
					
128
					return Status.OK_STATUS;
129
				}
130
			};
131
			job.schedule();
132
		}
133
	}
134
	
135

    
136

    
137
	public void unloadImage(final Runnable postRunnable) {
138
		Job job = new Job("Unloading image") {
139
			
140
			@Override
141
			protected IStatus run(IProgressMonitor monitor) {
142
				IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
143
				try {
144
					runnable.run(monitor);
145
				} catch (Exception e) {
146
					StoreUtil.errorDialog("Could not unload image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
147
				}
148
				
149
				return Status.OK_STATUS;
150
			}
151
		};
152
		job.schedule();
153
		
154
	}
155
	
156
	public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
157
			
158
		final Display display = getLayoutComposite().getDisplay(); 
159
		
160
		IRunnableWithProgress runnable = new IRunnableWithProgress(){
161
			
162
			@Override
163
			public void run(IProgressMonitor monitor) {
164
				monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
165
								
166
				// redraw the image container
167
				display.asyncExec(new Runnable(){
168
					@Override
169
					public void run() {
170
						if(! getLayoutComposite().isDisposed()){
171
							Event untypedEvent = new Event();
172
							untypedEvent.widget = container;
173
							PaintEvent event = new PaintEvent(untypedEvent);
174
							event.gc = new GC(container);
175
							paintControl(event);
176
							getLayoutComposite().layout();
177
						}
178
					}
179
				});
180
				
181
				// execute the external runnable
182
				if(postRunnable != null){
183
					display.asyncExec(postRunnable);
184
				}
185
				monitor.done();		
186
			}
187

    
188
		};
189
		
190
		return runnable;
191
	}
192
	
193
	private Rectangle calculateImageBounds(Image image, Control control){
194
		Rectangle imageBounds = image.getBounds();
195
		Rectangle containerBounds = control.getBounds();
196
		
197
		Integer imgWidth = imageBounds.width;
198
		Integer imgHeight = imageBounds.height;
199
		
200
		Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
201
		Integer width = containerBounds.width;
202
		Integer height = ((Float) (width * ratio)).intValue();
203
		
204
		return new Rectangle(containerBounds.x, containerBounds.y, width, height);
205
	}
206

    
207

    
208
	/* (non-Javadoc)
209
	 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
210
	 */
211
	/** {@inheritDoc} */
212
	@Override
213
	public void paintControl(PaintEvent e) {
214
		TableWrapData layoutData = CdmFormFactoryFacade.FILL(2, 1);
215
		Control control = (Control) e.widget;
216
		if(image != null){
217
			Rectangle imageMaxBounds = calculateImageBounds(image, control);
218
			layoutData.heightHint = imageMaxBounds.height;
219
			e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
220
		}else{
221
			layoutData.heightHint = 10;
222
			e.gc.drawRectangle(0, 0, 0, 10);
223
		}
224
		control.setLayoutData(layoutData);
225
		e.gc.dispose();
226
	}
227

    
228
}
(20-20/31)