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.ui.navigator.resources;
011:
012: import java.util.Iterator;
013: import java.util.LinkedHashSet;
014: import java.util.Set;
015:
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.swt.dnd.DragSourceEvent;
022: import org.eclipse.swt.dnd.FileTransfer;
023: import org.eclipse.swt.dnd.Transfer;
024: import org.eclipse.ui.navigator.CommonDragAdapterAssistant;
025: import org.eclipse.ui.navigator.INavigatorDnDService;
026: import org.eclipse.ui.part.ResourceTransfer;
027: import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
028:
029: /**
030: * Clients may reference this class in the <b>dragAssistant</b> element of a
031: * <b>org.eclipse.ui.navigator.viewer</b> extension point.
032: *
033: * <p>
034: * Clients may not extend or instantiate this class for any purpose other than
035: * {@link INavigatorDnDService#bindDragAssistant(CommonDragAdapterAssistant)}.
036: * Clients may have no direct dependencies on the contract of this class.
037: * </p>
038: *
039: * @since 3.2
040: *
041: */
042: public final class ResourceDragAdapterAssistant extends
043: CommonDragAdapterAssistant {
044:
045: private static final boolean DEBUG = false;
046:
047: private static final Transfer[] SUPPORTED_TRANSFERS = new Transfer[] {
048: ResourceTransfer.getInstance(),
049: LocalSelectionTransfer.getInstance(),
050: FileTransfer.getInstance() };
051:
052: private static final Class IRESOURCE_TYPE = IResource.class;
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.eclipse.ui.navigator.CommonDragAdapterAssistant#getSupportedTransferTypes()
058: */
059: public Transfer[] getSupportedTransferTypes() {
060: return SUPPORTED_TRANSFERS;
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see org.eclipse.ui.navigator.CommonDragAdapterAssistant#setDragData(org.eclipse.swt.dnd.DragSourceEvent,
067: * org.eclipse.jface.viewers.IStructuredSelection)
068: */
069: public boolean setDragData(DragSourceEvent anEvent,
070: IStructuredSelection aSelection) {
071:
072: if (LocalSelectionTransfer.getInstance().isSupportedType(
073: anEvent.dataType)) {
074: anEvent.data = aSelection;
075: if (DEBUG) {
076: System.out
077: .println("ResourceDragAdapterAssistant.dragSetData set LocalSelectionTransfer"); //$NON-NLS-1$
078: }
079: return true;
080: } else if (ResourceTransfer.getInstance().isSupportedType(
081: anEvent.dataType)) {
082: anEvent.data = getSelectedResources(aSelection);
083: if (DEBUG) {
084: System.out
085: .println("ResourceDragAdapterAssistant.dragSetData set ResourceTransfer"); //$NON-NLS-1$
086: }
087: return true;
088: } else if (FileTransfer.getInstance().isSupportedType(
089: anEvent.dataType)) {
090:
091: IResource[] resources = getSelectedResources(aSelection);
092:
093: // Get the path of each file and set as the drag data
094: final int length = resources.length;
095: int actualLength = 0;
096: String[] fileNames = new String[length];
097: for (int i = 0; i < length; i++) {
098: IPath location = resources[i].getLocation();
099: // location may be null. See bug 29491.
100: if (location != null) {
101: fileNames[actualLength++] = location.toOSString();
102: }
103: }
104: if (actualLength > 0) {
105: // was one or more of the locations null?
106: if (actualLength < length) {
107: String[] tempFileNames = fileNames;
108: fileNames = new String[actualLength];
109: for (int i = 0; i < actualLength; i++)
110: fileNames[i] = tempFileNames[i];
111: }
112: anEvent.data = fileNames;
113:
114: if (DEBUG)
115: System.out
116: .println("ResourceDragAdapterAssistant.dragSetData set FileTransfer"); //$NON-NLS-1$
117: return true;
118: }
119: }
120: return false;
121:
122: }
123:
124: private IResource[] getSelectedResources(
125: IStructuredSelection aSelection) {
126: Set resources = new LinkedHashSet();
127: IResource resource = null;
128: for (Iterator iter = aSelection.iterator(); iter.hasNext();) {
129: Object selected = iter.next();
130: resource = adaptToResource(selected);
131: if (resource != null) {
132: resources.add(resource);
133: }
134: }
135: return (IResource[]) resources.toArray(new IResource[resources
136: .size()]);
137: }
138:
139: private IResource adaptToResource(Object selected) {
140: IResource resource;
141: if (selected instanceof IResource) {
142: resource = (IResource) selected;
143: } else if (selected instanceof IAdaptable) {
144: resource = (IResource) ((IAdaptable) selected)
145: .getAdapter(IRESOURCE_TYPE);
146: } else {
147: resource = (IResource) Platform.getAdapterManager()
148: .getAdapter(selected, IRESOURCE_TYPE);
149: }
150: return resource;
151: }
152:
153: }
|