3 * Copyright (C) 2017 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.media
;
12 import java
.io
.InputStream
;
14 import java
.util
.List
;
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
;
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
.taxeditor
.preference
.PreferencesUtil
;
37 public class MediaViewLabelProvider
extends LabelProvider
implements IStyledLabelProvider
{
43 public String
getText(Object element
) {
44 return getStyledText(element
).getString();
48 public StyledString
getStyledText(Object element
) {
49 if (element
instanceof IdentifiableEntity
) {
50 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) element
;
51 if(identifiableEntity
.getId()==0){
52 return new StyledString(identifiableEntity
.generateTitle());
55 return new StyledString(identifiableEntity
.getTitleCache());
62 public Image
getImage(Object element
) {
64 if (element
instanceof Media
&& PreferencesUtil
.isShowMediaPreview()){
65 Set
<MediaRepresentation
> representations
= ((Media
)element
).getRepresentations();
66 if(representations
.iterator().hasNext()){
67 MediaRepresentation rep
= representations
.iterator().next();
68 List
<MediaRepresentationPart
> parts
= rep
.getParts();
69 MediaRepresentationPart smallestPart
= null;
70 for (MediaRepresentationPart part
: parts
){
71 if (smallestPart
!= null){
72 if (smallestPart
.getSize() != null && part
.getSize() != null) {
73 if (smallestPart
.getSize() > part
.getSize()){
82 URI uri
= parts
.get(0).getUri();
86 InputStream imageStream
;
88 imageStream
= UriUtils
.getInputStream(uri
);
89 image
= new Image(Display
.getCurrent(), imageStream
);
90 int height
= image
.getBounds().height
;
91 int width
= image
.getBounds().width
;
94 width
= width
/(height
/scale
);
97 height
= height
/(width
/scale
);
100 Image scaled
= new Image(Display
.getDefault(), scale
, scale
);
102 GC gc
= new GC(scaled
);
103 gc
.setAntialias(SWT
.ON
);
104 gc
.setInterpolation(SWT
.HIGH
);
107 gc
.drawImage(image
, 0, 0,
108 image
.getBounds().width
, image
.getBounds().height
,
109 0, 0, width
, height
);
112 image
.dispose(); // don't forget about me!
115 } catch (Exception e
) {
116 // TODO Auto-generated catch block
127 public void dispose() {
128 // garbage collection system resources