Commit | Line | Data |
---|---|---|
ef906471 XR |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Kalray | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Xavier Raynaud - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.views.filter; | |
14 | ||
15 | ||
16 | import org.eclipse.jface.util.LocalSelectionTransfer; | |
17 | import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode; | |
18 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode; | |
19 | import org.eclipse.swt.dnd.DND; | |
20 | import org.eclipse.swt.dnd.DropTargetAdapter; | |
21 | import org.eclipse.swt.dnd.DropTargetEvent; | |
22 | import org.eclipse.swt.widgets.TreeItem; | |
23 | ||
24 | /** | |
25 | * DropTargetListener for filter view | |
26 | * @author Xavier Raynaud <xavier.raynaud@kalray.eu> | |
27 | */ | |
28 | class FilterDropTargetAdapter extends DropTargetAdapter { | |
29 | ||
30 | private FilterViewer fViewer; | |
31 | ||
32 | /** | |
33 | * Constructor | |
34 | * @param viewer the content of the FilterView | |
35 | */ | |
36 | public FilterDropTargetAdapter(FilterViewer viewer) { | |
37 | super(); | |
38 | this.fViewer = viewer; | |
39 | } | |
40 | ||
41 | /** | |
42 | * Returns <code>true</code> if droppedNode is an ancestor of node. | |
43 | * | |
44 | * @param droppedNode | |
45 | * the ITmfFilterTreeNode to drop or paste | |
46 | * @param node | |
47 | * the ITmfFilterTreeNode receiving a new child | |
48 | * @return <code>true</code> if droppedNode is and ancestor of node, | |
49 | * <code>false</code> otherwise. | |
50 | */ | |
51 | private static boolean isAncestor(ITmfFilterTreeNode droppedNode, ITmfFilterTreeNode node) { | |
52 | ITmfFilterTreeNode tmp = node; | |
53 | ||
54 | while (tmp != null) { | |
55 | ITmfFilterTreeNode n = tmp.getParent(); | |
56 | if (n == droppedNode) { | |
57 | return true; | |
58 | } | |
59 | tmp = n; | |
60 | } | |
61 | return false; | |
62 | } | |
63 | ||
64 | @Override | |
65 | public void dropAccept(DropTargetEvent event) { | |
66 | ITmfFilterTreeNode treeNodeToDrop = null; | |
67 | if (LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType)) { | |
68 | treeNodeToDrop = FilterEditUtils.getTransferredTreeNode(); | |
69 | } | |
70 | if (treeNodeToDrop == null) { | |
71 | // should never occur | |
72 | event.detail = DND.DROP_NONE; | |
73 | return; | |
74 | } | |
75 | if (event.item instanceof TreeItem) { | |
76 | Object data = event.item.getData(); | |
77 | if (data instanceof ITmfFilterTreeNode) { | |
78 | ITmfFilterTreeNode node = (ITmfFilterTreeNode) data; | |
79 | if (node.getValidChildren().contains(treeNodeToDrop.getNodeName())) { | |
80 | if (isAncestor(treeNodeToDrop, node) && event.detail != DND.DROP_COPY) { | |
81 | // do nothing in this case | |
82 | event.detail = DND.DROP_NONE; | |
83 | } | |
84 | return; | |
85 | } | |
86 | } | |
87 | } else { // accept only TmfFilterNode | |
88 | if (!TmfFilterNode.NODE_NAME.equals(treeNodeToDrop.getNodeName())) { | |
89 | event.detail = DND.DROP_NONE; | |
90 | } | |
91 | return; | |
92 | } | |
93 | event.detail = DND.DROP_NONE; | |
94 | return; | |
95 | } | |
96 | ||
97 | @Override | |
98 | public void drop(DropTargetEvent event) { | |
99 | ITmfFilterTreeNode treeNodeToDrop = FilterEditUtils.getTransferredTreeNode(); | |
100 | if (event.item instanceof TreeItem) { | |
101 | Object data = event.item.getData(); | |
102 | if (data instanceof ITmfFilterTreeNode) { | |
103 | ITmfFilterTreeNode node = (ITmfFilterTreeNode) data; | |
104 | if (node.getValidChildren().contains(treeNodeToDrop.getNodeName())) { | |
105 | treeNodeToDrop = treeNodeToDrop.clone(); | |
106 | node.addChild(treeNodeToDrop); | |
107 | fViewer.refresh(); | |
108 | fViewer.setSelection(treeNodeToDrop, true); | |
109 | return; | |
110 | } | |
111 | } | |
112 | } else { // accept only TmfFilterNode | |
113 | if (TmfFilterNode.NODE_NAME.equals(treeNodeToDrop.getNodeName())) { | |
114 | ITmfFilterTreeNode root = fViewer.getInput(); | |
115 | treeNodeToDrop = treeNodeToDrop.clone(); | |
116 | root.addChild(treeNodeToDrop); | |
117 | fViewer.refresh(); | |
118 | fViewer.setSelection(treeNodeToDrop, true); | |
119 | return; | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | } |