Project

General

Profile

Download (5.31 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.InputStream;
14
import java.net.URI;
15

    
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.core.runtime.jobs.Job;
20
import org.eclipse.jface.operation.IRunnableWithProgress;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.events.PaintEvent;
23
import org.eclipse.swt.events.PaintListener;
24
import org.eclipse.swt.graphics.GC;
25
import org.eclipse.swt.graphics.Image;
26
import org.eclipse.swt.graphics.Rectangle;
27
import org.eclipse.swt.widgets.Composite;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Event;
30
import org.eclipse.swt.widgets.Label;
31
import org.eclipse.ui.forms.widgets.TableWrapData;
32

    
33
import eu.etaxonomy.cdm.common.UriUtils;
34
import eu.etaxonomy.taxeditor.store.StoreUtil;
35

    
36
/**
37
 * <p>ImageElement class.</p>
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
	private Rectangle imageMaxBounds;
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
		setImageUri(imageUri);
63
		loadImage();
64
		
65
		container = new Composite(getLayoutComposite(), style);
66
		container.setLayoutData(CdmFormFactory.FILL(2, 1));
67
		
68
		container.addPaintListener(this);
69
	}
70
	
71

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

    
79
	}
80

    
81
	/**
82
	 * <p>Setter for the field <code>imageUri</code>.</p>
83
	 *
84
	 * @param uri a {@link java.net.URI} object.
85
	 */
86
	public void setImageUri(URI uri) {
87
		this.imageUri = uri;
88
	}
89
	
90
	
91
	/**
92
	 * <p>Getter for the field <code>imageUri</code>.</p>
93
	 *
94
	 * @return the imageUri
95
	 */
96
	public URI getImageUri() {
97
		return imageUri;
98
	}
99
	
100
	public void loadImage(){
101
		loadImage(null);
102
	}
103
	
104
	public void loadImage(final Runnable postRunnable){
105
		if(getImageUri() != null){
106
			Job job = new Job("Loading image") {
107
				
108
				@Override
109
				protected IStatus run(IProgressMonitor monitor) {
110
					IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
111
					try {
112
						runnable.run(monitor);
113
					} catch (Exception e) {
114
						StoreUtil.errorDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
115
					}
116
					
117
					return Status.OK_STATUS;
118
				}
119
			};
120
			job.schedule();
121
		}
122
	}
123
	
124
	public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
125
		
126
		final Display display = getLayoutComposite().getDisplay(); 
127
		
128
		IRunnableWithProgress runnable = new IRunnableWithProgress(){
129
			
130
			@Override
131
			public void run(IProgressMonitor monitor) {
132
				try {	
133
					monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
134
					InputStream imageStream = UriUtils.getInputStream(imageUri);
135
					
136
					image = new Image(Display.getCurrent(), imageStream);
137
					
138
					// redraw the image container
139
					display.asyncExec(new Runnable(){
140
						@Override
141
						public void run() {
142
							if(! getLayoutComposite().isDisposed()){
143
								Event untypedEvent = new Event();
144
								untypedEvent.widget = container;
145
								PaintEvent event = new PaintEvent(untypedEvent);
146
								event.gc = new GC(container);
147
								paintControl(event);
148
								getLayoutComposite().layout();
149
							}
150
						}
151
					});
152
					
153
					// execute the external runnable
154
					if(postRunnable != null){
155
						display.asyncExec(postRunnable);
156
					}
157
					monitor.done();					
158
				}catch (Exception e) {
159
					StoreUtil.warn(getClass(), e);
160
				} 
161
			}					
162
		};
163
		
164
		return runnable;
165
	}
166

    
167
	private void calculateImageBounds(){
168
		Rectangle imageBounds = image.getBounds();
169
		Rectangle containerBounds = container.getBounds();
170
		
171
		Integer imgWidth = imageBounds.width;
172
		Integer imgHeight = imageBounds.height;
173
		
174
		Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
175
		Integer width = containerBounds.width;
176
		Float height = width * ratio;
177
		
178
		imageMaxBounds = new Rectangle(containerBounds.x, containerBounds.y, width, height.intValue());
179
	}
180

    
181

    
182
	/* (non-Javadoc)
183
	 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
184
	 */
185
	/** {@inheritDoc} */
186
	@Override
187
	public void paintControl(PaintEvent e) {
188
		if(image != null){
189
			calculateImageBounds();
190
			Composite composite = (Composite) e.widget;
191
			TableWrapData layoutData = CdmFormFactory.FILL(2, 1);
192
			layoutData.heightHint = imageMaxBounds.height;
193
			composite.setLayoutData(layoutData);
194
			e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
195
			e.gc.dispose();
196
		}		
197
	}
198
}
(20-20/35)