001: /*******************************************************************************
002: * Copyright (c) 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.navigator;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.Status;
017:
018: import org.eclipse.core.resources.IContainer;
019: import org.eclipse.core.resources.IResource;
020:
021: import org.eclipse.swt.dnd.DND;
022: import org.eclipse.swt.dnd.DropTargetEvent;
023: import org.eclipse.swt.dnd.FileTransfer;
024: import org.eclipse.swt.dnd.TransferData;
025:
026: import org.eclipse.jface.viewers.ISelection;
027: import org.eclipse.jface.viewers.IStructuredSelection;
028:
029: import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
030: import org.eclipse.ui.navigator.CommonDropAdapter;
031: import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
032:
033: import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
034:
035: import org.eclipse.jdt.core.IJavaElement;
036: import org.eclipse.jdt.core.JavaModelException;
037:
038: import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyProcessor;
039: import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
040: import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgDestinationFactory;
041: import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
042: import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
043: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
044: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
045:
046: import org.eclipse.jdt.internal.ui.packageview.PackagesMessages;
047: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
048: import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgCopyStarter;
049: import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveStarter;
050: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
051:
052: public class JavaDropAdapterAssistant extends
053: CommonDropAdapterAssistant {
054:
055: private List fElements;
056: private JavaMoveProcessor fMoveProcessor;
057: private int fCanMoveElements;
058: private JavaCopyProcessor fCopyProcessor;
059: private int fCanCopyElements;
060:
061: public IStatus handleDrop(CommonDropAdapter dropAdapter,
062: DropTargetEvent dropTargetEvent, Object target) {
063: if (LocalSelectionTransfer.getInstance().isSupportedType(
064: dropAdapter.getCurrentTransfer())) {
065: try {
066:
067: switch (dropAdapter.getCurrentOperation()) {
068: case DND.DROP_MOVE:
069: handleDropMove(target);
070: break;
071: case DND.DROP_COPY:
072: handleDropCopy(target);
073: break;
074: }
075: } catch (JavaModelException e) {
076: ExceptionHandler
077: .handle(
078: e,
079: PackagesMessages.SelectionTransferDropAdapter_error_title,
080: PackagesMessages.SelectionTransferDropAdapter_error_message);
081: } catch (InvocationTargetException e) {
082: ExceptionHandler
083: .handle(
084: e,
085: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
086: RefactoringMessages.OpenRefactoringWizardAction_exception);
087: } catch (InterruptedException e) {
088: //ok
089: } finally {
090: // The drag source listener must not perform any operation
091: // since this drop adapter did the remove of the source even
092: // if we moved something.
093: //event.detail= DND.DROP_NONE;
094: }
095: clear();
096: return Status.OK_STATUS;
097: } else if (FileTransfer.getInstance().isSupportedType(
098: dropAdapter.getCurrentTransfer())) {
099: try {
100:
101: final Object data = FileTransfer.getInstance()
102: .nativeToJava(dropAdapter.getCurrentTransfer());
103: if (!(data instanceof String[]))
104: return Status.CANCEL_STATUS;
105:
106: final IContainer targetContainer = getActualTarget(target);
107: if (targetContainer == null)
108: return Status.CANCEL_STATUS;
109:
110: getShell().forceActive();
111: new CopyFilesAndFoldersOperation(getShell()).copyFiles(
112: (String[]) data, targetContainer);
113: } catch (JavaModelException e) {
114: String title = PackagesMessages.DropAdapter_errorTitle;
115: String message = PackagesMessages.DropAdapter_errorMessage;
116: ExceptionHandler.handle(e, getShell(), title, message);
117: }
118: return Status.OK_STATUS;
119: }
120: return Status.CANCEL_STATUS;
121: }
122:
123: public IStatus validateDrop(Object target, int operation,
124: TransferData transferType) {
125: IStatus result = Status.OK_STATUS;
126: if (LocalSelectionTransfer.getInstance().isSupportedType(
127: transferType)) {
128: initializeSelection();
129: try {
130: switch (operation) {
131: case DND.DROP_DEFAULT:
132: if (handleValidateDefault(target) != DND.DROP_NONE) {
133: result = Status.OK_STATUS;
134: } else {
135: result = Status.CANCEL_STATUS;
136: }
137: break;
138: case DND.DROP_COPY:
139: if (handleValidateCopy(target) != DND.DROP_NONE) {
140: result = Status.OK_STATUS;
141: } else {
142: result = Status.CANCEL_STATUS;
143: }
144: break;
145: case DND.DROP_MOVE:
146: if (handleValidateMove(target) != DND.DROP_NONE) {
147: result = Status.OK_STATUS;
148: } else {
149: result = Status.CANCEL_STATUS;
150: }
151: break;
152: }
153: } catch (JavaModelException e) {
154: ExceptionHandler
155: .handle(
156: e,
157: PackagesMessages.SelectionTransferDropAdapter_error_title,
158: PackagesMessages.SelectionTransferDropAdapter_error_message);
159: //event.detail= DND.DROP_NONE;
160: result = Status.CANCEL_STATUS;
161: }
162: }
163: return result;
164: }
165:
166: public boolean isSupportedType(TransferData transferType) {
167: return super .isSupportedType(transferType)
168: || FileTransfer.getInstance().isSupportedType(
169: transferType);
170: }
171:
172: private IContainer getActualTarget(Object dropTarget)
173: throws JavaModelException {
174: if (dropTarget instanceof IContainer)
175: return (IContainer) dropTarget;
176: else if (dropTarget instanceof IJavaElement)
177: return getActualTarget(((IJavaElement) dropTarget)
178: .getCorrespondingResource());
179: return null;
180: }
181:
182: protected void initializeSelection() {
183: if (fElements != null)
184: return;
185: ISelection s = LocalSelectionTransfer.getInstance()
186: .getSelection();
187: if (!(s instanceof IStructuredSelection))
188: return;
189: fElements = ((IStructuredSelection) s).toList();
190: }
191:
192: private void handleDropMove(final Object target)
193: throws JavaModelException, InvocationTargetException,
194: InterruptedException {
195: IJavaElement[] javaElements = ReorgUtils
196: .getJavaElements(fElements);
197: IResource[] resources = ReorgUtils.getResources(fElements);
198: ReorgMoveStarter starter = ReorgMoveStarter.create(
199: javaElements, resources, ReorgDestinationFactory
200: .createDestination(target));
201: ;
202:
203: if (starter != null)
204: starter.run(getShell());
205: }
206:
207: private void handleDropCopy(final Object target)
208: throws JavaModelException, InvocationTargetException,
209: InterruptedException {
210: IJavaElement[] javaElements = ReorgUtils
211: .getJavaElements(fElements);
212: IResource[] resources = ReorgUtils.getResources(fElements);
213: ReorgCopyStarter starter = ReorgCopyStarter.create(
214: javaElements, resources, ReorgDestinationFactory
215: .createDestination(target));
216:
217: if (starter != null)
218: starter.run(getShell());
219: }
220:
221: private int handleValidateCopy(Object target)
222: throws JavaModelException {
223:
224: final ICopyPolicy policy = ReorgPolicyFactory.createCopyPolicy(
225: ReorgUtils.getResources(fElements), ReorgUtils
226: .getJavaElements(fElements));
227: fCopyProcessor = policy.canEnable() ? new JavaCopyProcessor(
228: policy) : null;
229:
230: if (!canCopyElements())
231: return DND.DROP_NONE;
232:
233: if (fCopyProcessor == null)
234: return DND.DROP_NONE;
235:
236: if (!fCopyProcessor.setDestination(
237: ReorgDestinationFactory.createDestination(target))
238: .isOK())
239: return DND.DROP_NONE;
240:
241: return DND.DROP_COPY;
242: }
243:
244: private int handleValidateDefault(Object target)
245: throws JavaModelException {
246: if (target == null)
247: return DND.DROP_NONE;
248:
249: return handleValidateMove(target);
250: }
251:
252: private int handleValidateMove(Object target)
253: throws JavaModelException {
254: if (target == null)
255: return DND.DROP_NONE;
256:
257: IMovePolicy policy = ReorgPolicyFactory.createMovePolicy(
258: ReorgUtils.getResources(fElements), ReorgUtils
259: .getJavaElements(fElements));
260: fMoveProcessor = (policy.canEnable()) ? new JavaMoveProcessor(
261: policy) : null;
262:
263: if (!canMoveElements())
264: return DND.DROP_NONE;
265:
266: if (fMoveProcessor == null)
267: return DND.DROP_NONE;
268:
269: if (!fMoveProcessor.setDestination(
270: ReorgDestinationFactory.createDestination(target))
271: .isOK())
272: return DND.DROP_NONE;
273:
274: return DND.DROP_MOVE;
275: }
276:
277: private boolean canMoveElements() {
278: if (fCanMoveElements == 0) {
279: fCanMoveElements = 2;
280: if (fMoveProcessor == null)
281: fCanMoveElements = 1;
282: }
283: return fCanMoveElements == 2;
284: }
285:
286: private boolean canCopyElements() {
287: if (fCanCopyElements == 0) {
288: fCanCopyElements = 2;
289: if (fCopyProcessor == null)
290: fCanCopyElements = 1;
291: }
292: return fCanCopyElements == 2;
293: }
294:
295: private void clear() {
296: fElements = null;
297: fMoveProcessor = null;
298: fCanMoveElements = 0;
299: fCopyProcessor = null;
300: fCanCopyElements = 0;
301: }
302: }
|