001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.part;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.core.runtime.IExtension;
015: import org.eclipse.core.runtime.IExtensionPoint;
016: import org.eclipse.core.runtime.IExtensionRegistry;
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.jface.viewers.StructuredViewer;
019: import org.eclipse.jface.viewers.ViewerDropAdapter;
020: import org.eclipse.swt.dnd.DND;
021: import org.eclipse.swt.dnd.DropTargetEvent;
022: import org.eclipse.swt.dnd.TransferData;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
026:
027: /**
028: * Adapter for adding handling of the <code>PluginTransfer</code> drag and drop
029: * transfer type to a drop action.
030: * <p>
031: * This class may be instantiated or subclassed.
032: * </p>
033: */
034: public class PluginDropAdapter extends ViewerDropAdapter {
035: /**
036: * The extension point attribute that defines the drop action class.
037: */
038: public static final String ATT_CLASS = "class";//$NON-NLS-1$
039:
040: /**
041: * The current transfer data, or <code>null</code> if none.
042: */
043: private TransferData currentTransfer;
044:
045: /**
046: * Creates a plug-in drop adapter for the given viewer.
047: *
048: * @param viewer the viewer
049: */
050: public PluginDropAdapter(StructuredViewer viewer) {
051: super (viewer);
052: }
053:
054: /* (non-Javadoc)
055: * Method declared on DropTargetAdapter.
056: * The user has dropped something on the desktop viewer.
057: */
058: public void drop(DropTargetEvent event) {
059: try {
060: if (PluginTransfer.getInstance().isSupportedType(
061: event.currentDataType)) {
062: PluginTransferData pluginData = (PluginTransferData) event.data;
063: IDropActionDelegate delegate = getPluginAdapter(pluginData);
064: if (!delegate.run(pluginData.getData(),
065: getCurrentTarget())) {
066: event.detail = DND.DROP_NONE;
067: }
068: } else {
069: super .drop(event);
070: }
071: } catch (CoreException e) {
072: WorkbenchPlugin.log("Drop Failed", e.getStatus());//$NON-NLS-1$
073: }
074: }
075:
076: /**
077: * Returns the current transfer.
078: */
079: protected TransferData getCurrentTransfer() {
080: return currentTransfer;
081: }
082:
083: /**
084: * Loads the class that will perform the action associated with the given drop
085: * data.
086: *
087: * @param data the drop data
088: * @return the viewer drop adapter
089: */
090: protected static IDropActionDelegate getPluginAdapter(
091: PluginTransferData data) throws CoreException {
092:
093: IExtensionRegistry registry = Platform.getExtensionRegistry();
094: String adapterName = data.getExtensionId();
095: IExtensionPoint xpt = registry.getExtensionPoint(
096: PlatformUI.PLUGIN_ID,
097: IWorkbenchRegistryConstants.PL_DROP_ACTIONS);
098: IExtension[] extensions = xpt.getExtensions();
099: for (int i = 0; i < extensions.length; i++) {
100: IConfigurationElement[] configs = extensions[i]
101: .getConfigurationElements();
102: if (configs != null && configs.length > 0) {
103: String id = configs[0].getAttribute("id");//$NON-NLS-1$
104: if (id != null && id.equals(adapterName)) {
105: return (IDropActionDelegate) WorkbenchPlugin
106: .createExtension(configs[0], ATT_CLASS);
107: }
108: }
109: }
110: return null;
111: }
112:
113: /**
114: * @see ViewerDropAdapter#performDrop
115: */
116: public boolean performDrop(Object data) {
117: //should never be called, since we override the drop() method.
118: return false;
119: }
120:
121: /**
122: * The <code>PluginDropAdapter</code> implementation of this
123: * <code>ViewerDropAdapter</code> method is used to notify the action that some
124: * aspect of the drop operation has changed. Subclasses may override.
125: */
126: public boolean validateDrop(Object target, int operation,
127: TransferData transferType) {
128: currentTransfer = transferType;
129: if (currentTransfer != null
130: && PluginTransfer.getInstance().isSupportedType(
131: currentTransfer)) {
132: //plugin cannot be loaded without the plugin data
133: return true;
134: }
135: return false;
136: }
137: }
|