Project

General

Profile

Download (11.6 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

    
100
                        disposeErrorLabel();
101
                        StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
102

    
103

    
104
                    }
105
                });
106
            } catch (Exception e){
107
               exception(e);
108
            }
109
            return Status.OK_STATUS;
110
        }
111
    }
112

    
113
    private UriWithLabelElement textUri;
114
    private MediaRepresentationSection section_mediaRepresentation;
115

    
116
    private boolean isAdvancedMediaView;
117
    private MediaRepresentationPart singleMediaRepresentationPart;
118
    private ICdmFormElement parentFormElement;
119
    private int style;
120

    
121
    /**
122
     * Used to store the URI even if it is invalid and thus cannot be stored in CDM
123
     */
124
    private String uriBuffer;
125
    private ImageElement element_image;
126
    private Label lblNoImage;
127

    
128
    public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
129
        super(formFactory, formElement);
130
    }
131

    
132
    @Override
133
    protected void createControls(ICdmFormElement formElement, Media entity, int style) {
134

    
135
        this.parentFormElement = formElement;
136
        this.style = style;
137

    
138
    }
139

    
140
    @Override
141
    public void setEntity(Media entity) {
142
        super.setEntity(entity);
143
        if(singleMediaRepresentationPart==null){
144
            singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
145
        }
146
        initIsAdvancedMediaView();
147
        showAdvancedView();
148
    }
149

    
150
    public boolean isShowImage() {
151
        return isShowImage;
152
    }
153

    
154
    public void setShowImage(boolean isShowImage) {
155
        this.isShowImage = isShowImage;
156
    }
157

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

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

    
199
    private void disposeImage(){
200
        if(element_image!=null){
201
            element_image.dispose();
202
            element_image = null;
203
        }
204
    }
205

    
206
	private void disposeErrorLabel() {
207
		if(lblNoImage!=null){
208
		    lblNoImage.dispose();
209
		}
210
		lblNoImage = null;
211
	}
212

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

    
229
    private void showAdvancedView() {
230

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

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

    
295
    public boolean isAdvancedMediaView() {
296
        return isAdvancedMediaView;
297
    }
298

    
299
    public void setUriBuffer(String uriBuffer) {
300
        this.uriBuffer = uriBuffer;
301
    }
302

    
303
    public String getUriBuffer() {
304
        return uriBuffer;
305
    }
306

    
307
}
(3-3/10)