~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBDropTargetListener.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
9
 
 *    Markus Schorn - initial API and implementation
10
 
 *******************************************************************************/ 
11
 
 
12
 
package org.eclipse.cdt.internal.ui.includebrowser;
13
 
 
14
 
import java.util.Iterator;
15
 
 
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;
30
 
 
31
 
import org.eclipse.cdt.core.model.CoreModelUtil;
32
 
import org.eclipse.cdt.core.model.ITranslationUnit;
33
 
 
34
 
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
35
 
 
36
 
public class IBDropTargetListener implements DropTargetListener {
37
 
    
38
 
    private IBViewPart fIncludeBrowser;
39
 
        private ITranslationUnit fTranslationUnit;
40
 
        private boolean fEnabled= true;
41
 
 
42
 
    public IBDropTargetListener(IBViewPart view) {
43
 
        fIncludeBrowser= view;
44
 
    }
45
 
    
46
 
    public void setEnabled(boolean val) {
47
 
        fEnabled= val;
48
 
    }
49
 
    
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;
61
 
                                        }
62
 
                                        else {
63
 
                                                event.currentDataType= alternateDataType;
64
 
                                        }
65
 
                                }
66
 
                }
67
 
        }
68
 
    }
69
 
 
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)) {
76
 
                                        return dataType;
77
 
                                }
78
 
                        }
79
 
                }
80
 
                return null;
81
 
        }
82
 
 
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;
90
 
                                }
91
 
                        }
92
 
                }
93
 
                return null;
94
 
        }
95
 
 
96
 
    public void dragLeave(DropTargetEvent event) {
97
 
    }
98
 
 
99
 
    public void dragOperationChanged(DropTargetEvent event) {
100
 
        checkOperation(event);
101
 
    }
102
 
 
103
 
    public void dragOver(DropTargetEvent event) {
104
 
    }
105
 
 
106
 
    public void drop(DropTargetEvent event) {
107
 
        if (fTranslationUnit == null) {
108
 
                fTranslationUnit= findFirstTranslationUnit(event.data);
109
 
        }            
110
 
        if (fTranslationUnit == null) {
111
 
            fIncludeBrowser.setMessage(IBMessages.IBViewPart_falseInputMessage);
112
 
            Display.getCurrent().beep();
113
 
        }
114
 
        else {
115
 
            fIncludeBrowser.setInput(fTranslationUnit);
116
 
        }
117
 
    }
118
 
 
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)));
125
 
                if (tu != null) {
126
 
                    return tu;
127
 
                }
128
 
            }
129
 
            return null;
130
 
        }
131
 
        if (o instanceof IResource[]) {
132
 
            return findTranslationUnit((IResource[]) o);
133
 
        }
134
 
        return null;
135
 
    }
136
 
 
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);
142
 
                if (tu != null) {
143
 
                    return tu;
144
 
                }
145
 
            }
146
 
        }
147
 
        return null;
148
 
    }
149
 
 
150
 
    public void dropAccept(DropTargetEvent event) {
151
 
    }
152
 
    
153
 
    private void checkOperation(DropTargetEvent event) {
154
 
        if (fEnabled && (event.operations & DND.DROP_COPY) != 0) {
155
 
            event.detail= DND.DROP_COPY;
156
 
        }
157
 
        else {
158
 
            event.detail= DND.DROP_NONE;
159
 
        }
160
 
    }
161
 
}