|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2009 Tasktop Technologies 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 |
* Tasktop Technologies - initial API and implementation |
| 10 |
* Jevgeni Holodkov - improvements |
| 11 |
*******************************************************************************/ |
| 12 |
|
| 13 |
package org.eclipse.mylyn.internal.tasks.ui.util; |
| 14 |
|
| 15 |
import java.io.File; |
| 16 |
import java.io.IOException; |
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.Iterator; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import org.eclipse.core.runtime.IStatus; |
| 22 |
import org.eclipse.core.runtime.Status; |
| 23 |
import org.eclipse.jface.util.LocalSelectionTransfer; |
| 24 |
import org.eclipse.jface.viewers.ISelection; |
| 25 |
import org.eclipse.jface.viewers.ISelectionProvider; |
| 26 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 27 |
import org.eclipse.mylyn.commons.core.StatusHandler; |
| 28 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
| 29 |
import org.eclipse.mylyn.internal.tasks.ui.actions.CopyTaskDetailsAction; |
| 30 |
import org.eclipse.mylyn.internal.tasks.ui.actions.SaveAttachmentsAction; |
| 31 |
import org.eclipse.mylyn.tasks.core.ITaskAttachment; |
| 32 |
import org.eclipse.swt.dnd.DragSourceAdapter; |
| 33 |
import org.eclipse.swt.dnd.DragSourceEvent; |
| 34 |
import org.eclipse.swt.dnd.FileTransfer; |
| 35 |
import org.eclipse.swt.dnd.TextTransfer; |
| 36 |
import org.eclipse.swt.dnd.URLTransfer; |
| 37 |
|
| 38 |
/** |
| 39 |
* @author Steffen Pingel |
| 40 |
*/ |
| 41 |
public class TaskAttachmentDragSourceListener extends DragSourceAdapter { |
| 42 |
|
| 43 |
private IStructuredSelection currentSelection; |
| 44 |
|
| 45 |
private final ISelectionProvider selectionProvider; |
| 46 |
|
| 47 |
public TaskAttachmentDragSourceListener(ISelectionProvider selectionProvider) { |
| 48 |
this.selectionProvider = selectionProvider; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void dragStart(DragSourceEvent event) { |
| 53 |
ISelection selection = selectionProvider.getSelection(); |
| 54 |
if (selection instanceof IStructuredSelection && !selection.isEmpty()) { |
| 55 |
this.currentSelection = (IStructuredSelection) selection; |
| 56 |
} else { |
| 57 |
this.currentSelection = null; |
| 58 |
event.doit = false; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
private List<ITaskAttachment> getAttachments(IStructuredSelection selection) { |
| 63 |
List<ITaskAttachment> attachments = new ArrayList<ITaskAttachment>(); |
| 64 |
Iterator<?> it = currentSelection.iterator(); |
| 65 |
while (it.hasNext()) { |
| 66 |
Object item = it.next(); |
| 67 |
if (item instanceof ITaskAttachment) { |
| 68 |
attachments.add((ITaskAttachment) item); |
| 69 |
} |
| 70 |
} |
| 71 |
return attachments; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void dragSetData(DragSourceEvent event) { |
| 76 |
if (currentSelection == null || currentSelection.isEmpty()) { |
| 77 |
event.doit = false; |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataType)) { |
| 82 |
LocalSelectionTransfer.getTransfer().setSelection(currentSelection); |
| 83 |
} else if (FileTransfer.getInstance().isSupportedType(event.dataType)) { |
| 84 |
List<ITaskAttachment> attachments = getAttachments(currentSelection); |
| 85 |
if (!attachments.isEmpty()) { |
| 86 |
String[] paths = new String[attachments.size()]; |
| 87 |
File directory; |
| 88 |
try { |
| 89 |
directory = File.createTempFile("attachments", null); //$NON-NLS-1$ |
| 90 |
} catch (IOException e) { |
| 91 |
StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, |
| 92 |
"Problems encountered dragging attachment", //$NON-NLS-1$ |
| 93 |
e)); |
| 94 |
event.doit = false; |
| 95 |
return; |
| 96 |
} |
| 97 |
directory.delete(); |
| 98 |
directory.mkdirs(); |
| 99 |
int i = 0; |
| 100 |
for (ITaskAttachment attachment : attachments) { |
| 101 |
String filename = SaveAttachmentsAction.getAttachmentFilename(attachment); |
| 102 |
File file = new File(directory, filename); |
| 103 |
DownloadAttachmentJob job = new DownloadAttachmentJob(attachment, file); |
| 104 |
job.setUser(true); |
| 105 |
job.schedule(); |
| 106 |
paths[i] = file.getAbsolutePath(); |
| 107 |
i++; |
| 108 |
} |
| 109 |
event.data = paths; |
| 110 |
} else { |
| 111 |
event.doit = false; |
| 112 |
} |
| 113 |
} else if (TextTransfer.getInstance().isSupportedType(event.dataType)) { |
| 114 |
event.data = CopyTaskDetailsAction.getTextForTask(currentSelection.getFirstElement()); |
| 115 |
} else if (URLTransfer.getInstance().isSupportedType(event.dataType)) { |
| 116 |
List<ITaskAttachment> attachments = getAttachments(currentSelection); |
| 117 |
if (!attachments.isEmpty()) { |
| 118 |
event.data = attachments.get(0).getUrl(); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void dragFinished(DragSourceEvent event) { |
| 125 |
if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataType)) { |
| 126 |
LocalSelectionTransfer.getTransfer().setSelection(null); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
} |