1
/*******************************************************************************
2
* Copyright (c) 2006, 2008 Wind River Systems, Inc. and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Markus Schorn - initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.cdt.internal.ui.includebrowser;
14
import java.util.Iterator;
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IResource;
18
import org.eclipse.core.runtime.Path;
19
import org.eclipse.jface.util.LocalSelectionTransfer;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.IStructuredSelection;
22
import org.eclipse.swt.dnd.DND;
23
import org.eclipse.swt.dnd.DropTargetEvent;
24
import org.eclipse.swt.dnd.DropTargetListener;
25
import org.eclipse.swt.dnd.FileTransfer;
26
import org.eclipse.swt.dnd.Transfer;
27
import org.eclipse.swt.dnd.TransferData;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.ui.part.ResourceTransfer;
31
import org.eclipse.cdt.core.model.CoreModelUtil;
32
import org.eclipse.cdt.core.model.ITranslationUnit;
34
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
36
public class IBDropTargetListener implements DropTargetListener {
38
private IBViewPart fIncludeBrowser;
39
private ITranslationUnit fTranslationUnit;
40
private boolean fEnabled= true;
42
public IBDropTargetListener(IBViewPart view) {
43
fIncludeBrowser= view;
46
public void setEnabled(boolean val) {
50
public void dragEnter(DropTargetEvent event) {
51
fTranslationUnit= null;
52
checkOperation(event);
53
if (event.detail != DND.DROP_NONE) {
54
if (LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType)) {
55
fTranslationUnit= checkLocalSelection();
56
if (fTranslationUnit == null) {
57
TransferData alternateDataType = checkForAlternateDataType(event.dataTypes,
58
new Transfer[] {ResourceTransfer.getInstance(), FileTransfer.getInstance()});
59
if (alternateDataType == null) {
60
event.detail= DND.DROP_NONE;
63
event.currentDataType= alternateDataType;
70
private TransferData checkForAlternateDataType(TransferData[] dataTypes, Transfer[] transfers) {
71
for (int i = 0; i < dataTypes.length; i++) {
72
TransferData dataType = dataTypes[i];
73
for (int j = 0; j < transfers.length; j++) {
74
Transfer transfer = transfers[j];
75
if (transfer.isSupportedType(dataType)) {
83
private ITranslationUnit checkLocalSelection() {
84
ISelection sel= LocalSelectionTransfer.getTransfer().getSelection();
85
if (sel instanceof IStructuredSelection) {
86
for (Iterator<?> iter = ((IStructuredSelection)sel).iterator(); iter.hasNext();) {
87
Object element = iter.next();
88
if (element instanceof ITranslationUnit) {
89
return (ITranslationUnit) element;
96
public void dragLeave(DropTargetEvent event) {
99
public void dragOperationChanged(DropTargetEvent event) {
100
checkOperation(event);
103
public void dragOver(DropTargetEvent event) {
106
public void drop(DropTargetEvent event) {
107
if (fTranslationUnit == null) {
108
fTranslationUnit= findFirstTranslationUnit(event.data);
110
if (fTranslationUnit == null) {
111
fIncludeBrowser.setMessage(IBMessages.IBViewPart_falseInputMessage);
112
Display.getCurrent().beep();
115
fIncludeBrowser.setInput(fTranslationUnit);
119
private ITranslationUnit findFirstTranslationUnit(Object o) {
120
if (o instanceof String[]) {
121
String[] filePaths= (String[]) o;
122
for (int i = 0; i < filePaths.length; i++) {
123
String filePath = filePaths[i];
124
ITranslationUnit tu= findTranslationUnit(ResourceLookup.findFilesForLocation(Path.fromOSString(filePath)));
131
if (o instanceof IResource[]) {
132
return findTranslationUnit((IResource[]) o);
137
private ITranslationUnit findTranslationUnit(IResource[] files) {
138
for (int i = 0; i < files.length; i++) {
139
IResource resource = files[i];
140
if (resource.getType() == IResource.FILE) {
141
ITranslationUnit tu= CoreModelUtil.findTranslationUnit((IFile) resource);
150
public void dropAccept(DropTargetEvent event) {
153
private void checkOperation(DropTargetEvent event) {
154
if (fEnabled && (event.operations & DND.DROP_COPY) != 0) {
155
event.detail= DND.DROP_COPY;
158
event.detail= DND.DROP_NONE;