Project

General

Profile

Download (7.71 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.AbstractUtility;
37
import eu.etaxonomy.taxeditor.model.MessagingUtils;
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
            try{
58
                EPartService partService = getFormFactory().getContext().get(EPartService.class);
59
                DetailsPartE4 detailsView = AbstractUtility.getDetailsView(partService);
60
                if(detailsView!=null){
61
                    AbstractCdmDataViewerE4 viewer = (AbstractCdmDataViewerE4) detailsView.getViewer();
62
                    if(viewer!=null){
63
                        viewer.reflow();
64
                    }
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){
71
                        AbstractCdmDataViewerE4 viewer = (AbstractCdmDataViewerE4) (supplementalPart).getViewer();
72
                        if(viewer!=null){
73
                            viewer.refresh();
74
                        }
75
                    }
76
                }
77
            }
78
            catch(IllegalStateException e){
79
                //when migrating to E4 this execption should not be thrown anymore
80
            }
81
        }
82
    };
83

    
84
    protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
85
        super(formFactory, parentElement);
86

    
87
        container = new Composite(getLayoutComposite(), style);
88
        container.setLayoutData(LayoutConstants.FILL(2, 1));
89

    
90
        container.addPaintListener(this);
91
    }
92

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

    
99

    
100
    public URI getImageUri() {
101
        return imageUri;
102
    }
103

    
104
    public void loadImage(){
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
                        MessagingUtils.messageDialog("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 void unloadImage() {
125
        Job job = new Job("Unloading image") {
126

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

    
136
                return Status.OK_STATUS;
137
            }
138
        };
139
        job.schedule();
140

    
141
    }
142

    
143
    public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
144

    
145
        final Display display = getLayoutComposite().getDisplay();
146

    
147
        IRunnableWithProgress runnable = new IRunnableWithProgress(){
148

    
149
            @Override
150
            public void run(IProgressMonitor monitor) {
151
                monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
152

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

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

    
175
        };
176

    
177
        return runnable;
178
    }
179

    
180
    private Rectangle calculateImageBounds(Image image, Control control){
181
        Rectangle imageBounds = image.getBounds();
182
        Rectangle containerBounds = control.getBounds();
183

    
184
        Integer imgWidth = imageBounds.width;
185
        Integer imgHeight = imageBounds.height;
186

    
187
        Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
188
        Integer width = containerBounds.width;
189
        Integer height = ((Float) (width * ratio)).intValue();
190

    
191
        return new Rectangle(containerBounds.x, containerBounds.y, width, height);
192
    }
193

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

    
206
    /** {@inheritDoc} */
207
    @Override
208
    public void paintControl(PaintEvent e) {
209
        TableWrapData layoutData = LayoutConstants.FILL(2, 1);
210
        Control control = (Control) e.widget;
211
        if(image != null){
212
            Rectangle imageMaxBounds = calculateImageBounds(image, control);
213
            layoutData.heightHint = imageMaxBounds.height;
214
            e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
215
        }else{
216
            layoutData.heightHint = 10;
217
            e.gc.drawRectangle(0, 0, 0, 10);
218
        }
219
        control.setLayoutData(layoutData);
220
        e.gc.dispose();
221
    }
222

    
223
}
(23-23/47)