Project

General

Profile

Download (10.3 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.l10n.Messages;
34
import eu.etaxonomy.taxeditor.model.MessagingUtils;
35
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
36
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
import eu.etaxonomy.taxeditor.store.StoreUtil;
38
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
39
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
40
import eu.etaxonomy.taxeditor.ui.element.ImageElement;
41
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
42
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
43
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
44

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

    
52
    private static final String LOAD_IMAGE = Messages.MediaDetailElement_LOAD_IMAGE;
53

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

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

    
104
    private UriWithLabelElement textUri;
105
    private MediaRepresentationSection section_mediaRepresentation;
106

    
107
    private boolean isAdvancedMediaView;
108
    private MediaRepresentationPart singleMediaRepresentationPart;
109
    private ICdmFormElement parentFormElement;
110
    private int style;
111

    
112
    /**
113
     * Used to store the URI even if it is invalid and thus cannot be stored in CDM
114
     */
115
    private String uriBuffer;
116
    private ImageElement element_image;
117
    private Label lblNoImage;
118

    
119
    public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
120
        super(formFactory, formElement);
121
    }
122

    
123
    @Override
124
    protected void createControls(ICdmFormElement formElement, Media entity, int style) {
125

    
126
        this.parentFormElement = formElement;
127
        this.style = style;
128

    
129
    }
130

    
131
    @Override
132
    public void setEntity(Media entity) {
133
        super.setEntity(entity);
134
        if(singleMediaRepresentationPart==null){
135
            singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
136
        }
137
        initIsAdvancedMediaView();
138
        showAdvancedView();
139
    }
140

    
141
    @Override
142
    public void handleEvent(Object eventSource){
143
        if(eventSource==textUri){
144
            textUri.setBackground(getPersistentBackground());
145
            URI uri = textUri.parseText();
146
            singleMediaRepresentationPart.setUri(uri);
147
            if(uri==null){
148
                uriBuffer=textUri.getText();
149
            }
150
            else{
151
                new LoadImageJob(uri, LOAD_IMAGE).schedule();
152
            }
153
        }
154
    }
155

    
156
    private void exception(Exception e){
157
        if(!getLayoutComposite().isDisposed()){
158
            getLayoutComposite().getDisplay().asyncExec(()->{
159
                String exceptionString;
160
                if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
161
                    disposeErrorLabel();
162
                    exceptionString = Messages.MediaDetailElement_NO_PREVIEW;
163
                }
164
                else{
165
                    disposeErrorLabel();
166
                    exceptionString = Messages.MediaDetailElement_NO_FILE_FOUND;
167
                }
168
                disposeImage();
169
                if(lblNoImage==null){
170
                    lblNoImage = formFactory.createLabel(getLayoutComposite(), exceptionString);
171
                    lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
172
                    lblNoImage.setAlignment(SWT.CENTER);
173
                }
174
                StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
175
            });
176
        }
177
    }
178

    
179
    private void disposeImage(){
180
        if(element_image!=null){
181
            element_image.dispose();
182
            element_image = null;
183
        }
184
    }
185

    
186
	private void disposeErrorLabel() {
187
		if(lblNoImage!=null){
188
		    lblNoImage.dispose();
189
		}
190
		lblNoImage = null;
191
	}
192

    
193
    public void toggleAdvancedMediaView() {
194
        if (getEntity().getRepresentations() != null
195
                && (getEntity().getRepresentations().size() > 1 ||
196
                        (getEntity().getRepresentations().size() == 1
197
                        && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
198
            MessagingUtils.informationDialog(Messages.MediaDetailElement_TOGGLE_NOT_POSSIBLE_TITLE,
199
                    Messages.MediaDetailElement_TOGGLE_NOT_POSSIBLE_MESSAGE);
200
            // toggling is only possible if there are no more than one
201
            // MediaRepresentation resp. MediaRepresentationParts
202
            return;
203
        }
204
        isAdvancedMediaView = !isAdvancedMediaView;
205
        showAdvancedView();
206
        StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
207
    }
208

    
209
    private void showAdvancedView() {
210

    
211
        if(isAdvancedMediaView){
212
            if(textUri!=null){
213
                removeElementsAndControls(textUri);
214
            }
215
            disposeImage();
216
            section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, StoreUtil.getSectionStyle(MediaRepresentationSection.class, getEntity().getClass().getCanonicalName()));
217
            section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
218
            section_mediaRepresentation.setEntity(getEntity());
219
            //set buffered uri as text if uri had parsing problems in simple view
220
            if(uriBuffer!=null){
221
                section_mediaRepresentation.getLayoutComposite().getChildren();
222
            }
223
        }
224
        else{
225
            if(section_mediaRepresentation!=null){
226
                removeElementsAndControls(section_mediaRepresentation);
227
            }
228
            textUri = formFactory.createUriWithLabelElement(parentFormElement, Messages.MediaDetailElement_Media_URI, null, style);
229
            URI uri = singleMediaRepresentationPart.getUri();
230
            textUri.setParsedText(uri);
231
            //set buffered uri as text if uri had parsing problems in advanced view
232
            if(uri==null && uriBuffer!=null){
233
                textUri.setText(uriBuffer);
234
                textUri.parseText();
235
            }
236
            textUri.getLayoutComposite().layout();
237

    
238
            new LoadImageJob(singleMediaRepresentationPart.getUri(), LOAD_IMAGE).schedule();
239
        }
240
    }
241

    
242
    private void initIsAdvancedMediaView(){
243
        if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
244
            isAdvancedMediaView = true;
245
            return;
246
        }
247
        Set<MediaRepresentation> representations = getEntity().getRepresentations();
248
        if(representations.size()>1){
249
            isAdvancedMediaView = true;
250
            return;
251
        }
252
        if(representations.size()==1){
253
            List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
254
            if(parts.size()>1){
255
                isAdvancedMediaView =  true;
256
                return;
257
            }
258
        }
259
        isAdvancedMediaView =  false;
260
    }
261

    
262
    public boolean isAdvancedMediaView() {
263
        return isAdvancedMediaView;
264
    }
265

    
266
    public void setUriBuffer(String uriBuffer) {
267
        this.uriBuffer = uriBuffer;
268
    }
269

    
270
    public String getUriBuffer() {
271
        return uriBuffer;
272
    }
273

    
274
}
(3-3/10)