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.CdmImageInfo;
|
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
|
try {
|
69
|
//first check if uri refers to an actual (non-image) file
|
70
|
UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
|
71
|
CdmImageInfo imageInfo = CdmImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
|
72
|
MediaDetailElement.this.getLayoutComposite().getDisplay().asyncExec(()->{
|
73
|
singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
|
74
|
if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
|
75
|
ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
|
76
|
image.setHeight(imageInfo.getHeight());
|
77
|
image.setWidth(imageInfo.getWidth());
|
78
|
}
|
79
|
if(singleMediaRepresentationPart.getMediaRepresentation()!=null){
|
80
|
singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
|
81
|
singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
|
82
|
}
|
83
|
disposeImage();
|
84
|
if (!parentFormElement.getLayoutComposite().isDisposed()) {
|
85
|
element_image = formFactory.createImageElement(parentFormElement, uri, style);
|
86
|
StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
|
87
|
try {
|
88
|
element_image.initImageUri(uri);
|
89
|
} catch (IOException | HttpException e) {
|
90
|
exception(e);
|
91
|
}
|
92
|
element_image.loadImage();
|
93
|
disposeErrorLabel();
|
94
|
}
|
95
|
});
|
96
|
} catch (Exception e){
|
97
|
exception(e);
|
98
|
}
|
99
|
return Status.OK_STATUS;
|
100
|
}
|
101
|
}
|
102
|
|
103
|
private UriWithLabelElement textUri;
|
104
|
private MediaRepresentationSection section_mediaRepresentation;
|
105
|
|
106
|
private boolean isAdvancedMediaView;
|
107
|
private MediaRepresentationPart singleMediaRepresentationPart;
|
108
|
private ICdmFormElement parentFormElement;
|
109
|
private int style;
|
110
|
|
111
|
/**
|
112
|
* Used to store the URI even if it is invalid and thus cannot be stored in CDM
|
113
|
*/
|
114
|
private String uriBuffer;
|
115
|
private ImageElement element_image;
|
116
|
private Label lblNoImage;
|
117
|
|
118
|
public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
|
119
|
super(formFactory, formElement);
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
protected void createControls(ICdmFormElement formElement, Media entity, int style) {
|
124
|
|
125
|
this.parentFormElement = formElement;
|
126
|
this.style = style;
|
127
|
|
128
|
}
|
129
|
|
130
|
@Override
|
131
|
public void setEntity(Media entity) {
|
132
|
super.setEntity(entity);
|
133
|
if(singleMediaRepresentationPart==null){
|
134
|
singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
|
135
|
}
|
136
|
initIsAdvancedMediaView();
|
137
|
showAdvancedView();
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public void handleEvent(Object eventSource){
|
142
|
if(eventSource==textUri){
|
143
|
textUri.setBackground(getPersistentBackground());
|
144
|
URI uri = textUri.parseText();
|
145
|
singleMediaRepresentationPart.setUri(uri);
|
146
|
if(uri==null){
|
147
|
uriBuffer=textUri.getText();
|
148
|
}
|
149
|
else{
|
150
|
new LoadImageJob(uri, LOAD_IMAGE).schedule();
|
151
|
}
|
152
|
}
|
153
|
}
|
154
|
|
155
|
private void exception(Exception e){
|
156
|
if(!getLayoutComposite().isDisposed()){
|
157
|
getLayoutComposite().getDisplay().asyncExec(()->{
|
158
|
String exceptionString;
|
159
|
if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
|
160
|
disposeErrorLabel();
|
161
|
exceptionString = Messages.MediaDetailElement_NO_PREVIEW;
|
162
|
}
|
163
|
else{
|
164
|
disposeErrorLabel();
|
165
|
exceptionString = Messages.MediaDetailElement_NO_FILE_FOUND;
|
166
|
}
|
167
|
disposeImage();
|
168
|
if(lblNoImage==null){
|
169
|
lblNoImage = formFactory.createLabel(getLayoutComposite(), exceptionString);
|
170
|
lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
171
|
lblNoImage.setAlignment(SWT.CENTER);
|
172
|
}
|
173
|
StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
|
174
|
});
|
175
|
}
|
176
|
}
|
177
|
|
178
|
private void disposeImage(){
|
179
|
if(element_image!=null){
|
180
|
element_image.dispose();
|
181
|
element_image = null;
|
182
|
}
|
183
|
}
|
184
|
|
185
|
private void disposeErrorLabel() {
|
186
|
if(lblNoImage!=null){
|
187
|
lblNoImage.dispose();
|
188
|
}
|
189
|
lblNoImage = null;
|
190
|
}
|
191
|
|
192
|
public void toggleAdvancedMediaView() {
|
193
|
if (getEntity().getRepresentations() != null
|
194
|
&& (getEntity().getRepresentations().size() > 1 ||
|
195
|
(getEntity().getRepresentations().size() == 1
|
196
|
&& getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
|
197
|
MessagingUtils.informationDialog(Messages.MediaDetailElement_TOGGLE_NOT_POSSIBLE_TITLE,
|
198
|
Messages.MediaDetailElement_TOGGLE_NOT_POSSIBLE_MESSAGE);
|
199
|
// toggling is only possible if there are no more than one
|
200
|
// MediaRepresentation resp. MediaRepresentationParts
|
201
|
return;
|
202
|
}
|
203
|
isAdvancedMediaView = !isAdvancedMediaView;
|
204
|
showAdvancedView();
|
205
|
StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
|
206
|
}
|
207
|
|
208
|
private void showAdvancedView() {
|
209
|
|
210
|
if(isAdvancedMediaView){
|
211
|
if(textUri!=null){
|
212
|
removeElementsAndControls(textUri);
|
213
|
}
|
214
|
disposeImage();
|
215
|
section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, StoreUtil.getSectionStyle(MediaRepresentationSection.class, getEntity().getClass().getCanonicalName()));
|
216
|
section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
217
|
section_mediaRepresentation.setEntity(getEntity());
|
218
|
//set buffered uri as text if uri had parsing problems in simple view
|
219
|
if(uriBuffer!=null){
|
220
|
section_mediaRepresentation.getLayoutComposite().getChildren();
|
221
|
}
|
222
|
}
|
223
|
else{
|
224
|
if(section_mediaRepresentation!=null){
|
225
|
removeElementsAndControls(section_mediaRepresentation);
|
226
|
}
|
227
|
textUri = formFactory.createUriWithLabelElement(parentFormElement, Messages.MediaDetailElement_Media_URI, null, style);
|
228
|
URI uri = singleMediaRepresentationPart.getUri();
|
229
|
textUri.setParsedText(uri);
|
230
|
//set buffered uri as text if uri had parsing problems in advanced view
|
231
|
if(uri==null && uriBuffer!=null){
|
232
|
textUri.setText(uriBuffer);
|
233
|
textUri.parseText();
|
234
|
}
|
235
|
textUri.getLayoutComposite().layout();
|
236
|
|
237
|
new LoadImageJob(singleMediaRepresentationPart.getUri(), LOAD_IMAGE).schedule();
|
238
|
}
|
239
|
}
|
240
|
|
241
|
private void initIsAdvancedMediaView(){
|
242
|
if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
|
243
|
isAdvancedMediaView = true;
|
244
|
return;
|
245
|
}
|
246
|
Set<MediaRepresentation> representations = getEntity().getRepresentations();
|
247
|
if(representations.size()>1){
|
248
|
isAdvancedMediaView = true;
|
249
|
return;
|
250
|
}
|
251
|
if(representations.size()==1){
|
252
|
List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
|
253
|
if(parts.size()>1){
|
254
|
isAdvancedMediaView = true;
|
255
|
return;
|
256
|
}
|
257
|
}
|
258
|
isAdvancedMediaView = false;
|
259
|
}
|
260
|
|
261
|
public boolean isAdvancedMediaView() {
|
262
|
return isAdvancedMediaView;
|
263
|
}
|
264
|
|
265
|
public void setUriBuffer(String uriBuffer) {
|
266
|
this.uriBuffer = uriBuffer;
|
267
|
}
|
268
|
|
269
|
public String getUriBuffer() {
|
270
|
return uriBuffer;
|
271
|
}
|
272
|
|
273
|
}
|