Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6609b7d9a6e62884479d386ed537e938247a5e4e (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
83
84
85
86
87
88
89
90
91
92
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.internal.tasks.ui.actions;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylar.internal.tasks.ui.TaskListImages;
import org.eclipse.mylar.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylar.tasks.core.AbstractQueryHit;
import org.eclipse.mylar.tasks.core.AbstractRepositoryQuery;
import org.eclipse.mylar.tasks.core.AbstractRepositoryTask;
import org.eclipse.mylar.tasks.core.ITask;
import org.eclipse.mylar.tasks.core.ITaskListElement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.actions.BaseSelectionListenerAction;

/**
 * @author Mik Kersten
 */
public class CopyTaskDetailsAction extends BaseSelectionListenerAction {

	private static final String LABEL = "Copy Details";

	public static final String ID = "org.eclipse.mylar.tasklist.actions.copy";

	public CopyTaskDetailsAction(boolean setAccelerator) {
		super(LABEL);
		setToolTipText(LABEL);
		setId(ID);
		setImageDescriptor(TaskListImages.COPY);
		if (setAccelerator) {
			setAccelerator(SWT.MOD1 + 'c');
		}
	}

	@Override
	public void run() {
		ISelection selection = super.getStructuredSelection();
		Object object = ((IStructuredSelection) selection).getFirstElement();
		String text = getTextForTask(object); 

		// HACK: this should be done using proper copying
		Composite dummyComposite = TaskListView.getFromActivePerspective().getFilteredTree();
		StyledText styledText = new StyledText(dummyComposite, SWT.NULL);
		styledText.setText(text);
		styledText.selectAll();
		styledText.copy();
		styledText.dispose();
	}

	public static String getTextForTask(Object object) {
		String text = "";
		if (object instanceof ITask || object instanceof AbstractQueryHit) {
			ITask task = null;
			if (object instanceof AbstractQueryHit) {
				task = ((AbstractQueryHit)object).getCorrespondingTask();
			} else if (object instanceof ITask) {
				task = (ITask)object;
			}
			if (task != null) {
				if (task instanceof AbstractRepositoryTask) {
					text += ((AbstractRepositoryTask)task).getIdLabel() + ": ";
				}
				
				text += task.getSummary();
				if (task.hasValidUrl()) {
					text += "\n" + task.getUrl();
				} 
			} else {
				text += ((AbstractQueryHit)object).getSummary();
			}
		} else if (object instanceof AbstractRepositoryQuery) {
			AbstractRepositoryQuery query = (AbstractRepositoryQuery)object;
			text += query.getSummary();
			text += "\n" + query.getUrl();
		} else if (object instanceof ITaskListElement) {
			ITaskListElement element = (ITaskListElement) object;
			text = element.getSummary();
		}
		return text;
	}
}

Back to the top