Fix potential NPE
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / character / CharacterDragListener.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.editor.descriptiveDataSet.character;
10
11 import java.util.Collection;
12
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.viewers.TreeSelection;
15 import org.eclipse.jface.viewers.TreeViewer;
16 import org.eclipse.swt.dnd.DragSourceEvent;
17
18 import eu.etaxonomy.cdm.model.description.Character;
19 import eu.etaxonomy.cdm.model.description.FeatureNode;
20 import eu.etaxonomy.taxeditor.featuretree.CharacterTransfer;
21 import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDragListener;
22
23 /**
24 * @author pplitzner
25 * @since Jun 2, 2017
26 *
27 */
28 public class CharacterDragListener extends FeatureNodeDragListener {
29
30 private TreeViewer structureViewer;
31 private TreeViewer propertyViewer;
32
33 public CharacterDragListener(TreeViewer structureViewer, TreeViewer propertyViewer) {
34 super(propertyViewer);
35 this.structureViewer = structureViewer;
36 this.propertyViewer = propertyViewer;
37 }
38
39 @Override
40 public void dragSetData(DragSourceEvent event) {
41 TreeSelection structureSelection = (TreeSelection) structureViewer.getSelection();
42 TreeSelection propertySelection = (TreeSelection) propertyViewer.getSelection();
43 if(structureSelection!=null && propertySelection!=null
44 && !structureSelection.isEmpty() && !propertySelection.isEmpty()){
45 //create all possible characters
46 Collection<Character> characters = CharacterEditor.createCharacters(structureSelection, propertySelection);
47
48 if (CharacterTransfer.getInstance().isSupportedType(event.dataType)) {
49 event.data = characters;
50 }
51 }
52 }
53
54 /**
55 * Method declared on DragSourceListener
56 */
57 @Override
58 public void dragStart(DragSourceEvent event) {
59 event.doit =
60 !structureViewer.getSelection().isEmpty()
61 && !propertyViewer.getSelection().isEmpty()
62 && ((IStructuredSelection) structureViewer.getSelection()).toList()
63 .stream().allMatch(element -> element instanceof FeatureNode)
64 && ((IStructuredSelection) propertyViewer.getSelection()).toList()
65 .stream().allMatch(element -> element instanceof FeatureNode);
66 }
67
68 }