001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.dnd;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IAdaptable;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.MultiStatus;
021:
022: import org.eclipse.core.resources.IResource;
023:
024: import org.eclipse.swt.dnd.DND;
025: import org.eclipse.swt.dnd.DragSourceAdapter;
026: import org.eclipse.swt.dnd.DragSourceEvent;
027: import org.eclipse.swt.dnd.Transfer;
028: import org.eclipse.swt.widgets.Shell;
029:
030: import org.eclipse.jface.dialogs.ErrorDialog;
031: import org.eclipse.jface.util.TransferDragSourceListener;
032: import org.eclipse.jface.viewers.ISelection;
033: import org.eclipse.jface.viewers.ISelectionProvider;
034: import org.eclipse.jface.viewers.IStructuredSelection;
035:
036: import org.eclipse.ui.part.ResourceTransfer;
037:
038: import org.eclipse.jdt.core.IJavaElement;
039:
040: import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
041: import org.eclipse.jdt.internal.ui.JavaPlugin;
042: import org.eclipse.jdt.internal.ui.JavaUIMessages;
043: import org.eclipse.jdt.internal.ui.util.SWTUtil;
044:
045: /**
046: * A drag adapter that transfers the current selection as </code>
047: * IResource</code>. Only those elements in the selection are part
048: * of the transfer which can be converted into an <code>IResource
049: * </code>.
050: */
051: public class ResourceTransferDragAdapter extends DragSourceAdapter
052: implements TransferDragSourceListener {
053:
054: private ISelectionProvider fProvider;
055:
056: private static final List EMPTY_LIST = new ArrayList(0);
057:
058: /**
059: * Creates a new ResourceTransferDragAdapter for the given selection
060: * provider.
061: *
062: * @param provider the selection provider to access the viewer's selection
063: */
064: public ResourceTransferDragAdapter(ISelectionProvider provider) {
065: fProvider = provider;
066: Assert.isNotNull(fProvider);
067: }
068:
069: public Transfer getTransfer() {
070: return ResourceTransfer.getInstance();
071: }
072:
073: public void dragStart(DragSourceEvent event) {
074: event.doit = convertSelection().size() > 0;
075: }
076:
077: public void dragSetData(DragSourceEvent event) {
078: List resources = convertSelection();
079: event.data = resources.toArray(new IResource[resources.size()]);
080: }
081:
082: public void dragFinished(DragSourceEvent event) {
083: if (!event.doit)
084: return;
085:
086: if (event.detail == DND.DROP_MOVE) {
087: handleFinishedDropMove(event);
088: }
089: }
090:
091: private List convertSelection() {
092: ISelection s = fProvider.getSelection();
093: if (!(s instanceof IStructuredSelection))
094: return EMPTY_LIST;
095: IStructuredSelection selection = (IStructuredSelection) s;
096: List result = new ArrayList(selection.size());
097: for (Iterator iter = selection.iterator(); iter.hasNext();) {
098: Object element = iter.next();
099: IResource resource = null;
100: if (element instanceof IJavaElement) {
101: // don't use IAdaptable as for members only the top level type adapts
102: resource = ((IJavaElement) element).getResource();
103: } else if (element instanceof IAdaptable) {
104: resource = (IResource) ((IAdaptable) element)
105: .getAdapter(IResource.class);
106: }
107: if (resource != null)
108: result.add(resource);
109: }
110: return result;
111: }
112:
113: private void handleFinishedDropMove(DragSourceEvent event) {
114: MultiStatus status = new MultiStatus(
115: JavaPlugin.getPluginId(),
116: IJavaStatusConstants.INTERNAL_ERROR,
117: JavaUIMessages.ResourceTransferDragAdapter_cannot_delete_resource,
118: null);
119: List resources = convertSelection();
120: for (Iterator iter = resources.iterator(); iter.hasNext();) {
121: IResource resource = (IResource) iter.next();
122: try {
123: resource.delete(true, null);
124: } catch (CoreException e) {
125: status.add(e.getStatus());
126: }
127: }
128: if (status.getChildren().length > 0) {
129: Shell parent = SWTUtil.getShell(event.widget);
130: ErrorDialog error = new ErrorDialog(
131: parent,
132: JavaUIMessages.ResourceTransferDragAdapter_moving_resource,
133: JavaUIMessages.ResourceTransferDragAdapter_cannot_delete_files,
134: status, IStatus.ERROR);
135: error.open();
136: }
137: }
138: }
|