1 |
a904e4d0
|
Katja Luther
|
// $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 |
e8e82804
|
Katja Luther
|
import eu.etaxonomy.cdm.model.media.MediaUtils;
|
31 |
a904e4d0
|
Katja Luther
|
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 |
fa07dfed
|
Patrick Plitzner
|
/**
|
41 |
|
|
* {@inheritDoc}
|
42 |
|
|
*/
|
43 |
|
|
@Override
|
44 |
|
|
public String getText(Object element) {
|
45 |
|
|
return getStyledText(element).getString();
|
46 |
|
|
}
|
47 |
|
|
|
48 |
a904e4d0
|
Katja Luther
|
@Override
|
49 |
|
|
public StyledString getStyledText(Object element) {
|
50 |
|
|
if (element instanceof IdentifiableEntity) {
|
51 |
6d7fe32f
|
Andreas Müller
|
IdentifiableEntity<?> identifiableEntity= (IdentifiableEntity<?>) element;
|
52 |
fa07dfed
|
Patrick Plitzner
|
if(identifiableEntity.getId()==0){
|
53 |
|
|
return new StyledString(identifiableEntity.generateTitle());
|
54 |
|
|
}
|
55 |
|
|
else{
|
56 |
|
|
return new StyledString(identifiableEntity.getTitleCache());
|
57 |
|
|
}
|
58 |
a904e4d0
|
Katja Luther
|
}
|
59 |
|
|
return null;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
@Override
|
63 |
|
|
public Image getImage(Object element) {
|
64 |
|
|
Image image = null;
|
65 |
e8e82804
|
Katja Luther
|
int scale = 50;
|
66 |
a904e4d0
|
Katja Luther
|
if (element instanceof Media && PreferencesUtil.isShowMediaPreview()){
|
67 |
|
|
Set<MediaRepresentation> representations = ((Media)element).getRepresentations();
|
68 |
|
|
if(representations.iterator().hasNext()){
|
69 |
e8e82804
|
Katja Luther
|
MediaRepresentation rep = null;
|
70 |
|
|
|
71 |
29c6624e
|
Katja Luther
|
MediaRepresentationPart smallestPart = null;
|
72 |
e8e82804
|
Katja Luther
|
rep = MediaUtils.findBestMatchingRepresentation((Media)element, null, null, scale, scale, null);
|
73 |
|
|
List<MediaRepresentationPart> parts = rep.getParts();
|
74 |
29c6624e
|
Katja Luther
|
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 |
179110e9
|
Katja Luther
|
if (smallestPart != null){
|
87 |
|
|
URI uri = smallestPart.getUri();
|
88 |
|
|
if (uri == null){
|
89 |
|
|
return null;
|
90 |
a904e4d0
|
Katja Luther
|
}
|
91 |
179110e9
|
Katja Luther
|
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 |
a904e4d0
|
Katja Luther
|
|
107 |
179110e9
|
Katja Luther
|
GC gc = new GC(scaled);
|
108 |
|
|
gc.setAntialias(SWT.ON);
|
109 |
|
|
gc.setInterpolation(SWT.HIGH);
|
110 |
a904e4d0
|
Katja Luther
|
|
111 |
|
|
|
112 |
179110e9
|
Katja Luther
|
gc.drawImage(image, 0, 0,
|
113 |
|
|
image.getBounds().width, image.getBounds().height,
|
114 |
|
|
0, 0, width, height);
|
115 |
a904e4d0
|
Katja Luther
|
|
116 |
179110e9
|
Katja Luther
|
gc.dispose();
|
117 |
|
|
image.dispose(); // don't forget about me!
|
118 |
|
|
return scaled;
|
119 |
a904e4d0
|
Katja Luther
|
|
120 |
179110e9
|
Katja Luther
|
} catch (Exception e) {
|
121 |
|
|
// TODO Auto-generated catch block
|
122 |
|
|
return null;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
}else{
|
126 |
|
|
return null;
|
127 |
a904e4d0
|
Katja Luther
|
}
|
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 |
|
|
}
|