Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 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.section.media;
10

    
11
import java.io.IOException;
12
import java.net.URI;
13
import java.util.List;
14
import java.util.Set;
15

    
16
import org.apache.commons.imaging.ImageReadException;
17
import org.apache.http.HttpException;
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.core.runtime.jobs.Job;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.SelectionAdapter;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.widgets.Button;
26
import org.eclipse.swt.widgets.Label;
27

    
28
import eu.etaxonomy.cdm.common.UriUtils;
29
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.media.ImageFile;
32
import eu.etaxonomy.cdm.model.media.Media;
33
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
34
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
35
import eu.etaxonomy.cdm.model.media.MediaUtils;
36
import eu.etaxonomy.taxeditor.l10n.Messages;
37
import eu.etaxonomy.taxeditor.model.MessagingUtils;
38
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
39
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
40
import eu.etaxonomy.taxeditor.store.StoreUtil;
41
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
42
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
43
import eu.etaxonomy.taxeditor.ui.element.ImageElement;
44
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
45
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
46
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
47

    
48
/**
49
 * @author pplitzner
50
 * @date 25.08.2014
51
 */
52
public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
53

    
54
    private static final String LOAD_IMAGE = Messages.MediaDetailElement_LOAD_IMAGE;
55

    
56
    private boolean isShowImage = true;
57

    
58
    /**
59
     * @author pplitzner
60
     * @since Jul 11, 2019
61
     *
62
     */
63
    private final class LoadImageJob extends Job {
64
        private URI uri;
65
        private LoadImageJob(URI uri, String name) {
66
            super(name);
67
            this.uri = uri;
68
        }
69

    
70
        @Override
71
        protected IStatus run(IProgressMonitor monitor) {
72
            try {
73
                //first check if uri refers to an actual (non-image) file
74

    
75
                UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
76

    
77
                CdmImageInfo imageInfo = CdmImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
78
                MediaDetailElement.this.getLayoutComposite().getDisplay().asyncExec(()->{
79
                    singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
80
                    if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
81
                        ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
82
                        image.setHeight(imageInfo.getHeight());
83
                        image.setWidth(imageInfo.getWidth());
84
                    }
85
                    if(singleMediaRepresentationPart.getMediaRepresentation()!=null){
86
                        singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
87
                        singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
88
                    }
89
                    disposeImage();
90
                    if (!parentFormElement.getLayoutComposite().isDisposed()) {
91
                        element_image = formFactory.createImageElement(parentFormElement, uri, style);
92
                        StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
93
                        try {
94
                            element_image.initImageUri(uri);
95
                        } catch (IOException | HttpException e) {
96
                            exception(e);
97
                        }
98
                        element_image.loadImage();
99
                        disposeErrorLabel();
100
                    }
101
                });
102
            } catch (Exception e){
103
               exception(e);
104
            }
105
            return Status.OK_STATUS;
106
        }
107
    }
108

    
109
    private UriWithLabelElement textUri;
110
    private MediaRepresentationSection section_mediaRepresentation;
111

    
112
    private boolean isAdvancedMediaView;
113
    private MediaRepresentationPart singleMediaRepresentationPart;
114
    private ICdmFormElement parentFormElement;
115
    private int style;
116

    
117
    /**
118
     * Used to store the URI even if it is invalid and thus cannot be stored in CDM
119
     */
120
    private String uriBuffer;
121
    private ImageElement element_image;
122
    private Label lblNoImage;
123

    
124
    public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
125
        super(formFactory, formElement);
126
    }
127

    
128
    @Override
129
    protected void createControls(ICdmFormElement formElement, Media entity, int style) {
130

    
131
        this.parentFormElement = formElement;
132
        this.style = style;
133

    
134
    }
135

    
136
    @Override
137
    public void setEntity(Media entity) {
138
        super.setEntity(entity);
139
        if(singleMediaRepresentationPart==null){
140
            singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
141
        }
142
        initIsAdvancedMediaView();
143
        showAdvancedView();
144
    }
145

    
146
    public boolean isShowImage() {
147
        return isShowImage;
148
    }
149

    
150
    public void setShowImage(boolean isShowImage) {
151
        this.isShowImage = isShowImage;
152
    }
153

    
154
    @Override
155
    public void handleEvent(Object eventSource){
156
        if(eventSource==textUri){
157
            textUri.setBackground(getPersistentBackground());
158
            URI uri = textUri.parseText();
159
            singleMediaRepresentationPart.setUri(uri);
160
            if(uri==null){
161
                uriBuffer=textUri.getText();
162
            }
163
            else{
164
                if (isShowImage){
165
                    LoadImageJob job = new LoadImageJob(uri, LOAD_IMAGE);
166
                    job.schedule();
167
                }
168
            }
169
        }
170
    }
