Skip to main content
summaryrefslogtreecommitdiffstats
blob: cc1c4ea786cd5f2814f3fcd1ca79f5669fedfbc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*******************************************************************************
 * Copyright (c) 2004 - 2006 Mylar committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.actions;

import java.io.ByteArrayOutputStream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.core.MylarStatusHandler;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IAttachmentHandler;
import org.eclipse.mylyn.tasks.core.IMylarStatusConstants;
import org.eclipse.mylyn.tasks.core.MylarStatus;
import org.eclipse.mylyn.tasks.core.RepositoryAttachment;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.ui.PlatformUI;

/**
 * @author Steffen Pingel
 */
public class CopyAttachmentToClipboardJob extends Job {

	private RepositoryAttachment attachment;

	public CopyAttachmentToClipboardJob(RepositoryAttachment attachment) {
		super("Copying Attachment to Clipboard");

		this.attachment = attachment;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		TaskRepository repository = TasksUiPlugin.getRepositoryManager().getRepository(attachment.getRepositoryKind(),
				attachment.getRepositoryUrl());
		AbstractRepositoryConnector connector = TasksUiPlugin.getRepositoryManager().getRepositoryConnector(
				attachment.getRepositoryKind());
		IAttachmentHandler handler = connector.getAttachmentHandler();
		if (handler == null) {
			return new MylarStatus(IStatus.INFO, TasksUiPlugin.PLUGIN_ID, IMylarStatusConstants.INTERNAL_ERROR,
					"The repository does not support attachments.");
		}

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			handler.downloadAttachment(repository, attachment, out, monitor);
		} catch (final CoreException e) {
			MylarStatusHandler.displayStatus("Copy Attachment to Clipboard", e.getStatus());
			return Status.OK_STATUS;
		}
		
		String contents = new String(out.toByteArray());
		contents = contents.replaceAll("\r\n|\n", System.getProperty("line.separator"));
		copyToClipboard(contents);
		
		return Status.OK_STATUS;
	}

	/** For testing. */
	public void copyToClipboard(final String contents) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench().getDisplay());
				clipboard.setContents(new Object[] { contents }, new Transfer[] { TextTransfer.getInstance() });
				clipboard.dispose();
			}
		});
	}

}

Back to the top