Project

General

Profile

Download (7.16 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.io.IOException;
13
import java.net.URI;
14
import java.util.List;
15
import java.util.Set;
16

    
17
import org.apache.http.HttpException;
18

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

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

    
41
    private UriWithLabelElement textUri;
42
    private MediaRepresentationSection section_mediaRepresentation;
43

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

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

    
55
    private final Runnable postRunnable = new Runnable(){
56
        @Override
57
        public void run() {
58
            AbstractUtility.reflowDetailsViewer();
59
            AbstractUtility.reflowSupplementalViewer();
60
        }
61
    };
62

    
63
    public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
64
        super(formFactory, formElement);
65
    }
66

    
67
    @Override
68
    protected void createControls(ICdmFormElement formElement, Media entity, int style) {
69
        this.parentFormElement = formElement;
70
        this.style = style;
71
    }
72

    
73
    @Override
74
    public void setEntity(Media entity) {
75
        super.setEntity(entity);
76
        if(singleMediaRepresentationPart==null){
77
            singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
78
        }
79
        initIsAdvancedMediaView();
80
        showAdvancedView();
81
    }
82

    
83
    @Override
84
    public void handleEvent(Object eventSource){
85
        if(eventSource==textUri){
86
            textUri.setBackground(getPersistentBackground());
87
            URI uri = textUri.parseText();
88
            singleMediaRepresentationPart.setUri(uri);
89
            if(uri==null){
90
                uriBuffer=textUri.getText();
91
            }
92
            try {
93
                loadImage(singleMediaRepresentationPart.getUri(), true);
94
            } catch (Exception e) {
95
                handleException(e);
96
            }
97
        }
98
    }
99

    
100
    public void toggleAdvancedMediaView() {
101
        if (getEntity().getRepresentations() != null
102
                && (getEntity().getRepresentations().size() > 1 ||
103
                        (getEntity().getRepresentations().size() == 1
104
                        && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
105
            MessagingUtils.informationDialog("Toggling not possible",
106
                    "Media has consists of multiple representations or representatio parts");
107
            // toggling is only possible if there are no more than one
108
            // MediaRepresentation resp. MediaRepresentationParts
109
            return;
110
        }
111
        isAdvancedMediaView = !isAdvancedMediaView;
112
        showAdvancedView();
113
        reflowParentScrolledForm(true);
114
    }
115

    
116
    private void showAdvancedView() {
117
        if(isAdvancedMediaView){
118
            if(textUri!=null){
119
                removeElementsAndControls(textUri);
120
            }
121
            if(element_image!=null){
122
                element_image.dispose();
123
                element_image = null;
124
            }
125
            section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
126
            section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
127
            section_mediaRepresentation.setEntity(getEntity());
128
            //set buffered uri as text if uri had parsing problems in simple view
129
            if(uriBuffer!=null){
130
                section_mediaRepresentation.getLayoutComposite().getChildren();
131
            }
132
        }
133
        else{
134
            if(section_mediaRepresentation!=null){
135
                removeElementsAndControls(section_mediaRepresentation);
136
            }
137
            textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
138
            URI uri = singleMediaRepresentationPart.getUri();
139
            textUri.setParsedText(uri);
140
            //set buffered uri as text if uri had parsing problems in advanced view
141
            if(uri==null && uriBuffer!=null){
142
                textUri.setText(uriBuffer);
143
                textUri.parseText();
144
            }
145
            textUri.getLayoutComposite().layout();
146

    
147
            element_image = formFactory.createImageElement(parentFormElement, null, style);
148
            try {
149
                loadImage(singleMediaRepresentationPart.getUri(), false);
150
            } catch (Exception e) {
151
                handleException(e);
152
            }
153
        }
154
    }
155

    
156
    private void initIsAdvancedMediaView(){
157
        if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
158
            isAdvancedMediaView = true;
159
            return;
160
        }
161
        Set<MediaRepresentation> representations = getEntity().getRepresentations();
162
        if(representations.size()>1){
163
            isAdvancedMediaView = true;
164
            return;
165
        }
166
        if(representations.size()==1){
167
            List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
168
            if(parts.size()>1){
169
                isAdvancedMediaView =  true;
170
                return;
171
            }
172
        }
173
        isAdvancedMediaView =  false;
174
    }
175

    
176
    private void loadImage(URI uri, boolean updateDimensions) throws IOException, HttpException{
177
        element_image.initImageUri(uri);
178
        element_image.loadImage(postRunnable);
179
        if(uri == null){
180
            return;
181
        }
182
    }
183

    
184
    private void handleException(Exception e) {
185
        element_image.unloadImage(postRunnable);
186
        element_image.loadImage(new Runnable(){
187
            @Override
188
            public void run() {
189
                AbstractUtility.reflowDetailsViewer();
190
                AbstractUtility.reflowSupplementalViewer();
191
            }
192
        });
193
    }
194
    public boolean isAdvancedMediaView() {
195
        return isAdvancedMediaView;
196
    }
197

    
198
    public void setUriBuffer(String uriBuffer) {
199
        this.uriBuffer = uriBuffer;
200
    }
201

    
202
    public String getUriBuffer() {
203
        return uriBuffer;
204
    }
205

    
206
}
(3-3/10)