Project

General

Profile

Download (7.62 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.util.Collection;
13
import java.util.HashSet;
14
import java.util.UUID;
15
import java.util.stream.Collectors;
16

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

    
36
import eu.etaxonomy.cdm.api.service.IMediaService;
37
import eu.etaxonomy.cdm.common.CdmUtils;
38
import eu.etaxonomy.cdm.common.URI;
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
public class TermSearchResultComposite extends CheckBoxSearchResultComposite<AbstractTermDto, TermSearchResult> {
52

    
53
    private Collection<Image> imageBuffer;
54

    
55

    
56

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

    
60
    }
61

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

    
69
        super.dispose();
70
    }
71

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

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

    
175

    
176

    
177

    
178
}
(5-5/5)