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
.cdm
.model
.media
.MediaUtils
;
31 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
38 public class MediaViewLabelProvider
extends LabelProvider
implements IStyledLabelProvider
{
44 public String
getText(Object element
) {
45 return getStyledText(element
).getString();
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());
56 return new StyledString(identifiableEntity
.getTitleCache());
63 public Image
getImage(Object element
) {
66 if (element
instanceof Media
&& PreferencesUtil
.isShowMediaPreview()){
67 Set
<MediaRepresentation
> representations
= ((Media
)element
).getRepresentations();
68 if(representations
.iterator().hasNext()){
69 MediaRepresentation rep
= null;
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()){
86 if (smallestPart
!= null){
87 URI uri
= smallestPart
.getUri();
91 InputStream imageStream
;
93 imageStream
= UriUtils
.getInputStream(uri
);
94 image
= new Image(Display
.getCurrent(), imageStream
);
95 int height
= image
.getBounds().height
;
96 int width
= image
.getBounds().width
;
99 width
= width
/(height
/scale
);
102 height
= height
/(width
/scale
);
105 Image scaled
= new Image(Display
.getDefault(), scale
, scale
);
107 GC gc
= new GC(scaled
);
108 gc
.setAntialias(SWT
.ON
);
109 gc
.setInterpolation(SWT
.HIGH
);
112 gc
.drawImage(image
, 0, 0,
113 image
.getBounds().width
, image
.getBounds().height
,
114 0, 0, width
, height
);
117 image
.dispose(); // don't forget about me!
120 } catch (Exception e
) {
121 // TODO Auto-generated catch block
136 public void dispose() {
137 // garbage collection system resources