The CTRL+Space did not show function details. This was missed when i first fixed...
[phpeclipse.git] / net.sourceforge.phpeclipse / 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.PHPeclipsePlugin;
21
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.MultiStatus;
27 import org.eclipse.jface.dialogs.ErrorDialog;
28 import org.eclipse.jface.util.Assert;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.ISelectionProvider;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.swt.dnd.DND;
33 import org.eclipse.swt.dnd.DragSourceAdapter;
34 import org.eclipse.swt.dnd.DragSourceEvent;
35 import org.eclipse.swt.dnd.Transfer;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.ui.part.ResourceTransfer;
38
39 /**
40  * A drag adapter that transfers the current selection as </code> IResource</code>.
41  * Only those elements in the selection are part of the transfer which can be
42  * converted into an <code>IResource </code>.
43  */
44 public class ResourceTransferDragAdapter extends DragSourceAdapter implements
45                 TransferDragSourceListener {
46
47         private ISelectionProvider fProvider;
48
49         private static final List EMPTY_LIST = new ArrayList(0);
50
51         /**
52          * Creates a new ResourceTransferDragAdapter for the given selection
53          * provider.
54          * 
55          * @param provider
56          *            the selection provider to access the viewer's selection
57          */
58         public ResourceTransferDragAdapter(ISelectionProvider provider) {
59                 fProvider = provider;
60                 Assert.isNotNull(fProvider);
61         }
62
63         public Transfer getTransfer() {
64                 return ResourceTransfer.getInstance();
65         }
66
67         public void dragStart(DragSourceEvent event) {
68                 event.doit = convertSelection().size() > 0;
69         }
70
71         public void dragSetData(DragSourceEvent event) {
72                 List resources = convertSelection();
73                 event.data = (IResource[]) resources.toArray(new IResource[resources
74                                 .size()]);
75         }
76
77         public void dragFinished(DragSourceEvent event) {
78                 if (!event.doit)
79                         return;
80
81                 if (event.detail == DND.DROP_MOVE) {
82                         handleFinishedDropMove(event);
83                 }
84         }
85
86         private List convertSelection() {
87                 ISelection s = fProvider.getSelection();
88                 if (!(s instanceof IStructuredSelection))
89                         return EMPTY_LIST;
90                 IStructuredSelection selection = (IStructuredSelection) s;
91                 List result = new ArrayList(selection.size());
92                 for (Iterator iter = selection.iterator(); iter.hasNext();) {
93                         Object element = iter.next();
94                         if (element instanceof IAdaptable) {
95                                 IAdaptable adaptable = (IAdaptable) element;
96                                 IResource resource = (IResource) adaptable
97                                                 .getAdapter(IResource.class);
98                                 if (resource != null)
99                                         result.add(resource);
100                         }
101                 }
102                 return result;
103         }
104
105         private void handleFinishedDropMove(DragSourceEvent event) {
106                 MultiStatus status = new MultiStatus(
107                                 PHPeclipsePlugin.getPluginId(),
108                                 IJavaStatusConstants.INTERNAL_ERROR,
109                                 PHPUIMessages
110                                                 .getString("ResourceTransferDragAdapter.cannot_delete_resource"), //$NON-NLS-1$
111                                 null);
112                 List resources = convertSelection();
113                 for (Iterator iter = resources.iterator(); iter.hasNext();) {
114                         IResource resource = (IResource) iter.next();
115                         try {
116                                 resource.delete(true, null);
117                         } catch (CoreException e) {
118                                 status.add(e.getStatus());
119                         }
120                 }
121                 if (status.getChildren().length > 0) {
122                         Shell parent = SWTUtil.getShell(event.widget);
123                         ErrorDialog error = new ErrorDialog(
124                                         parent,
125                                         PHPUIMessages
126                                                         .getString("ResourceTransferDragAdapter.moving_resource"), //$NON-NLS-1$
127                                         PHPUIMessages
128                                                         .getString("ResourceTransferDragAdapter.cannot_delete_files"), //$NON-NLS-1$
129                                         status, IStatus.ERROR);
130                         error.open();
131                 }
132         }
133 }