Skip to main content
summaryrefslogtreecommitdiffstats
blob: a35029268d7bbd15cb36905a46cbed75df94f4d1 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.eclipse.mylyn.internal.tasks.ui;

import java.text.MessageFormat;

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.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.core.MylarStatusHandler;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.AbstractTaskContainer;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.TaskCategory;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Retrieves an existing repository task and adds it to the tasklist
 * 
 * @author Willian Mitsuda
 */
public class AddExistingTaskJob extends Job {

	/**
	 * Task repository whose task will be added
	 */
	private TaskRepository repository;

	/**
	 * Identifies a existing task on the repository
	 */
	private String taskId;

	/**
	 * Optional; informs the task container the task initialy belongs to; if
	 * null, it will be added to the current selected task's category in task
	 * list
	 */
	private AbstractTaskContainer taskContainer;

	public AddExistingTaskJob(TaskRepository repository, String taskId) {
		this(repository, taskId, null);
	}

	public AddExistingTaskJob(TaskRepository repository, String taskId, AbstractTaskContainer taskContainer) {
		super(MessageFormat.format("Adding task: \"{0}\"...", taskId));
		this.repository = repository;
		this.taskId = taskId;
		this.taskContainer = taskContainer;
	}

	@Override
	public IStatus run(IProgressMonitor monitor) {
		final AbstractRepositoryConnector connector = TasksUiPlugin.getRepositoryManager().getRepositoryConnector(
				repository.getKind());
		try {
			final ITask newTask = connector.createTaskFromExistingId(repository, taskId, monitor);

//			if (newTask instanceof AbstractRepositoryTask) {
//				// TODO: encapsulate in abstract connector
//				AbstractRepositoryTask repositoryTask = (AbstractRepositoryTask) newTask;
//				TasksUiPlugin.getDefault().getTaskDataManager().push(newTask.getHandleIdentifier(),
//						repositoryTask.getTaskData());
//			}

			if (newTask != null) {
				TasksUiUtil.refreshAndOpenTaskListElement(newTask);
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

					public void run() {
						AbstractTaskContainer category = taskContainer;
						TaskListView taskListView = TaskListView.getFromActivePerspective();
						if (category == null) {
							Object selectedObject = ((IStructuredSelection) taskListView.getViewer().getSelection())
									.getFirstElement();
							if (selectedObject instanceof TaskCategory) {
								category = (TaskCategory) selectedObject;
							} else {
								category = TasksUiPlugin.getTaskListManager().getTaskList().getUncategorizedCategory();
							}
						}
						TasksUiPlugin.getTaskListManager().getTaskList().moveToContainer(category, newTask);
						taskListView.getViewer().setSelection(new StructuredSelection(newTask));
					}

				});
			} else {
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

					public void run() {
						IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
						if (window != null) {
							MessageDialog.openWarning(window.getShell(), "Add Existing Task Failed", MessageFormat
									.format("Unable to retrieve task \"{0}\" from repository.", taskId));
						}
					}

				});
			}
		} catch (final CoreException e) {
			MylarStatusHandler.fail(e.getStatus().getException(), e.getMessage(), true);
		} finally {
			monitor.done();
		}
		return Status.OK_STATUS;
	}

}

Back to the top