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.media.ImageFile;
|
22
|
import eu.etaxonomy.cdm.model.media.Media;
|
23
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
24
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
25
|
import eu.etaxonomy.cdm.model.media.MediaUtils;
|
26
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
27
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
28
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
29
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
30
|
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
|
31
|
import eu.etaxonomy.taxeditor.ui.element.ImageElement;
|
32
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
33
|
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
|
34
|
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
|
35
|
|
36
|
/**
|
37
|
* @author pplitzner
|
38
|
* @date 25.08.2014
|
39
|
*
|
40
|
*/
|
41
|
public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
|
42
|
|
43
|
private UriWithLabelElement textUri;
|
44
|
private MediaRepresentationSection section_mediaRepresentation;
|
45
|
|
46
|
private boolean isAdvancedMediaView;
|
47
|
private MediaRepresentationPart singleMediaRepresentationPart;
|
48
|
private ICdmFormElement parentFormElement;
|
49
|
private int style;
|
50
|
|
51
|
/**
|
52
|
* Used to store the URI even if it is invalid and thus cannot be stored in CDM
|
53
|
*/
|
54
|
private String uriBuffer;
|
55
|
private ImageElement element_image;
|
56
|
private Label lblNoImage;
|
57
|
|
58
|
public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
|
59
|
super(formFactory, formElement);
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
protected void createControls(ICdmFormElement formElement, Media entity, int style) {
|
64
|
this.parentFormElement = formElement;
|
65
|
this.style = style;
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public void setEntity(Media entity) {
|
70
|
super.setEntity(entity);
|
71
|
if(singleMediaRepresentationPart==null){
|
72
|
singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
|
73
|
}
|
74
|
initIsAdvancedMediaView();
|
75
|
showAdvancedView();
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public void handleEvent(Object eventSource){
|
80
|
if(eventSource==textUri){
|
81
|
textUri.setBackground(getPersistentBackground());
|
82
|
URI uri = textUri.parseText();
|
83
|
singleMediaRepresentationPart.setUri(uri);
|
84
|
if(uri==null){
|
85
|
uriBuffer=textUri.getText();
|
86
|
}
|
87
|
else{
|
88
|
createImageElement(uri);
|
89
|
}
|
90
|
}
|
91
|
}
|
92
|
|
93
|
private void createImageElement(URI uri) {
|
94
|
ImageInfo imageInfo;
|
95
|
try {
|
96
|
if(uri == null){
|
97
|
return;
|
98
|
}
|
99
|
//first check if uri refers to an actual (non-image) file
|
100
|
UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
|
101
|
imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
|
102
|
singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
|
103
|
if(singleMediaRepresentationPart instanceof ImageFile){
|
104
|
((ImageFile) singleMediaRepresentationPart).setHeight(imageInfo.getHeight());
|
105
|
((ImageFile) singleMediaRepresentationPart).setWidth(imageInfo.getWidth());
|
106
|
}
|
107
|
singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
|
108
|
singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
|
109
|
element_image = formFactory.createImageElement(parentFormElement, uri, style);
|
110
|
element_image.initImageUri(uri);
|
111
|
element_image.loadImage();
|
112
|
disposeErrorLabel();
|
113
|
} catch (Exception e){
|
114
|
if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
|
115
|
disposeErrorLabel();
|
116
|
handleException(uri, "No preview available for this file type");
|
117
|
}
|
118
|
else{
|
119
|
disposeErrorLabel();
|
120
|
handleException(uri, "No file found");
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
|
125
|
private void disposeErrorLabel() {
|
126
|
if(lblNoImage!=null){
|
127
|
lblNoImage.dispose();
|
128
|
}
|
129
|
lblNoImage = null;
|
130
|
}
|
131
|
|
132
|
public void toggleAdvancedMediaView() {
|
133
|
if (getEntity().getRepresentations() != null
|
134
|
&& (getEntity().getRepresentations().size() > 1 ||
|
135
|
(getEntity().getRepresentations().size() == 1
|
136
|
&& getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
|
137
|
MessagingUtils.informationDialog("Toggling not possible",
|
138
|
"Media has consists of multiple representations or representatio parts");
|
139
|
// toggling is only possible if there are no more than one
|
140
|
// MediaRepresentation resp. MediaRepresentationParts
|
141
|
return;
|
142
|
}
|
143
|
isAdvancedMediaView = !isAdvancedMediaView;
|
144
|
showAdvancedView();
|
145
|
reflowParentScrolledForm(true);
|
146
|
}
|
147
|
|
148
|
private void showAdvancedView() {
|
149
|
if(isAdvancedMediaView){
|
150
|
if(textUri!=null){
|
151
|
removeElementsAndControls(textUri);
|
152
|
}
|
153
|
if(element_image!=null){
|
154
|
element_image.dispose();
|
155
|
element_image = null;
|
156
|
}
|
157
|
section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
|
158
|
section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
159
|
section_mediaRepresentation.setEntity(getEntity());
|
160
|
//set buffered uri as text if uri had parsing problems in simple view
|
161
|
if(uriBuffer!=null){
|
162
|
section_mediaRepresentation.getLayoutComposite().getChildren();
|
163
|
}
|
164
|
}
|
165
|
else{
|
166
|
if(section_mediaRepresentation!=null){
|
167
|
removeElementsAndControls(section_mediaRepresentation);
|
168
|
}
|
169
|
textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
|
170
|
URI uri = singleMediaRepresentationPart.getUri();
|
171
|
textUri.setParsedText(uri);
|
172
|
//set buffered uri as text if uri had parsing problems in advanced view
|
173
|
if(uri==null && uriBuffer!=null){
|
174
|
textUri.setText(uriBuffer);
|
175
|
textUri.parseText();
|
176
|
}
|
177
|
textUri.getLayoutComposite().layout();
|
178
|
|
179
|
createImageElement(singleMediaRepresentationPart.getUri());
|
180
|
}
|
181
|
}
|
182
|
|
183
|
private void initIsAdvancedMediaView(){
|
184
|
if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
|
185
|
isAdvancedMediaView = true;
|
186
|
return;
|
187
|
}
|
188
|
Set<MediaRepresentation> representations = getEntity().getRepresentations();
|
189
|
if(representations.size()>1){
|
190
|
isAdvancedMediaView = true;
|
191
|
return;
|
192
|
}
|
193
|
if(representations.size()==1){
|
194
|
List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
|
195
|
if(parts.size()>1){
|
196
|
isAdvancedMediaView = true;
|
197
|
return;
|
198
|
}
|
199
|
}
|
200
|
isAdvancedMediaView = false;
|
201
|
}
|
202
|
|
203
|
private void handleException(URI uri, String labelText) {
|
204
|
if(element_image!=null){
|
205
|
element_image.unloadImage();
|
206
|
element_image.loadImage();
|
207
|
element_image.dispose();
|
208
|
}
|
209
|
if(lblNoImage==null){
|
210
|
lblNoImage = formFactory.createLabel(getLayoutComposite(), labelText);
|
211
|
lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
212
|
lblNoImage.setAlignment(SWT.CENTER);
|
213
|
}
|
214
|
}
|
215
|
|
216
|
public boolean isAdvancedMediaView() {
|
217
|
return isAdvancedMediaView;
|
218
|
}
|
219
|
|
220
|
public void setUriBuffer(String uriBuffer) {
|
221
|
this.uriBuffer = uriBuffer;
|
222
|
}
|
223
|
|
224
|
public String getUriBuffer() {
|
225
|
return uriBuffer;
|
226
|
}
|
227
|
|
228
|
}
|