Project

General

Profile

Download (7.23 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
    /**
50
     * @author pplitzner
51
     * @since Jul 17, 2019
52
     *
53
     */
54
    public class LoadImageJob extends Job {
55
        public LoadImageJob(String name) {
56
            super(name);
57
        }
58

    
59
        @Override
60
        protected IStatus run(IProgressMonitor monitor) {
61
            IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
62
            try {
63
                runnable.run(monitor);
64
            } catch (Exception e) {
65
                MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage()  + ": " +  getImageUri(), e);
66
            }
67

    
68
            return Status.OK_STATUS;
69
        }
70
    }
71

    
72
    private URI imageUri;
73
    private Image image;
74

    
75
    private Composite container;
76

    
77
    private final Runnable postRunnable = new Runnable(){
78
        @Override
79
        public void run() {
80
            try{
81
                EPartService partService = getFormFactory().getContext().get(EPartService.class);
82
                DetailsPartE4 detailsView = AbstractUtility.getDetailsView(partService);
83
                if(detailsView!=null){
84
                    AbstractCdmDataViewerE4 viewer = (AbstractCdmDataViewerE4) detailsView.getViewer();
85
                    if(viewer!=null){
86
                        viewer.reflow();
87
                    }
88
                }
89

    
90
                MPart mPartSupplemental = partService.findPart("eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4");
91
                if(mPartSupplemental!=null){
92
                    SupplementalDataPartE4 supplementalPart = (SupplementalDataPartE4)mPartSupplemental.getObject();
93
                    if(supplementalPart!=null){
94
                        AbstractCdmDataViewerE4 viewer = (AbstractCdmDataViewerE4) (supplementalPart).getViewer();
95
                        if(viewer!=null){
96
                            viewer.refresh();
97
                        }
98
                    }
99
                }
100
            }
101
            catch(IllegalStateException e){
102
                //when migrating to E4 this execption should not be thrown anymore
103
            }
104
        }
105
    };
106

    
107
    protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
108
        super(formFactory, parentElement);
109

    
110
        container = new Composite(getLayoutComposite(), style);
111
        container.setLayoutData(LayoutConstants.FILL(2, 1));
112

    
113
        container.addPaintListener(this);
114
    }
115

    
116
    public void initImageUri(URI uri) throws IOException, HttpException {
117
        this.imageUri = uri;
118
        InputStream imageStream = UriUtils.getInputStream(imageUri);
119
        image = new Image(Display.getCurrent(), imageStream);
120
    }
121

    
122

    
123
    public URI getImageUri() {
124
        return imageUri;
125
    }
126

    
127
    public void loadImage(){
128
        if(getImageUri() != null){
129
            Job job = new LoadImageJob("Loading image");
130
            job.schedule();
131
        }
132
    }
133

    
134
    public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
135

    
136
        final Display display = getLayoutComposite().getDisplay();
137

    
138
        IRunnableWithProgress runnable = new IRunnableWithProgress(){
139

    
140
            @Override
141
            public void run(IProgressMonitor monitor) {
142
                monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
143

    
144
                // redraw the image container
145
                display.asyncExec(new Runnable(){
146
                    @Override
147
                    public void run() {
148
                        if(! getLayoutComposite().isDisposed() && container!=null){
149
                            Event untypedEvent = new Event();
150
                            untypedEvent.widget = container;
151
                            PaintEvent event = new PaintEvent(untypedEvent);
152
                            event.gc = new GC(container);
153
                            paintControl(event);
154
                            getLayoutComposite().layout();
155
                        }
156
                    }
157
                });
158

    
159
                // execute the external runnable
160
                if(postRunnable != null){
161
                    display.asyncExec(postRunnable);
162
                }
163
                monitor.done();
164
            }
165

    
166
        };
167

    
168
        return runnable;
169
    }
170

    
171
    private Rectangle calculateImageBounds(Image image, Control control){
172
        Rectangle imageBounds = image.getBounds();
173
        Rectangle containerBounds = control.getBounds();
174

    
175
        Integer imgWidth = imageBounds.width;
176
        Integer imgHeight = imageBounds.height;
177

    
178
        Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
179
        Integer width = containerBounds.width;
180
        Integer height = ((Float) (width * ratio)).intValue();
181

    
182
        return new Rectangle(containerBounds.x, containerBounds.y, width, height);
183
    }
184

    
185
    public void dispose(){
186
        if(image!=null){
187
            image.dispose();
188
            image = null;
189
        }
190
        imageUri = null;
191
        if(container!=null){
192
            container.dispose();
193
            container = null;
194
        }
195
    }
196

    
197
    /** {@inheritDoc} */
198
    @Override
199
    public void paintControl(PaintEvent e) {
200
        TableWrapData layoutData = LayoutConstants.FILL(2, 1);
201
        Control control = (Control) e.widget;
202
        if(image != null){
203
            Rectangle imageMaxBounds = calculateImageBounds(image, control);
204
            layoutData.heightHint = imageMaxBounds.height;
205
            e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
206
        }else{
207
            layoutData.heightHint = 10;
208
            e.gc.drawRectangle(0, 0, 0, 10);
209
        }
210
        control.setLayoutData(layoutData);
211
        e.gc.dispose();
212
    }
213

    
214
}
(24-24/48)