Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 559df1409c5624609312bcb2ffb575201dba7638 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2006, 2008 Wind River Systems, Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Anton Leherbauer (Wind River Systems) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.navigator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.ui.navigator.CommonDragAdapterAssistant;

/**
 * A Common Navigator drag assistant supporting <code>LocalSelectionTransfer</code> of
 * <code>ICElement</code>s being also <code>ISourceReference</code>s and
 * <code>FileTransfer</code> for external translation units.
 *
 * @see org.eclipse.cdt.internal.ui.cview.SelectionTransferDragAdapter
 */
public class CNavigatorDragAdapterAssistant extends CommonDragAdapterAssistant {

	private static final Transfer[] TRANSFERS = new Transfer[] { LocalSelectionTransfer.getTransfer(),
			FileTransfer.getInstance() };

	/*
	 * @see org.eclipse.ui.navigator.CommonDragAdapterAssistant#getSupportedTransferTypes()
	 */
	@Override
	public Transfer[] getSupportedTransferTypes() {
		return TRANSFERS;
	}

	/*
	 * @see org.eclipse.ui.navigator.CommonDragAdapterAssistant#setDragData(org.eclipse.swt.dnd.DragSourceEvent, org.eclipse.jface.viewers.IStructuredSelection)
	 */
	@Override
	public boolean setDragData(DragSourceEvent event, IStructuredSelection selection) {
		if (selection != null) {
			if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataType)) {
				boolean applicable = false;
				for (Iterator<?> iter = (selection).iterator(); iter.hasNext();) {
					Object element = iter.next();
					if (element instanceof ICElement) {
						if (element instanceof ITranslationUnit) {
							continue;
						}
						if (!(element instanceof ISourceReference)) {
							return false;
						}
						applicable = true;
					}
				}
				if (applicable) {
					event.data = selection;
					return true;
				}
			} else if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
				List<String> files = new ArrayList<>();
				for (Iterator<?> iter = (selection).iterator(); iter.hasNext();) {
					Object element = iter.next();
					if (element instanceof ITranslationUnit) {
						ITranslationUnit tu = (ITranslationUnit) element;
						IPath location = tu.getLocation();
						if (location != null) {
							files.add(location.toOSString());
						}
					}
				}
				if (!files.isEmpty()) {
					event.data = files.toArray(new String[files.size()]);
					return true;
				}
			}
		}
		return false;
	}
}

Back to the top