2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.media
;
11 import java
.io
.InputStream
;
12 import java
.util
.List
;
15 import org
.eclipse
.jface
.viewers
.DelegatingStyledCellLabelProvider
.IStyledLabelProvider
;
16 import org
.eclipse
.jface
.viewers
.LabelProvider
;
17 import org
.eclipse
.jface
.viewers
.StyledString
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.graphics
.GC
;
20 import org
.eclipse
.swt
.graphics
.Image
;
21 import org
.eclipse
.swt
.widgets
.Display
;
23 import eu
.etaxonomy
.cdm
.common
.URI
;
24 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
25 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
26 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
27 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
28 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
29 import eu
.etaxonomy
.cdm
.model
.media
.MediaUtils
;
30 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
36 public class MediaViewLabelProvider
extends LabelProvider
implements IStyledLabelProvider
{
42 public String
getText(Object element
) {
43 return getStyledText(element
).getString();
47 public StyledString
getStyledText(Object element
) {
48 if (element
instanceof IdentifiableEntity
) {
49 IdentifiableEntity
<?
> identifiableEntity
= (IdentifiableEntity
<?
>) element
;
50 if(identifiableEntity
.getId()==0){
51 return new StyledString(identifiableEntity
.generateTitle());
54 return new StyledString(identifiableEntity
.getTitleCache());
61 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
= null;
69 MediaRepresentationPart smallestPart
= null;
70 rep
= MediaUtils
.findBestMatchingRepresentation((Media
)element
, null, null, scale
, scale
, null, MediaUtils
.MissingValueStrategy
.MAX
);
71 List
<MediaRepresentationPart
> parts
= rep
.getParts();
72 for (MediaRepresentationPart part
: parts
){
73 if (smallestPart
!= null){
74 if (smallestPart
.getSize() != null && part
.getSize() != null) {
75 if (smallestPart
.getSize() > part
.getSize()){
84 if (smallestPart
!= null){
85 URI uri
= smallestPart
.getUri();
89 InputStream imageStream
;
91 imageStream
= UriUtils
.getInputStream(uri
);
92 image
= new Image(Display
.getCurrent(), imageStream
);
93 int height
= image
.getBounds().height
;
94 int width
= image
.getBounds().width
;
97 width
= width
/(height
/scale
);
100 height
= height
/(width
/scale
);
103 Image scaled
= new Image(Display
.getDefault(), scale
, scale
);
105 GC gc
= new GC(scaled
);
106 gc
.setAntialias(SWT
.ON
);
107 gc
.setInterpolation(SWT
.HIGH
);
110 gc
.drawImage(image
, 0, 0,
111 image
.getBounds().width
, image
.getBounds().height
,
112 0, 0, width
, height
);
115 image
.dispose(); // don't forget about me!
118 } catch (Exception e
) {
119 // TODO Auto-generated catch block
134 public void dispose() {
135 // garbage collection system resources