Project

General

Profile

Download (7.7 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2014 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.ui.section.media;
11

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

    
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.widgets.Label;
18

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

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

    
42
    private UriWithLabelElement textUri;
43
    private MediaRepresentationSection section_mediaRepresentation;
44

    
45
    private boolean isAdvancedMediaView;
46
    private MediaRepresentationPart singleMediaRepresentationPart;
47
    private ICdmFormElement parentFormElement;
48
    private int style;
49

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

    
57
    public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
58
        super(formFactory, formElement);
59
    }
60

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

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

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

    
92
    private void createImageElement(URI uri) {
93
        ImageInfo imageInfo;
94
        try {
95
            if(uri == null){
96
                return;
97
            }
98
            imageInfo = ImageInfo.NewInstance(uri, 10000);
99
            singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
100
            if(singleMediaRepresentationPart instanceof ImageFile){
101
                ((ImageFile) singleMediaRepresentationPart).setHeight(imageInfo.getHeight());
102
                ((ImageFile) singleMediaRepresentationPart).setWidth(imageInfo.getWidth());
103
            }
104
            singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
105
            singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
106
            element_image = formFactory.createImageElement(parentFormElement, uri, style);
107
            element_image.initImageUri(uri);
108
            element_image.loadImage();
109
            if(lblNoImage!=null){
110
                lblNoImage.dispose();
111
            }
112
            lblNoImage = null;
113
        } catch (Exception e) {
114
            handleException();
115
        }
116
    }
117

    
118
    public void toggleAdvancedMediaView() {
119
        if (getEntity().getRepresentations() != null
120
                && (getEntity().getRepresentations().size() > 1 ||
121
                        (getEntity().getRepresentations().size() == 1
122
                        && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
123
            MessagingUtils.informationDialog("Toggling not possible",
124
                    "Media has consists of multiple representations or representatio parts");
125
            // toggling is only possible if there are no more than one
126
            // MediaRepresentation resp. MediaRepresentationParts
127
            return;
128
        }
129
        isAdvancedMediaView = !isAdvancedMediaView;
130
        showAdvancedView();
131
        reflowParentScrolledForm(true);
132
    }
133

    
134
    private void showAdvancedView() {
135
        if(isAdvancedMediaView){
136
            if(textUri!=null){
137
                removeElementsAndControls(textUri);
138
            }
139
            if(element_image!=null){
140
                element_image.dispose();
141
                element_image = null;
142
            }
143
            section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
144
            section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
145
            section_mediaRepresentation.setEntity(getEntity());
146
            //set buffered uri as text if uri had parsing problems in simple view
147
            if(uriBuffer!=null){
148
                section_mediaRepresentation.getLayoutComposite().getChildren();
149
            }
150
        }
151
        else{
152
            if(section_mediaRepresentation!=null){
153
                removeElementsAndControls(section_mediaRepresentation);
154
            }
155
            textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
156
            URI uri = singleMediaRepresentationPart.getUri();
157
            textUri.setParsedText(uri);
158
            //set buffered uri as text if uri had parsing problems in advanced view
159
            if(uri==null && uriBuffer!=null){
160
                textUri.setText(uriBuffer);
161
                textUri.parseText();
162
            }
163
            textUri.getLayoutComposite().layout();
164

    
165
            createImageElement(singleMediaRepresentationPart.getUri());
166
        }
167
    }
168

    
169
    private void initIsAdvancedMediaView(){
170
        if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
171
            isAdvancedMediaView = true;
172
            return;
173
        }
174
        Set<MediaRepresentation> representations = getEntity().getRepresentations();
175
        if(representations.size()>1){
176
            isAdvancedMediaView = true;
177
            return;
178
        }
179
        if(representations.size()==1){
180
            List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
181
            if(parts.size()>1){
182
                isAdvancedMediaView =  true;
183
                return;
184
            }
185
        }
186
        isAdvancedMediaView =  false;
187
    }
188

    
189
    private void handleException() {
190
        if(element_image!=null){
191
            element_image.unloadImage();
192
            element_image.loadImage();
193
            element_image.dispose();
194
        }
195
        if(lblNoImage==null){
196
            lblNoImage = formFactory.createLabel(getLayoutComposite(), "No Image found");
197
            lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
198
            lblNoImage.setAlignment(SWT.CENTER);
199
        }
200
    }
201

    
202
    public boolean isAdvancedMediaView() {
203
        return isAdvancedMediaView;
204
    }
205

    
206
    public void setUriBuffer(String uriBuffer) {
207
        this.uriBuffer = uriBuffer;
208
    }
209

    
210
    public String getUriBuffer() {
211
        return uriBuffer;
212
    }
213

    
214
}
(3-3/10)