KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > PluginDropAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.part;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.IExtension;
16 import org.eclipse.core.runtime.IExtensionPoint;
17 import org.eclipse.core.runtime.IExtensionRegistry;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.viewers.StructuredViewer;
20 import org.eclipse.jface.viewers.ViewerDropAdapter;
21 import org.eclipse.swt.dnd.DND;
22 import org.eclipse.swt.dnd.DropTargetEvent;
23 import org.eclipse.swt.dnd.TransferData;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.WorkbenchPlugin;
26 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
27
28 /**
29  * Adapter for adding handling of the <code>PluginTransfer</code> drag and drop
30  * transfer type to a drop action.
31  * <p>
32  * This class may be instantiated or subclassed.
33  * </p>
34  */

35 public class PluginDropAdapter extends ViewerDropAdapter {
36     /**
37      * The extension point attribute that defines the drop action class.
38      */

39     public static final String JavaDoc ATT_CLASS = "class";//$NON-NLS-1$
40

41     /**
42      * The current transfer data, or <code>null</code> if none.
43      */

44     private TransferData currentTransfer;
45
46     /**
47      * Creates a plug-in drop adapter for the given viewer.
48      *
49      * @param viewer the viewer
50      */

51     public PluginDropAdapter(StructuredViewer viewer) {
52         super(viewer);
53     }
54
55     /* (non-Javadoc)
56      * Method declared on DropTargetAdapter.
57      * The user has dropped something on the desktop viewer.
58      */

59     public void drop(DropTargetEvent event) {
60         try {
61             if (PluginTransfer.getInstance().isSupportedType(
62                     event.currentDataType)) {
63                 PluginTransferData pluginData = (PluginTransferData) event.data;
64                 IDropActionDelegate delegate = getPluginAdapter(pluginData);
65                 if (!delegate.run(pluginData.getData(), getCurrentTarget())) {
66                     event.detail = DND.DROP_NONE;
67                 }
68             } else {
69                 super.drop(event);
70             }
71         } catch (CoreException e) {
72             WorkbenchPlugin.log("Drop Failed", e.getStatus());//$NON-NLS-1$
73
}
74     }
75
76     /**
77      * Returns the current transfer.
78      */

79     protected TransferData getCurrentTransfer() {
80         return currentTransfer;
81     }
82
83     /**
84      * Loads the class that will perform the action associated with the given drop
85      * data.
86      *
87      * @param data the drop data
88      * @return the viewer drop adapter
89      */

90     protected static IDropActionDelegate getPluginAdapter(
91             PluginTransferData data) throws CoreException {
92
93         IExtensionRegistry registry = Platform.getExtensionRegistry();
94         String JavaDoc adapterName = data.getExtensionId();
95         IExtensionPoint xpt = registry.getExtensionPoint(PlatformUI.PLUGIN_ID,
96                 IWorkbenchRegistryConstants.PL_DROP_ACTIONS);
97         IExtension[] extensions = xpt.getExtensions();
98         for (int i = 0; i < extensions.length; i++) {
99             IConfigurationElement[] configs = extensions[i]
100                     .getConfigurationElements();
101             if (configs != null && configs.length > 0) {
102                 String JavaDoc id = configs[0].getAttribute("id");//$NON-NLS-1$
103
if (id != null && id.equals(adapterName)) {
104                     return (IDropActionDelegate) WorkbenchPlugin
105                             .createExtension(configs[0], ATT_CLASS);
106                 }
107             }
108         }
109         return null;
110     }
111
112     /**
113      * @see ViewerDropAdapter#performDrop
114      */

115     public boolean performDrop(Object JavaDoc data) {
116         //should never be called, since we override the drop() method.
117
return false;
118     }
119
120     /**
121      * The <code>PluginDropAdapter</code> implementation of this
122      * <code>ViewerDropAdapter</code> method is used to notify the action that some
123      * aspect of the drop operation has changed. Subclasses may override.
124      */

125     public boolean validateDrop(Object JavaDoc target, int operation,
126             TransferData transferType) {
127         currentTransfer = transferType;
128         if (currentTransfer != null
129                 && PluginTransfer.getInstance()
130                         .isSupportedType(currentTransfer)) {
131             //plugin cannot be loaded without the plugin data
132
return true;
133         }
134         return false;
135     }
136 }
137
Popular Tags