01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.views.plugins;
11:
12: import java.util.ArrayList;
13: import java.util.Iterator;
14:
15: import org.eclipse.jface.viewers.ISelectionProvider;
16: import org.eclipse.jface.viewers.IStructuredSelection;
17: import org.eclipse.pde.internal.core.FileAdapter;
18: import org.eclipse.swt.dnd.DragSource;
19: import org.eclipse.swt.dnd.DragSourceAdapter;
20: import org.eclipse.swt.dnd.DragSourceEvent;
21: import org.eclipse.swt.dnd.FileTransfer;
22: import org.eclipse.swt.widgets.Control;
23:
24: public class PluginsDragAdapter extends DragSourceAdapter {
25: ISelectionProvider selectionProvider;
26:
27: /**
28: * NavigatorDragAction constructor comment.
29: */
30: public PluginsDragAdapter(ISelectionProvider provider) {
31: selectionProvider = provider;
32: }
33:
34: /**
35: * Returns the data to be transferred in a drag and drop
36: * operation.
37: */
38: public void dragSetData(DragSourceEvent event) {
39:
40: //resort to a file transfer
41: if (!FileTransfer.getInstance().isSupportedType(event.dataType))
42: return;
43:
44: FileAdapter[] files = getSelectedFiles();
45:
46: // Get the path of each file and set as the drag data
47: final int len = files.length;
48: String[] fileNames = new String[len];
49: for (int i = 0, length = len; i < length; i++) {
50: fileNames[i] = files[i].getFile().getAbsolutePath();
51: }
52: event.data = fileNames;
53: }
54:
55: /**
56: * All selection must be files or folders.
57: */
58: public void dragStart(DragSourceEvent event) {
59:
60: // Workaround for 1GEUS9V
61: DragSource dragSource = (DragSource) event.widget;
62: Control control = dragSource.getControl();
63: if (control != control.getDisplay().getFocusControl()) {
64: event.doit = false;
65: return;
66: }
67:
68: FileAdapter[] files = getSelectedFiles();
69:
70: if (files.length == 0) {
71: event.doit = false;
72: return;
73: }
74: event.doit = true;
75: }
76:
77: private FileAdapter[] getSelectedFiles() {
78: IStructuredSelection selection = (IStructuredSelection) selectionProvider
79: .getSelection();
80: ArrayList files = new ArrayList();
81: for (Iterator iter = selection.iterator(); iter.hasNext();) {
82: Object obj = iter.next();
83: if (obj instanceof FileAdapter)
84: files.add(obj);
85: else
86: return new FileAdapter[0];
87: }
88: return (FileAdapter[]) files.toArray(new FileAdapter[files
89: .size()]);
90: }
91: }
|