1 |
41153841
|
Patrick Plitzner
|
/**
|
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 |
652496d7
|
Andreas Müller
|
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.character;
|
10 |
41153841
|
Patrick Plitzner
|
|
11 |
53b63498
|
Patrick Plitzner
|
import java.util.Collection;
|
12 |
e4ce2709
|
Patrick Plitzner
|
import java.util.stream.Collectors;
|
13 |
86fcba21
|
Patrick Plitzner
|
|
14 |
53b63498
|
Patrick Plitzner
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
15 |
86fcba21
|
Patrick Plitzner
|
import org.eclipse.jface.viewers.TreeSelection;
|
16 |
41153841
|
Patrick Plitzner
|
import org.eclipse.jface.viewers.TreeViewer;
|
17 |
e4ce2709
|
Patrick Plitzner
|
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
|
18 |
41153841
|
Patrick Plitzner
|
import org.eclipse.swt.dnd.DragSourceEvent;
|
19 |
e4ce2709
|
Patrick Plitzner
|
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 |
41153841
|
Patrick Plitzner
|
|
24 |
86fcba21
|
Patrick Plitzner
|
import eu.etaxonomy.cdm.model.description.Character;
|
25 |
53b63498
|
Patrick Plitzner
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
26 |
|
|
import eu.etaxonomy.taxeditor.featuretree.CharacterTransfer;
|
27 |
1cea32bb
|
Patrick Plitzner
|
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDragListener;
|
28 |
41153841
|
Patrick Plitzner
|
|
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 |
e4ce2709
|
Patrick Plitzner
|
Collection<Character> characters;
|
39 |
|
|
private Image image;
|
40 |
|
|
private GC gc;
|
41 |
41153841
|
Patrick Plitzner
|
|
42 |
|
|
public CharacterDragListener(TreeViewer structureViewer, TreeViewer propertyViewer) {
|
43 |
|
|
super(propertyViewer);
|
44 |
|
|
this.structureViewer = structureViewer;
|
45 |
|
|
this.propertyViewer = propertyViewer;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
@Override
|
49 |
e4ce2709
|
Patrick Plitzner
|
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 |
86fcba21
|
Patrick Plitzner
|
|
62 |
e4ce2709
|
Patrick Plitzner
|
@Override
|
63 |
|
|
public void dragSetData(DragSourceEvent event) {
|
64 |
|
|
if (CharacterTransfer.getInstance().isSupportedType(event.dataType)) {
|
65 |
|
|
event.data = characters;
|
66 |
41153841
|
Patrick Plitzner
|
}
|
67 |
|
|
}
|
68 |
|
|
|
69 |
53b63498
|
Patrick Plitzner
|
@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 |
e4ce2709
|
Patrick Plitzner
|
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 |
53b63498
|
Patrick Plitzner
|
}
|
113 |
|
|
|
114 |
41153841
|
Patrick Plitzner
|
}
|