001: /*******************************************************************************
002: * Copyright (c) 2003, 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.ui.internal.navigator.dnd;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.jface.util.LocalSelectionTransfer;
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
016: import org.eclipse.ui.navigator.INavigatorContentService;
017: import org.eclipse.ui.part.IDropActionDelegate;
018: import org.eclipse.ui.part.PluginTransferData;
019:
020: /**
021: *
022: *
023: * @since 3.2
024: *
025: */
026: public class NavigatorPluginDropAction implements IDropActionDelegate {
027:
028: private static final boolean DEBUG = false;
029:
030: private static final String CN_PLUGIN_ACTION_ID = "org.eclipse.ui.navigator.PluginDropAction"; //$NON-NLS-1$
031:
032: /**
033: * A default no-args constructor is required by the
034: * <b>org.eclipse.ui.dropAdapters</b> extension point
035: */
036: public NavigatorPluginDropAction() {
037: super ();
038: }
039:
040: /*
041: * (non-Javadoc)
042: *
043: * @see org.eclipse.ui.part.IDropActionDelegate#run(java.lang.Object,
044: * java.lang.Object)
045: */
046: public boolean run(Object sourceData, Object target) {
047:
048: if (DEBUG) {
049: System.out.println("NavigatorPluginDropAction.run (begin)"); //$NON-NLS-1$
050: }
051:
052: String sourceViewerId = new String((byte[]) sourceData);
053:
054: IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer
055: .getTransfer().getSelection();
056:
057: INavigatorContentService contentService = NavigatorContentServiceTransfer
058: .getInstance().findService(sourceViewerId);
059:
060: if (contentService == null) {
061: return false;
062: }
063: try {
064: CommonDropAdapterAssistant[] assistants = contentService
065: .getDnDService().findCommonDropAdapterAssistants(
066: target, selection);
067:
068: IStatus valid = null;
069: for (int i = 0; i < assistants.length; i++) {
070: valid = assistants[i].validatePluginTransferDrop(
071: selection, target);
072: if (valid != null && valid.isOK()) {
073: valid = assistants[i].handlePluginTransferDrop(
074: selection, target);
075: return valid != null && valid.isOK();
076: }
077: }
078: } finally {
079: NavigatorContentServiceTransfer.getInstance()
080: .unregisterContentService(contentService);
081: }
082:
083: if (DEBUG) {
084: System.out.println("NavigatorPluginDropAction.run (exit)"); //$NON-NLS-1$
085: }
086:
087: return false;
088: }
089:
090: /**
091: *
092: * @param aContentService
093: * The associated content service that is the source of the drag
094: * @return A PluginTransferData properly configured to call the Common
095: * Navigator's PluginDropAction.
096: */
097: public static PluginTransferData createTransferData(
098: INavigatorContentService aContentService) {
099: NavigatorContentServiceTransfer.getInstance()
100: .registerContentService(aContentService);
101: return new PluginTransferData(CN_PLUGIN_ACTION_ID,
102: aContentService.getViewerId().getBytes());
103: }
104:
105: }
|