Project

General

Profile

Download (6.76 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.ui.element;
11

    
12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.net.URI;
15

    
16
import org.apache.http.HttpException;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
20
import org.eclipse.core.runtime.jobs.Job;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.workbench.modeling.EPartService;
23
import org.eclipse.jface.operation.IRunnableWithProgress;
24
import org.eclipse.swt.events.PaintEvent;
25
import org.eclipse.swt.events.PaintListener;
26
import org.eclipse.swt.graphics.GC;
27
import org.eclipse.swt.graphics.Image;
28
import org.eclipse.swt.graphics.Rectangle;
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.model.MessagingUtils;
37
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
38
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmDataViewerE4;
39
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
40
import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
41

    
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
	private Composite container;
53

    
54
	private final Runnable postRunnable = new Runnable(){
55
        @Override
56
        public void run() {
57
            //FIXME E4 migrate
58
            try{
59
            EPartService partService = TaxeditorStorePlugin.getDefault().getWorkbench().getService(EPartService.class);
60
            MPart mPartDetails = partService.findPart("eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4");
61
            if(mPartDetails!=null){
62
                DetailsPartE4 detailsPart = (DetailsPartE4)mPartDetails.getObject();
63
                if(detailsPart!=null && detailsPart.getViewer()!=null){
64
                    ((AbstractCdmDataViewerE4) detailsPart.getViewer()).reflow();
65
                }
66
            }
67
            MPart mPartSupplemental = partService.findPart("eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4");
68
            if(mPartSupplemental!=null){
69
                SupplementalDataPartE4 supplementalPart = (SupplementalDataPartE4)mPartSupplemental.getObject();
70
                if(supplementalPart!=null && supplementalPart.getViewer()!=null){
71
                    ((AbstractCdmDataViewerE4) (supplementalPart).getViewer()).reflow();
72
                }
73
            }
74
            }
75
            catch(IllegalStateException e){
76
                //when migrating to E4 this execption should not be thrown anymore
77
            }
78
        }
79
    };
80

    
81
	protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
82
		super(formFactory, parentElement);
83

    
84
		container = new Composite(getLayoutComposite(), style);
85
		container.setLayoutData(LayoutConstants.FILL(2, 1));
86

    
87
		container.addPaintListener(this);
88
	}
89

    
90
	public void initImageUri(URI uri) throws IOException, HttpException {
91
		this.imageUri = uri;
92
		InputStream imageStream = UriUtils.getInputStream(imageUri);
93
		image = new Image(Display.getCurrent(), imageStream);
94
	}
95

    
96

    
97
	public URI getImageUri() {
98
		return imageUri;
99
	}
100

    
101
	public void loadImage(){
102
		if(getImageUri() != null){
103
			Job job = new Job("Loading image") {
104

    
105
				@Override
106
				protected IStatus run(IProgressMonitor monitor) {
107
					IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
108
					try {
109
						runnable.run(monitor);
110
					} catch (Exception e) {
111
						MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
112
					}
113

    
114
					return Status.OK_STATUS;
115
				}
116
			};
117
			job.schedule();
118
		}
119
	}
120

    
121
	public void unloadImage() {
122
		Job job = new Job("Unloading image") {
123

    
124
			@Override
125
			protected IStatus run(IProgressMonitor monitor) {
126
				IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
127
				try {
128
					runnable.run(monitor);
129
				} catch (Exception e) {
130
					MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
131
				}
132

    
133
				return Status.OK_STATUS;
134
			}
135
		};
136
		job.schedule();
137

    
138
	}
139

    
140
	public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
141

    
142
		final Display display = getLayoutComposite().getDisplay();
143

    
144
		IRunnableWithProgress runnable = new IRunnableWithProgress(){
145

    
146
			@Override
147
			public void run(IProgressMonitor monitor) {
148
				monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
149

    
150
				// redraw the image container
151
				display.asyncExec(new Runnable(){
152
					@Override
153
					public void run() {
154
						if(! getLayoutComposite().isDisposed() && container!=null){
155
							Event untypedEvent = new Event();
156
							untypedEvent.widget = container;
157
							PaintEvent event = new PaintEvent(untypedEvent);
158
							event.gc = new GC(container);
159
							paintControl(event);
160
							getLayoutComposite().layout();
161
						}
162
					}
163
				});
164

    
165
				// execute the external runnable
166
				if(postRunnable != null){
167
					display.asyncExec(postRunnable);
168
				}
169
				monitor.done();
170
			}
171

    
172
		};
173

    
174
		return runnable;
175
	}
176

    
177
	private Rectangle calculateImageBounds(Image image, Control control){
178
		Rectangle imageBounds = image.getBounds();
179
		Rectangle containerBounds = control.getBounds();
180

    
181
		Integer imgWidth = imageBounds.width;
182
		Integer imgHeight = imageBounds.height;
183

    
184
		Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
185
		Integer width = containerBounds.width;
186
		Integer height = ((Float) (width * ratio)).intValue();
187

    
188
		return new Rectangle(containerBounds.x, containerBounds.y, width, height);
189
	}
190

    
191
	public void dispose(){
192
	    if(image!=null){
193
	        image.dispose();
194
	        image = null;
195
	    }
196
	    imageUri = null;
197
	    if(container!=null){
198
	        container.dispose();
199
	        container = null;
200
	    }
201
	}
202

    
203
	/** {@inheritDoc} */
204
	@Override
205
	public void paintControl(PaintEvent e) {
206
		TableWrapData layoutData = LayoutConstants.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
}
(23-23/45)