Better event handling for browser preview refreshs in Wikipedia plugin
[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 /**
41  * A drag adapter that transfers the current selection as </code>
42  * IResource</code>. Only those elements in the selection are part 
43  * of the transfer which can be converted into an <code>IResource
44  * </code>.
45  */
46 public class ResourceTransferDragAdapter extends DragSourceAdapter implements 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 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.size()]);
74         }
75         
76         public void dragFinished(DragSourceEvent event) {
77                 if (!event.doit)
78                         return;
79
80                 if (event.detail == DND.DROP_MOVE) {
81                         handleFinishedDropMove(event);
82                 }       
83         }
84         
85         private List convertSelection() {
86                 ISelection s= fProvider.getSelection();
87                 if (!(s instanceof IStructuredSelection))
88                         return EMPTY_LIST;
89                 IStructuredSelection selection= (IStructuredSelection)s;
90                 List result= new ArrayList(selection.size());
91                 for (Iterator iter= selection.iterator(); iter.hasNext();) {
92                         Object element= iter.next();
93                         if (element instanceof IAdaptable) {
94                                 IAdaptable adaptable= (IAdaptable)element;
95                                 IResource resource= (IResource)adaptable.getAdapter(IResource.class);
96                                 if (resource != null)
97                                         result.add(resource);
98                         }
99                 }
100                 return result;
101         }
102         
103         private void handleFinishedDropMove(DragSourceEvent event) {
104                 MultiStatus status= new MultiStatus(
105                         PHPeclipsePlugin.getPluginId(), 
106                         IJavaStatusConstants.INTERNAL_ERROR, 
107                         PHPUIMessages.getString("ResourceTransferDragAdapter.cannot_delete_resource"),  //$NON-NLS-1$
108                         null);
109                 List resources= convertSelection();
110                 for (Iterator iter= resources.iterator(); iter.hasNext();) {
111                         IResource resource= (IResource) iter.next();
112                         try {
113                                 resource.delete(true, null);
114                         } catch (CoreException e) {
115                                 status.add(e.getStatus());
116                         }
117                 }
118                 if (status.getChildren().length > 0) {
119                         Shell parent= SWTUtil.getShell(event.widget);
120                         ErrorDialog error= new ErrorDialog(parent, 
121                           PHPUIMessages.getString("ResourceTransferDragAdapter.moving_resource"),  //$NON-NLS-1$
122                           PHPUIMessages.getString("ResourceTransferDragAdapter.cannot_delete_files"),  //$NON-NLS-1$
123                                 status, IStatus.ERROR);
124                         error.open();
125                 }
126         }
127 }
128