Project

General

Profile

Download (9.74 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.http.HttpException;
17
import org.apache.sanselan.ImageReadException;
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.widgets.Label;
24

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

    
44
/**
45
 * @author pplitzner
46
 * @date 25.08.2014
47
 *
48
 */
49
public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
50

    
51
    /**
52
     * @author pplitzner
53
     * @since Jul 11, 2019
54
     *
55
     */
56
    private final class LoadImageJob extends Job {
57
        private URI uri;
58
        private LoadImageJob(URI uri, String name) {
59
            super(name);
60
            this.uri = uri;
61
        }
62

    
63
        @Override
64
        protected IStatus run(IProgressMonitor monitor) {
65
            ImageInfo imageInfo;
66
            try {
67
                //first check if uri refers to an actual (non-image) file
68
                UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
69
                imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
70
                MediaDetailElement.this.getLayoutComposite().getDisplay().asyncExec(()->{
71
                    singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
72
                    if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
73
                        ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
74
                        image.setHeight(imageInfo.getHeight());
75
                        image.setWidth(imageInfo.getWidth());
76
                    }
77
                    singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
78
                    singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
79
                    element_image = formFactory.createImageElement(parentFormElement, uri, style);
80
                    StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
81
                    try {
82
                        element_image.initImageUri(uri);
83
                    } catch (IOException | HttpException e) {
84
                        exception(e, uri);
85
                    }
86
                    element_image.loadImage();
87
                    disposeErrorLabel();
88
                });
89
            } catch (IOException|HttpException e){
90
               exception(e, uri);
91
            }
92
            return Status.OK_STATUS;
93
        }
94
    }
95

    
96
    private UriWithLabelElement textUri;
97
    private MediaRepresentationSection section_mediaRepresentation;
98

    
99
    private boolean isAdvancedMediaView;
100
    private MediaRepresentationPart singleMediaRepresentationPart;
101
    private ICdmFormElement parentFormElement;
102
    private int style;
103

    
104
    /**
105
     * Used to store the URI even if it is invalid and thus cannot be stored in CDM
106
     */
107
    private String uriBuffer;
108
    private ImageElement element_image;
109
    private Label lblNoImage;
110

    
111
    public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
112
        super(formFactory, formElement);
113
    }
114

    
115
    @Override
116
    protected void createControls(ICdmFormElement formElement, Media entity, int style) {
117

    
118
        this.parentFormElement = formElement;
119
        this.style = style;
120

    
121
    }
122

    
123
    @Override
124
    public void setEntity(Media entity) {
125
        super.setEntity(entity);
126
        if(singleMediaRepresentationPart==null){
127
            singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
128
        }
129
        initIsAdvancedMediaView();
130
        showAdvancedView();
131
    }
132

    
133
    @Override
134
    public void handleEvent(Object eventSource){
135
        if(eventSource==textUri){
136
            textUri.setBackground(getPersistentBackground());
137
            URI uri = textUri.parseText();
138
            singleMediaRepresentationPart.setUri(uri);
139
            if(uri==null){
140
                uriBuffer=textUri.getText();
141
            }
142
            else{
143
                new LoadImageJob(uri, "loadImage").schedule();
144
            }
145
        }
146
    }
147

    
148
    private void exception(Exception e, URI uri){
149
        getLayoutComposite().getDisplay().asyncExec(()->{
150
            if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
151
                disposeErrorLabel();
152
                handleException(uri, "No preview available for this file type");
153
            }
154
            else{
155
                disposeErrorLabel();
156
                handleException(uri, "No file found");
157
            }
158
        });
159
    }
160

    
161
	private void disposeErrorLabel() {
162
		if(lblNoImage!=null){
163
		    lblNoImage.dispose();
164
		}
165
		lblNoImage = null;
166
	}
167

    
168
    public void toggleAdvancedMediaView() {
169
        if (getEntity().getRepresentations() != null
170
                && (getEntity().getRepresentations().size() > 1 ||
171
                        (getEntity().getRepresentations().size() == 1
172
                        && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
173
            MessagingUtils.informationDialog("Toggling not possible",
174
                    "Media consists of multiple representations or representatio parts");
175
            // toggling is only possible if there are no more than one
176
            // MediaRepresentation resp. MediaRepresentationParts
177
            return;
178
        }
179
        isAdvancedMediaView = !isAdvancedMediaView;
180
        showAdvancedView();
181
        StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
182
    }
183

    
184
    private void showAdvancedView() {
185

    
186
        if(isAdvancedMediaView){
187
            if(textUri!=null){
188
                removeElementsAndControls(textUri);
189
            }
190
            if(element_image!=null){
191
                element_image.dispose();
192
                element_image = null;
193
            }
194
            section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, StoreUtil.getSectionStyle(MediaRepresentationSection.class, getEntity().getClass().getCanonicalName()));
195
            section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
196
            section_mediaRepresentation.setEntity(getEntity());
197
            //set buffered uri as text if uri had parsing problems in simple view
198
            if(uriBuffer!=null){
199
                section_mediaRepresentation.getLayoutComposite().getChildren();
200
            }
201
        }
202
        else{
203
            if(section_mediaRepresentation!=null){
204
                removeElementsAndControls(section_mediaRepresentation);
205
            }
206
            textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
207
            URI uri = singleMediaRepresentationPart.getUri();
208
            textUri.setParsedText(uri);
209
            //set buffered uri as text if uri had parsing problems in advanced view
210
            if(uri==null && uriBuffer!=null){
211
                textUri.setText(uriBuffer);
212
                textUri.parseText();
213
            }
214
            textUri.getLayoutComposite().layout();
215

    
216
            new LoadImageJob(singleMediaRepresentationPart.getUri(), "loadImage").schedule();
217
        }
218
    }
219

    
220
    private void initIsAdvancedMediaView(){
221
        if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
222
            isAdvancedMediaView = true;
223
            return;
224
        }
225
        Set<MediaRepresentation> representations = getEntity().getRepresentations();
226
        if(representations.size()>1){
227
            isAdvancedMediaView = true;
228
            return;
229
        }
230
        if(representations.size()==1){
231
            List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
232
            if(parts.size()>1){
233
                isAdvancedMediaView =  true;
234
                return;
235
            }
236
        }
237
        isAdvancedMediaView =  false;
238
    }
239

    
240
    private void handleException(URI uri, String labelText) {
241
        if(element_image!=null){
242
            element_image.unloadImage();
243
            element_image.loadImage();
244
            element_image.dispose();
245
        }
246
        if(lblNoImage==null){
247
			lblNoImage = formFactory.createLabel(getLayoutComposite(), labelText);
248
            lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
249
            lblNoImage.setAlignment(SWT.CENTER);
250
        }
251
    }
252

    
253
    public boolean isAdvancedMediaView() {
254
        return isAdvancedMediaView;
255
    }
256

    
257
    public void setUriBuffer(String uriBuffer) {
258
        this.uriBuffer = uriBuffer;
259
    }
260

    
261
    public String getUriBuffer() {
262
        return uriBuffer;
263
    }
264

    
265
}
(3-3/10)