1
/*******************************************************************************
2
* Copyright (c) 2000, 2008 IBM Corporation 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
* IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.ui.dnd;
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.Iterator;
16
import java.util.List;
18
import org.eclipse.core.resources.IResource;
19
import org.eclipse.core.runtime.Assert;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IAdaptable;
22
import org.eclipse.jface.viewers.ISelection;
23
import org.eclipse.jface.viewers.ISelectionProvider;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.swt.dnd.DND;
26
import org.eclipse.swt.dnd.DragSource;
27
import org.eclipse.swt.dnd.DragSourceEvent;
28
import org.eclipse.swt.dnd.Transfer;
29
import org.eclipse.swt.widgets.Control;
30
import org.eclipse.swt.widgets.Shell;
31
import org.eclipse.ui.actions.ReadOnlyStateChecker;
32
import org.eclipse.ui.part.ResourceTransfer;
34
import org.eclipse.cdt.internal.ui.CUIMessages;
37
* A drag adapter that transfers the current selection as </code>
38
* IResource</code>. Only those elements in the selection are part
39
* of the transfer which can be converted into an <code>IResource
42
public class ResourceTransferDragAdapter implements TransferDragSourceListener {
43
private final ISelectionProvider provider;
46
* Creates a new ResourceTransferDragAdapter for the given selection provider.
48
* @param provider the selection provider to access the viewer's selection
50
public ResourceTransferDragAdapter(ISelectionProvider provider) {
52
this.provider = provider;
53
Assert.isNotNull(provider);
56
public Transfer getTransfer() {
57
return ResourceTransfer.getInstance();
60
public void dragStart(DragSourceEvent event) {
61
IResource[] resources = getSelectedResources();
62
event.doit = resources.length > 0;
65
public void dragSetData(DragSourceEvent event) {
66
event.data = getSelectedResources();
69
public void dragFinished(DragSourceEvent event) {
70
if (event.doit && event.detail == DND.DROP_MOVE) {
71
IResource[] resources = getSelectedResources();
73
if (resources.length == 0)
76
DragSource dragSource = (DragSource) event.widget;
77
Control control = dragSource.getControl();
78
Shell shell = control.getShell();
79
String title = CUIMessages.Drag_move_problem_title;
80
String message = CUIMessages.Drag_move_problem_message;
82
ReadOnlyStateChecker checker = new ReadOnlyStateChecker(shell, title, message);
84
resources = checker.checkReadOnlyResources(resources);
86
// delete the old elements
87
for (int i = 0; i < resources.length; ++i) {
89
resources[i].delete(IResource.KEEP_HISTORY | IResource.FORCE, null);
90
} catch (CoreException e) {
97
private IResource[] getSelectedResources() {
98
List<IResource> resources = Collections.emptyList();
99
ISelection selection = provider.getSelection();
101
if (selection instanceof IStructuredSelection) {
102
IStructuredSelection structured = (IStructuredSelection) selection;
104
resources = new ArrayList<IResource>(structured.size());
106
for (Iterator<?> iterator = structured.iterator(); iterator.hasNext();) {
107
Object element = iterator.next();
108
IResource resource = null;
109
if (element instanceof IResource) {
110
resource = (IResource)element;
111
} else if (element instanceof IAdaptable) {
112
IAdaptable adaptable = (IAdaptable) element;
113
resource = (IResource) adaptable.getAdapter(IResource.class);
115
if (resource != null) {
116
resources.add(resource);
121
IResource[] result = new IResource[resources.size()];
122
resources.toArray(result);