171

    
172
    private void exception(Exception e){
173
        if(!getLayoutComposite().isDisposed()){
174
            getLayoutComposite().getDisplay().asyncExec(()->{
175
                String exceptionString;
176
                if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
177
                    disposeErrorLabel();
178
                    exceptionString = Messages.MediaDetailElement_NO_PREVIEW;
179
                }
180
                else{
181
                    disposeErrorLabel();
182
                    exceptionString = Messages.MediaDetailElement_NO_FILE_FOUND;
183
                }
184
                disposeImage();
185
                if(lblNoImage==null){
186
                    lblNoImage = formFactory.createLabel(getLayoutComposite(), exceptionString);
187
                    lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
188
                    lblNoImage.setAlignment(SWT.CENTER);
189
                }
190
                StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
191
            });
192
        }
193
    }
194

    
195
    private void disposeImage(){
196
        if(element_image!=null){
197
            element_image.dispose();
198
            element_image = null;
199
        }
200
    }
201

    
202
	private void disposeErrorLabel() {
203
		if(lblNoImage!=null){
204
		    lblNoImage.dispose();
205
		}
206
		lblNoImage = null;
207
	}
208

    
209
    public void toggleAdvancedMediaView() {
210
        if (getEntity().getRepresentations() != null
211
                && (getEntity().getRepresentations().size() > 1 ||
212
                        (getEntity().getRepresentations().size() == 1
213
                        && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
214
            MessagingUtils.informationDialog(Messages.MediaDetailElement_TOGGLE_NOT_POSSIBLE_TITLE,
215
                    Messages.MediaDetailElement_TOGGLE_NOT_POSSIBLE_MESSAGE);
216
            // toggling is only possible if there are no more than one
217
            // MediaRepresentation resp. MediaRepresentationParts
218
            return;
219
        }
220
        isAdvancedMediaView = !isAdvancedMediaView;
221
        showAdvancedView();
222
        StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
223
    }
224

    
225
    private void showAdvancedView() {
226

    
227
        if(isAdvancedMediaView){
228
            if(textUri!=null){
229
                removeElementsAndControls(textUri);
230
            }
231
            disposeImage();
232
            section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, StoreUtil.getSectionStyle(MediaRepresentationSection.class, getEntity().getClass().getCanonicalName()));
233
            section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
234
            section_mediaRepresentation.setEntity(getEntity());
235
            //set buffered uri as text if uri had parsing problems in simple view
236
            if(uriBuffer!=null){
237
                section_mediaRepresentation.getLayoutComposite().getChildren();
238
            }
239
        }
240
        else{
241
            if(section_mediaRepresentation!=null){
242
                removeElementsAndControls(section_mediaRepresentation);
243
            }
244
            textUri = formFactory.createUriWithLabelElement(parentFormElement, Messages.MediaDetailElement_Media_URI, null, style);
245
            URI uri = singleMediaRepresentationPart.getUri();
246
            textUri.setParsedText(uri);
247
            //set buffered uri as text if uri had parsing problems in advanced view
248
            if(uri==null && uriBuffer!=null){
249
                textUri.setText(uriBuffer);
250
                textUri.parseText();
251
            }
252
            textUri.getLayoutComposite().layout();
253
            if (isShowImage){
254
                LoadImageJob job = new LoadImageJob(singleMediaRepresentationPart.getUri(), LOAD_IMAGE);
255
                job.schedule();
256
            } else{
257
                Button button_showImage = formFactory.createButton(getLayoutComposite(), "Show Image", SWT.PUSH);
258
                button_showImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
259
                button_showImage.addSelectionListener(new SelectionAdapter() {
260
                    @Override
261
                    public void widgetSelected(SelectionEvent e) {
262
                        LoadImageJob job = new LoadImageJob(singleMediaRepresentationPart.getUri(), "Load Image");
263
                        job.schedule();
264
                    }
265
                });
266
            }
267
        }
268
    }
269

    
270
    private void initIsAdvancedMediaView(){
271
        if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
272
            isAdvancedMediaView = true;
273
            return;
274
        }
275
        Set<MediaRepresentation> representations = getEntity().getRepresentations();
276
        if(representations.size()>1){
277
            isAdvancedMediaView = true;
278
            return;
279
        }
280
        if(representations.size()==1){
281
            List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
282
            if(parts.size()>1){
283
                isAdvancedMediaView =  true;
284
                return;
285
            }
286
        }
287
        isAdvancedMediaView =  false;
288
    }
289

    
290
    public boolean isAdvancedMediaView() {
291
        return isAdvancedMediaView;
292
    }
293

    
294
    public void setUriBuffer(String uriBuffer) {
295
        this.uriBuffer = uriBuffer;
296
    }
297

    
298
    public String getUriBuffer() {
299
        return uriBuffer;
300
    }
301

    
302
}
(3-3/10)