1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.dnd;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import net.sourceforge.phpdt.internal.ui.IJavaStatusConstants;
18 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
19 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
20 import net.sourceforge.phpeclipse.ui.WebUI;
21 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.MultiStatus;
28 import org.eclipse.jface.dialogs.ErrorDialog;
29 import org.eclipse.jface.util.Assert;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.ISelectionProvider;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.swt.dnd.DND;
34 import org.eclipse.swt.dnd.DragSourceAdapter;
35 import org.eclipse.swt.dnd.DragSourceEvent;
36 import org.eclipse.swt.dnd.Transfer;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.ui.part.ResourceTransfer;
41 * A drag adapter that transfers the current selection as </code> IResource</code>.
42 * Only those elements in the selection are part of the transfer which can be
43 * converted into an <code>IResource </code>.
45 public class ResourceTransferDragAdapter extends DragSourceAdapter implements
46 TransferDragSourceListener {
48 private ISelectionProvider fProvider;
50 private static final List EMPTY_LIST = new ArrayList(0);
53 * Creates a new ResourceTransferDragAdapter for the given selection
57 * the selection provider to access the viewer's selection
59 public ResourceTransferDragAdapter(ISelectionProvider provider) {
61 Assert.isNotNull(fProvider);
64 public Transfer getTransfer() {
65 return ResourceTransfer.getInstance();
68 public void dragStart(DragSourceEvent event) {
69 event.doit = convertSelection().size() > 0;
72 public void dragSetData(DragSourceEvent event) {
73 List resources = convertSelection();
74 event.data = (IResource[]) resources.toArray(new IResource[resources
78 public void dragFinished(DragSourceEvent event) {
82 if (event.detail == DND.DROP_MOVE) {
83 handleFinishedDropMove(event);
87 private List convertSelection() {
88 ISelection s = fProvider.getSelection();
89 if (!(s instanceof IStructuredSelection))
91 IStructuredSelection selection = (IStructuredSelection) s;
92 List result = new ArrayList(selection.size());
93 for (Iterator iter = selection.iterator(); iter.hasNext();) {
94 Object element = iter.next();
95 if (element instanceof IAdaptable) {
96 IAdaptable adaptable = (IAdaptable) element;
97 IResource resource = (IResource) adaptable
98 .getAdapter(IResource.class);
100 result.add(resource);
106 private void handleFinishedDropMove(DragSourceEvent event) {
107 MultiStatus status = new MultiStatus(
109 IJavaStatusConstants.INTERNAL_ERROR,
111 .getString("ResourceTransferDragAdapter.cannot_delete_resource"), //$NON-NLS-1$
113 List resources = convertSelection();
114 for (Iterator iter = resources.iterator(); iter.hasNext();) {
115 IResource resource = (IResource) iter.next();
117 resource.delete(true, null);
118 } catch (CoreException e) {
119 status.add(e.getStatus());
122 if (status.getChildren().length > 0) {
123 Shell parent = SWTUtil.getShell(event.widget);
124 ErrorDialog error = new ErrorDialog(
127 .getString("ResourceTransferDragAdapter.moving_resource"), //$NON-NLS-1$
129 .getString("ResourceTransferDragAdapter.cannot_delete_files"), //$NON-NLS-1$
130 status, IStatus.ERROR);