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