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