Project

General

Profile

Download (8.35 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.net.URI;
12
import java.util.List;
13
import java.util.Set;
14

    
15
import org.apache.sanselan.ImageReadException;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.widgets.Label;
18

    
19
import eu.etaxonomy.cdm.common.UriUtils;
20
import eu.etaxonomy.cdm.common.media.ImageInfo;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22
import eu.etaxonomy.cdm.model.media.ImageFile;
23
import eu.etaxonomy.cdm.model.media.Media;
24
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
25
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
26
import eu.etaxonomy.cdm.model.media.MediaUtils;
27
import eu.etaxonomy.taxeditor.model.MessagingUtils;
28
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
29
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
31
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32
import eu.etaxonomy.taxeditor.ui.element.ImageElement;
33
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
34
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
35
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
36

    
37
/**
38
 * @author pplitzner
39
 * @date 25.08.2014
40
 *
41
 */
42
public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
43

    
44
    private UriWithLabelElement textUri;
45
    private MediaRepresentationSection section_mediaRepresentation;
46

    
47
    private boolean isAdvancedMediaView;
48
    private MediaRepresentationPart singleMediaRepresentationPart;
49
    private ICdmFormElement parentFormElement;
50
    private int style;
51

    
52
    /**
53
     * Used to store the URI even if it is invalid and thus cannot be stored in CDM
54
     */
55
    private String uriBuffer;
56
    private ImageElement element_image;
57
    private Label lblNoImage;
58

    
59
    public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
60
        super(formFactory, formElement);
61
    }
62

    
63
    @Override
64
    protected void createControls(ICdmFormElement formElement, Media entity, int style) {
65
        this.parentFormElement = formElement;
66
        this.style = style;
67
    }
68

    
69
    @Override
70
    public void setEntity(Media entity) {
71
        super.setEntity(entity);
72
        if(singleMediaRepresentationPart==null){
73
            singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
74
        }
75
        initIsAdvancedMediaView();
76
        showAdvancedView();
77
    }
78

    
79
    @Override
80
    public void handleEvent(Object eventSource){
81
        if(eventSource==textUri){
82
            textUri.setBackground(getPersistentBackground());
83
            URI uri = textUri.parseText();
84
            singleMediaRepresentationPart.setUri(uri);
85
            if(uri==null){
86
                uriBuffer=textUri.getText();
87
            }
88
            else{
89
                createImageElement(uri);
90
            }
91
        }
92
    }
93

    
94
    private void createImageElement(URI uri) {
95
        ImageInfo imageInfo;
96
        try {
97
            if(uri == null){
98
                return;
99
            }
100
            //first check if uri refers to an actual (non-image) file
101
            UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
102
            imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
103
            singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
104
            if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
105
            	ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
106
                image.setHeight(imageInfo.getHeight());
107
                image.setWidth(imageInfo.getWidth());
108
            }
109
            singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
110
            singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
111
            element_image = formFactory.createImageElement(parentFormElement, uri, style);
112
            element_image.initImageUri(uri);
113
            element_image.loadImage();
114
    		disposeErrorLabel();
115
        } catch (Exception e){
116
        	if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
117
        		disposeErrorLabel();
118
        		handleException(uri, "No preview available for this file type");
119
        	}
120
        	else{
121
        		disposeErrorLabel();
122
        		handleException(uri, "No file found");
123
        	}
124
        }
125
    }
126

    
127
	private void disposeErrorLabel() {
128
		if(lblNoImage!=null){
129
		    lblNoImage.dispose();
130
		}
131
		lblNoImage = null;
132
	}
133

    
134
    public void toggleAdvancedMediaView() {
135
        if (getEntity().getRepresentations() != null
136
                && (getEntity().getRepresentations().size() > 1 ||
137
                        (getEntity().getRepresentations().size() == 1
138
                        && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
139
            MessagingUtils.informationDialog("Toggling not possible",
140
                    "Media consists of multiple representations or representatio parts");
141
            // toggling is only possible if there are no more than one
142
            // MediaRepresentation resp. MediaRepresentationParts
143
            return;
144
        }
145
        isAdvancedMediaView = !isAdvancedMediaView;
146
        showAdvancedView();
147
        reflowParentScrolledForm(true);
148
    }
149

    
150
    private void showAdvancedView() {
151
        if(isAdvancedMediaView){
152
            if(textUri!=null){
153
                removeElementsAndControls(textUri);
154
            }
155
            if(element_image!=null){
156
                element_image.dispose();
157
                element_image = null;
158
            }
159
            section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
160
            section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
161
            section_mediaRepresentation.setEntity(getEntity());
162
            //set buffered uri as text if uri had parsing problems in simple view
163
            if(uriBuffer!=null){
164
                section_mediaRepresentation.getLayoutComposite().getChildren();
165
            }
166
        }
167
        else{
168
            if(section_mediaRepresentation!=null){
169
                removeElementsAndControls(section_mediaRepresentation);
170
            }
171
            textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
172
            URI uri = singleMediaRepresentationPart.getUri();
173
            textUri.setParsedText(uri);
174
            //set buffered uri as text if uri had parsing problems in advanced view
175
            if(uri==null && uriBuffer!=null){
176
                textUri.setText(uriBuffer);
177
                textUri.parseText();
178
            }
179
            textUri.getLayoutComposite().layout();
180

    
181
            createImageElement(singleMediaRepresentationPart.getUri());
182
        }
183
    }
184

    
185
    private void initIsAdvancedMediaView(){
186
        if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
187
            isAdvancedMediaView = true;
188
            return;
189
        }
190
        Set<MediaRepresentation> representations = getEntity().getRepresentations();
191
        if(representations.size()>1){
192
            isAdvancedMediaView = true;
193
            return;
194
        }
195
        if(representations.size()==1){
196
            List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
197
            if(parts.size()>1){
198
                isAdvancedMediaView =  true;
199
                return;
200
            }
201
        }
202
        isAdvancedMediaView =  false;
203
    }
204

    
205
    private void handleException(URI uri, String labelText) {
206
        if(element_image!=null){
207
            element_image.unloadImage();
208
            element_image.loadImage();
209
            element_image.dispose();
210
        }
211
        if(lblNoImage==null){
212
			lblNoImage = formFactory.createLabel(getLayoutComposite(), labelText);
213
            lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
214
            lblNoImage.setAlignment(SWT.CENTER);
215
        }
216
    }
217

    
218
    public boolean isAdvancedMediaView() {
219
        return isAdvancedMediaView;
220
    }
221

    
222
    public void setUriBuffer(String uriBuffer) {
223
        this.uriBuffer = uriBuffer;
224
    }
225

    
226
    public String getUriBuffer() {
227
        return uriBuffer;
228
    }
229

    
230
}
(3-3/10)