Project

General

Profile

Download (7.6 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
package eu.etaxonomy.taxeditor.ui.element;
10

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

    
15
import org.apache.http.HttpException;
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.e4.core.contexts.IEclipseContext;
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.store.StoreUtil;
39
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmDataViewerE4;
40
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
41
import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
42

    
43
/**
44
 * @author n.hoffmann
45
 * @created Sep 24, 2010
46
 */
47
public class ImageElement extends AbstractCdmFormElement implements PaintListener{
48

    
49
    /**
50
     * @author pplitzner
51
     * @since Jul 17, 2019
52
     */
53
    public class LoadImageJob extends Job {
54
        public LoadImageJob(String name) {
55
            super(name);
56
        }
57

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

    
67
            return Status.OK_STATUS;
68
        }
69
    }
70

    
71
    private URI imageUri;
72
    private Image image;
73

    
74
    private Composite container;
75

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

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

    
110
    protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
111
        super(formFactory, parentElement);
112

    
113
        container = new Composite(getLayoutComposite(), style);
114
        container.setLayoutData(LayoutConstants.FILL(2, 1));
115

    
116
        container.addPaintListener(this);
117
    }
118

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

    
125

    
126
    public URI getImageUri() {
127
        return imageUri;
128
    }
129

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

    
137
    public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
138

    
139
        final Display display = getLayoutComposite().getDisplay();
140

    
141
        IRunnableWithProgress runnable = new IRunnableWithProgress(){
142

    
143
            @Override
144
            public void run(IProgressMonitor monitor) {
145
                monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
146

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

    
158
                        }
159
                    }
160
                });
161

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

    
169
        };
170

    
171
        return runnable;
172
    }
173

    
174
    private Rectangle calculateImageBounds(Image image, Control control){
175
        Rectangle imageBounds = image.getBounds();
176
        Rectangle containerBounds = control.getBounds();
177

    
178
        Integer imgWidth = imageBounds.width;
179
        Integer imgHeight = imageBounds.height;
180

    
181
        Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
182
        Integer width = containerBounds.width;
183
        Integer height = ((Float) (width * ratio)).intValue();
184

    
185
        return new Rectangle(containerBounds.x, containerBounds.y, width, height);
186
    }
187

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

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

    
217
}
(28-28/53)