1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2017 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.editor.view.media;
|
11
|
|
12
|
import java.io.InputStream;
|
13
|
import java.net.URI;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
|
18
|
import org.eclipse.jface.viewers.LabelProvider;
|
19
|
import org.eclipse.jface.viewers.StyledString;
|
20
|
import org.eclipse.swt.SWT;
|
21
|
import org.eclipse.swt.graphics.GC;
|
22
|
import org.eclipse.swt.graphics.Image;
|
23
|
import org.eclipse.swt.widgets.Display;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.UriUtils;
|
26
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
27
|
import eu.etaxonomy.cdm.model.media.Media;
|
28
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
29
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
30
|
import eu.etaxonomy.cdm.model.media.MediaUtils;
|
31
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
32
|
|
33
|
/**
|
34
|
* @author k.luther
|
35
|
* @date 11.10.2017
|
36
|
*
|
37
|
*/
|
38
|
public class MediaViewLabelProvider extends LabelProvider implements IStyledLabelProvider {
|
39
|
|
40
|
/**
|
41
|
* {@inheritDoc}
|
42
|
*/
|
43
|
@Override
|
44
|
public String getText(Object element) {
|
45
|
return getStyledText(element).getString();
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public StyledString getStyledText(Object element) {
|
50
|
if (element instanceof IdentifiableEntity) {
|
51
|
IdentifiableEntity<?> identifiableEntity= (IdentifiableEntity<?>) element;
|
52
|
if(identifiableEntity.getId()==0){
|
53
|
return new StyledString(identifiableEntity.generateTitle());
|
54
|
}
|
55
|
else{
|
56
|
return new StyledString(identifiableEntity.getTitleCache());
|
57
|
}
|
58
|
}
|
59
|
return null;
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public Image getImage(Object element) {
|
64
|
Image image = null;
|
65
|
int scale = 50;
|
66
|
if (element instanceof Media && PreferencesUtil.isShowMediaPreview()){
|
67
|
Set<MediaRepresentation> representations = ((Media)element).getRepresentations();
|
68
|
if(representations.iterator().hasNext()){
|
69
|
MediaRepresentation rep = null;
|
70
|
|
71
|
MediaRepresentationPart smallestPart = null;
|
72
|
rep = MediaUtils.findBestMatchingRepresentation((Media)element, null, null, scale, scale, null);
|
73
|
List<MediaRepresentationPart> parts = rep.getParts();
|
74
|
for (MediaRepresentationPart part: parts){
|
75
|
if (smallestPart != null){
|
76
|
if (smallestPart.getSize() != null && part.getSize() != null) {
|
77
|
if (smallestPart.getSize() > part.getSize()){
|
78
|
smallestPart = part;
|
79
|
}
|
80
|
}
|
81
|
}else{
|
82
|
smallestPart = part;
|
83
|
}
|
84
|
|
85
|
}
|
86
|
if (smallestPart != null){
|
87
|
URI uri = smallestPart.getUri();
|
88
|
if (uri == null){
|
89
|
return null;
|
90
|
}
|
91
|
InputStream imageStream;
|
92
|
try {
|
93
|
imageStream = UriUtils.getInputStream(uri);
|
94
|
image = new Image(Display.getCurrent(), imageStream);
|
95
|
int height = image.getBounds().height;
|
96
|
int width = image.getBounds().width;
|
97
|
|
98
|
if (height>width){
|
99
|
width = width/(height/scale);
|
100
|
height = scale;
|
101
|
}else{
|
102
|
height = height/(width/scale);
|
103
|
width = scale;
|
104
|
}
|
105
|
Image scaled = new Image(Display.getDefault(), scale, scale);
|
106
|
|
107
|
GC gc = new GC(scaled);
|
108
|
gc.setAntialias(SWT.ON);
|
109
|
gc.setInterpolation(SWT.HIGH);
|
110
|
|
111
|
|
112
|
gc.drawImage(image, 0, 0,
|
113
|
image.getBounds().width, image.getBounds().height,
|
114
|
0, 0, width, height);
|
115
|
|
116
|
gc.dispose();
|
117
|
image.dispose(); // don't forget about me!
|
118
|
return scaled;
|
119
|
|
120
|
} catch (Exception e) {
|
121
|
// TODO Auto-generated catch block
|
122
|
return null;
|
123
|
}
|
124
|
|
125
|
}else{
|
126
|
return null;
|
127
|
}
|
128
|
|
129
|
}
|
130
|
|
131
|
}
|
132
|
return image;
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
public void dispose() {
|
137
|
// garbage collection system resources
|
138
|
|
139
|
}
|
140
|
|
141
|
|
142
|
|
143
|
}
|