Project

General

Profile

Download (4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.editor.descriptiveDataSet.character;
10

    
11
import java.util.Collection;
12
import java.util.stream.Collectors;
13

    
14
import org.eclipse.jface.viewers.IStructuredSelection;
15
import org.eclipse.jface.viewers.TreeSelection;
16
import org.eclipse.jface.viewers.TreeViewer;
17
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
18
import org.eclipse.swt.dnd.DragSourceEvent;
19
import org.eclipse.swt.graphics.GC;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.graphics.Point;
22
import org.eclipse.swt.widgets.Display;
23

    
24
import eu.etaxonomy.cdm.model.description.Character;
25
import eu.etaxonomy.cdm.model.description.FeatureNode;
26
import eu.etaxonomy.taxeditor.featuretree.CharacterTransfer;
27
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDragListener;
28

    
29
/**
30
 * @author pplitzner
31
 * @since Jun 2, 2017
32
 *
33
 */
34
public class CharacterDragListener extends FeatureNodeDragListener {
35

    
36
    private TreeViewer structureViewer;
37
    private TreeViewer propertyViewer;
38
    Collection<Character> characters;
39
    private Image image;
40
    private GC gc;
41

    
42
    public CharacterDragListener(TreeViewer structureViewer, TreeViewer propertyViewer) {
43
        super(propertyViewer);
44
        this.structureViewer = structureViewer;
45
        this.propertyViewer = propertyViewer;
46
    }
47

    
48
    @Override
49
    public void dragFinished(DragSourceEvent event) {
50
        characters = null;
51
        if(image!=null){
52
            image.dispose();
53
            image = null;
54
        }
55
        if(gc!=null){
56
            gc.dispose();
57
            gc = null;
58
        }
59
        super.dragFinished(event);
60
    }
61

    
62
    @Override
63
    public void dragSetData(DragSourceEvent event) {
64
        if (CharacterTransfer.getInstance().isSupportedType(event.dataType)) {
65
            event.data = characters;
66
        }
67
    }
68

    
69
    @Override
70
    public void dragStart(DragSourceEvent event) {
71
        event.doit =
72
                !structureViewer.getSelection().isEmpty()
73
                && !propertyViewer.getSelection().isEmpty()
74
                && ((IStructuredSelection) structureViewer.getSelection()).toList()
75
                .stream().allMatch(element -> element instanceof FeatureNode)
76
                && ((IStructuredSelection) propertyViewer.getSelection()).toList()
77
                .stream().allMatch(element -> element instanceof FeatureNode);
78
        if(!event.doit){
79
            return;
80
        }
81
        TreeSelection structureSelection = (TreeSelection) structureViewer.getSelection();
82
        TreeSelection propertySelection = (TreeSelection) propertyViewer.getSelection();
83
        if(structureSelection!=null && propertySelection!=null
84
                && !structureSelection.isEmpty() && !propertySelection.isEmpty()){
85
            //create all possible characters
86
            characters = CharacterEditor.createCharacters(structureSelection, propertySelection);
87

    
88
        }
89
        String text = characters.stream().map(character->character.generateTitle()).collect(Collectors.joining("\n"));
90

    
91
        // creating temporary GC for size calculation
92
        gc = new GC(Display.getDefault());
93
        // Calculate text extent
94
        Point compositeSize = gc.textExtent(text);
95
        //remove temporary gc
96
        gc.dispose();
97
        gc = null;
98
        // Creating new Image
99
        image = new Image(Display.getCurrent(),compositeSize.x+10,compositeSize.y+10);
100
        //create gc for image to be able to draw on it
101
        gc = new GC(image);
102
        //set and fill with bg color
103
        gc.setBackground(GUIHelper.COLOR_LIST_BACKGROUND);
104
        gc.fillRectangle(0, 0, compositeSize.x+10, compositeSize.y+10);
105
        //draw text
106
        gc.drawText(text, 5, 5, true);
107
        //draw border
108
        gc.drawRectangle(0, 0, compositeSize.x+9, compositeSize.y+9);
109
        // Setting widget to DnD image
110
        event.image = image;
111

    
112
    }
113

    
114
}
(1-1/3)