Project

General

Profile

Download (6.87 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.taxeditor.view.search.facet.term;
10

    
11
import java.io.InputStream;
12
import java.net.URI;
13
import java.util.Collection;
14
import java.util.HashSet;
15
import java.util.UUID;
16
import java.util.stream.Collectors;
17

    
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.core.runtime.jobs.Job;
22
import org.eclipse.jface.layout.GridDataFactory;
23
import org.eclipse.jface.resource.JFaceResources;
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.events.ControlAdapter;
26
import org.eclipse.swt.events.ControlEvent;
27
import org.eclipse.swt.events.PaintEvent;
28
import org.eclipse.swt.events.PaintListener;
29
import org.eclipse.swt.graphics.Image;
30
import org.eclipse.swt.layout.FillLayout;
31
import org.eclipse.swt.layout.GridData;
32
import org.eclipse.swt.layout.GridLayout;
33
import org.eclipse.swt.widgets.Composite;
34
import org.eclipse.swt.widgets.Display;
35
import org.eclipse.swt.widgets.Label;
36

    
37
import eu.etaxonomy.cdm.api.service.IMediaService;
38
import eu.etaxonomy.cdm.common.CdmUtils;
39
import eu.etaxonomy.cdm.common.UriUtils;
40
import eu.etaxonomy.cdm.model.media.Media;
41
import eu.etaxonomy.cdm.model.media.MediaUtils;
42
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
43
import eu.etaxonomy.cdm.persistence.dto.TermDto;
44
import eu.etaxonomy.taxeditor.store.CdmStore;
45
import eu.etaxonomy.taxeditor.view.search.facet.CheckBoxSearchResultComposite;
46

    
47
/**
48
 * @author pplitzner
49
 * @since Jan 23, 2019
50
 *
51
 */
52
public class TermSearchResultComposite extends CheckBoxSearchResultComposite<AbstractTermDto, TermSearchResult> {
53

    
54
    private Collection<Image> imageBuffer;
55

    
56
    public TermSearchResultComposite(TermSearchResult result, Composite parent, int style) {
57
        super(result, parent, style);
58
    }
59

    
60
    @Override
61
    public void dispose() {
62
        for (Image image : imageBuffer) {
63
            image.dispose();
64
            imageBuffer = null;
65
        }
66
        super.dispose();
67
    }
68

    
69
    @Override
70
    public Composite createContent(Composite parent) {
71
        Composite content = new Composite(this, SWT.NONE);
72
        content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
73
        GridLayout layout = new GridLayout(1, false);
74
        content.setLayout(layout);
75
        Label label = new Label(content, SWT.WRAP);
76
        label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
77
        label.setText(result.getContent().getRepresentation_L10n());
78
        label.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
79
        GridDataFactory.fillDefaults().applyTo(label);
80

    
81
        // label
82
        String facetText = result.getFacets().stream()
83
                .map(facet -> (facet.getFacet() != null ? facet.getFacet() : "")
84
                        + (facet.getCategory() != null ? " (" + facet.getCategory() + ")" : ""))
85
                .collect(Collectors.joining(","));
86
        if (CdmUtils.isNotBlank(facetText)) {
87
            Label labelFacets = new Label(content, SWT.WRAP);
88
            labelFacets.setText(facetText);
89
            labelFacets.setFont(JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT));
90
        }
91
        // description
92
        String representation_L10n_text = result.getContent().getRepresentation_L10n_text();
93
        if(representation_L10n_text!=null){
94
            Label lblDescription = new Label(content, SWT.WRAP);
95
            GridData layoutData = new GridData();
96
            lblDescription.setLayoutData(layoutData);
97
            parent.addControlListener(new ControlAdapter() {
98
                @Override
99
                public void controlResized(ControlEvent e) {
100
                    layoutData.widthHint = parent.getClientArea().width - (6*layout.marginWidth);
101
                    parent.layout(true);
102
                }
103
            });
104
            lblDescription.setText(representation_L10n_text);
105
        }
106
        // media
107
        if (result.getContent() instanceof TermDto){
108
            Collection<UUID> mediaUuids = ((TermDto)result.getContent()).getMedia();
109
            if(mediaUuids!=null){
110
                if(imageBuffer==null){
111
                    imageBuffer = new HashSet<>();
112
                }
113
                new Job("Load term media") {
114
                    @Override
115
                    protected IStatus run(IProgressMonitor monitor) {
116
                        Collection<URI> mediaUris = new HashSet<>();
117
                        for (UUID uuid : mediaUuids) {
118
                            Media media = CdmStore.getService(IMediaService.class).load(uuid);
119
                            if(media==null){
120
                                continue;
121
                            }
122
                            URI uri = MediaUtils.getFirstMediaRepresentationPart(media).getUri();
123
                            mediaUris.add(uri);
124
                        }
125
                        TermSearchResultComposite.this.getDisplay().asyncExec(()->
126
                        {
127
                            for (URI uri : mediaUris) {
128
                                try {
129
                                    InputStream imageStream = UriUtils.getInputStream(uri);
130
                                    imageBuffer.add(new Image(Display.getCurrent(), imageStream));
131
                                } catch (Exception e1) {
132
                                    // ignore
133
                                }
134
                            }
135
                            Composite mediaContainer = new Composite(content, SWT.NONE);
136
                            mediaContainer.setLayout(new FillLayout());
137
                            for (Image image : imageBuffer) {
138
                                Composite composite = new Composite(mediaContainer, SWT.NONE);
139
                                composite.addPaintListener(new PaintListener() {
140
                                    @Override
141
                                    public void paintControl(PaintEvent e) {
142
                                        if(image!=null){
143
                                            e.gc.setAntialias(SWT.ON);
144
                                            e.gc.setInterpolation(SWT.HIGH);
145
                                            e.gc.drawImage(image, 0, 0,image.getBounds().width, image.getBounds().height,0, 0, 40, 40);
146
                                            e.gc.dispose();
147
                                        }
148
                                    }
149
                                });
150
                            }
151

    
152
                            TermSearchResultComposite.this.getParent().layout();
153
                        });
154
                        return Status.OK_STATUS;
155
                    }
156
                }.schedule();
157
            }
158
        }
159
        return content;
160
    }
161

    
162
}
(5-5/5)