Update MANIFEST.MF, replaced deprecated Provide-Package with Export-Package.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / dnd / ResourceTransferDragAdapter.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.dnd;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
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;
22
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;
39
40 /**
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>.
44  */
45 public class ResourceTransferDragAdapter extends DragSourceAdapter implements
46                 TransferDragSourceListener {
47
48         private ISelectionProvider fProvider;
49
50         private static final List EMPTY_LIST = new ArrayList(0);
51
52         /**
53          * Creates a new ResourceTransferDragAdapter for the given selection
54          * provider.
55          * 
56          * @param provider
57          *            the selection provider to access the viewer's selection
58          */
59         public ResourceTransferDragAdapter(ISelectionProvider provider) {
60                 fProvider = provider;
61                 Assert.isNotNull(fProvider);
62         }
63
64         public Transfer getTransfer() {
65                 return ResourceTransfer.getInstance();
66         }
67
68         public void dragStart(DragSourceEvent event) {
69                 event.doit = convertSelection().size() > 0;
70         }
71
72         public void dragSetData(DragSourceEvent event) {
73                 List resources = convertSelection();
74                 event.data = (IResource[]) resources.toArray(new IResource[resources
75                                 .size()]);
76         }
77
78         public void dragFinished(DragSourceEvent event) {
79                 if (!event.doit)
80                         return;
81
82                 if (event.detail == DND.DROP_MOVE) {
83                         handleFinishedDropMove(event);
84                 }
85         }
86
87         private List convertSelection() {
88                 ISelection s = fProvider.getSelection();
89                 if (!(s instanceof IStructuredSelection))
90                         return EMPTY_LIST;
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);
99                                 if (resource != null)
100                                         result.add(resource);
101                         }
102                 }
103                 return result;
104         }
105
106         private void handleFinishedDropMove(DragSourceEvent event) {
107                 MultiStatus status = new MultiStatus(
108                                 WebUI.getPluginId(),
109                                 IJavaStatusConstants.INTERNAL_ERROR,
110                                 PHPUIMessages
111                                                 .getString("ResourceTransferDragAdapter.cannot_delete_resource"), //$NON-NLS-1$
112                                 null);
113                 List resources = convertSelection();
114                 for (Iterator iter = resources.iterator(); iter.hasNext();) {
115                         IResource resource = (IResource) iter.next();
116                         try {
117                                 resource.delete(true, null);
118                         } catch (CoreException e) {
119                                 status.add(e.getStatus());
120                         }
121                 }
122                 if (status.getChildren().length > 0) {
123                         Shell parent = SWTUtil.getShell(event.widget);
124                         ErrorDialog error = new ErrorDialog(
125                                         parent,
126                                         PHPUIMessages
127                                                         .getString("ResourceTransferDragAdapter.moving_resource"), //$NON-NLS-1$
128                                         PHPUIMessages
129                                                         .getString("ResourceTransferDragAdapter.cannot_delete_files"), //$NON-NLS-1$
130                                         status, IStatus.ERROR);
131                         error.open();
132                 }
133         }
134 }