Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / FeatureNodeDtoDragListener.java
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.featuretree.e4;
10
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.viewers.TreeSelection;
14 import org.eclipse.jface.viewers.TreeViewer;
15 import org.eclipse.swt.dnd.DragSourceAdapter;
16 import org.eclipse.swt.dnd.DragSourceEvent;
17
18 import eu.etaxonomy.cdm.model.term.TermNode;
19 import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
20 import eu.etaxonomy.taxeditor.featuretree.FeatureNodeDtoTransfer;
21
22 public class FeatureNodeDtoDragListener extends DragSourceAdapter {
23
24 private final TreeViewer viewer;
25
26 public FeatureNodeDtoDragListener(TreeViewer viewer) {
27 this.viewer = viewer;
28 }
29
30 /**
31 * Method declared on DragSourceListener
32 */
33 @Override
34 public void dragSetData(DragSourceEvent event) {
35 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
36 if(selection!=null && !selection.isEmpty()){
37 if (selection instanceof TreeSelection && ((TreeSelection)selection).getFirstElement() instanceof TermNodeDto){
38 TermNodeDto[] featureNodes = (TermNodeDto[]) selection.toList().toArray(new TermNodeDto[selection.size()]);
39 if (FeatureNodeDtoTransfer.getInstance().isSupportedType(event.dataType)) {
40 event.data = featureNodes;
41 }
42 }
43 }
44
45 }
46
47 /**
48 * Method declared on DragSourceListener
49 */
50 @Override
51 public void dragStart(DragSourceEvent event) {
52 ISelection sel = viewer.getSelection();
53 if (sel instanceof TreeSelection && ((TreeSelection)sel).getFirstElement() instanceof TermNodeDto){
54 event.doit = !viewer.getSelection().isEmpty()
55 && ((IStructuredSelection) viewer.getSelection()).toList()
56 .stream().allMatch(element -> element instanceof TermNodeDto);
57 }else {
58 event.doit = !viewer.getSelection().isEmpty()
59 && ((IStructuredSelection) viewer.getSelection()).toList()
60 .stream().allMatch(element -> element instanceof TermNode);
61 }
62 }
63
64 }