Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4786048f4ae6724e0c98cc7fa2380834fc2c0a14 (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
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

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

import java.text.MessageFormat;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.internal.tasks.core.AbstractTaskCategory;
import org.eclipse.mylyn.internal.tasks.core.TaskCategory;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.ui.PlatformUI;

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

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

	/**
	 * Identifies a existing task on the repository
	 */
	private final 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 final AbstractTaskCategory taskContainer;

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

	public AddExistingTaskJob(TaskRepository repository, String taskId, AbstractTaskCategory taskContainer) {
		super(MessageFormat.format(Messages.AddExistingTaskJob_Adding_task_X_, taskId));
		this.repository = repository;
		this.taskId = taskId;
		this.taskContainer = taskContainer;
	}

	@Override
	public IStatus run(IProgressMonitor monitor) {
		OpenRepositoryTaskJob job = new OpenRepositoryTaskJob(repository.getConnectorKind(),
				repository.getRepositoryUrl(), taskId, null, null);
		IStatus result = job.run(monitor);
		final ITask newTask = job.getTask();
		if (newTask != null) {
			PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
				public void run() {
					AbstractTaskCategory category = taskContainer;
					TaskListView taskListView = TaskListView.getFromActivePerspective();
					if (category == null) {
						Object selectedObject = ((IStructuredSelection) taskListView.getViewer().getSelection()).getFirstElement();
						if (selectedObject instanceof TaskCategory) {
							category = (TaskCategory) selectedObject;
						}
					}
					TasksUiInternal.getTaskList().addTask(newTask, category);
					taskListView.getViewer().setSelection(new StructuredSelection(newTask));
				}
			});
		}
		return result;
//		try {
//			TasksUiUtil.openTask(repository, taskId);
//			final AbstractTask newTask = (AbstractTask) TasksUiInternal.createTask(repository, taskId, monitor);
//			if (newTask != null) {
//				TasksUiPlugin.getTaskActivityManager().setScheduledFor(newTask, TaskActivityUtil.getCurrentWeek());
//
//				TasksUiInternal.refreshAndOpenTaskListElement(newTask);
//				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
//
//					public void run() {
//						AbstractTaskCategory category = taskContainer;
//						TaskListView taskListView = TaskListView.getFromActivePerspective();
//						if (category == null) {
//							Object selectedObject = ((IStructuredSelection) taskListView.getViewer().getSelection()).getFirstElement();
//							if (selectedObject instanceof TaskCategory) {
//								category = (TaskCategory) selectedObject;
//							}
//						}
//						TasksUiInternal.getTaskList().addTask(newTask, category);
//						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) {
//			TasksUiInternal.asyncDisplayStatus("Unable to open task", e.getStatus());
//		} finally {
//			monitor.done();
//		}
//		return Status.OK_STATUS;
	}
}

Back to the top