Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8828f7dbcd2d9891cf776764416225621991d7da (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
 *     Ken Sueda - original prototype
 *******************************************************************************/

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskContainer;

/**
 * Maintains a list of tasks that have been activated in the past. Each task only occurs once in the list. The list is
 * sorted by most recent activation, i.e. the task with the highest index is the task that was most recently activated.
 * 
 * @author Wesley Coelho (Added persistent tasks)
 * @author Mik Kersten (hardening)
 * @author Rob Elves
 * @author Steffen Pingel
 */
public class TaskActivationHistory {

	/**
	 * The most recently activated task has the highest index in the list.
	 */
	private final List<AbstractTask> history = new ArrayList<AbstractTask>();

	/**
	 * Index pointing to the task that was previously active.
	 */
	private int previousIndex = -1;

	public void addTask(AbstractTask task) {
		boolean isPreviousTask = false;
		// optimization: do not modify list, if task is already last
		if (history.isEmpty() || history.get(history.size() - 1) != task) {
			if (previousIndex >= 0 && previousIndex < history.size() && history.get(previousIndex) == task) {
				isPreviousTask = true;
			}
			history.remove(task);
			history.add(task);
		}
		if (isPreviousTask) {
			// the previous task was activated, move the cursor
			previousIndex--;
		} else {
			previousIndex = history.size() - 2;
		}
	}

	public boolean containsTask(ITask task) {
		return history.contains(task);
	}

	public boolean removeTask(ITask task) {
		return history.remove(task);
	}

	public AbstractTask getPreviousTask() {
		if (history.isEmpty()) {
			return null;
		}
		AbstractTask currentTask = history.get(history.size() - 1);
		if (currentTask.isActive() && previousIndex >= 0 && previousIndex < history.size()) {
			return history.get(previousIndex);
		} else {
			return currentTask;
		}
	}

	public List<AbstractTask> getPreviousTasks() {
		return Collections.unmodifiableList(new ArrayList<AbstractTask>(history));
	}

	/**
	 * Returns task activation history for tasks present in <code>containers</code>
	 */
	public List<AbstractTask> getPreviousTasks(Set<AbstractTaskContainer> containers) {
		if (containers.isEmpty()) {
			return getPreviousTasks();
		}
		Set<ITask> allWorkingSetTasks = new HashSet<ITask>();
		for (ITaskContainer container : containers) {
			allWorkingSetTasks.addAll(container.getChildren());
		}
		List<AbstractTask> allScopedTasks = new ArrayList<AbstractTask>(getPreviousTasks());
		for (Iterator<AbstractTask> it = allScopedTasks.iterator(); it.hasNext();) {
			AbstractTask task = it.next();
			if (!allWorkingSetTasks.contains(task)) {
				it.remove();
			}
		}
		return Collections.unmodifiableList(allScopedTasks);
	}

	public boolean hasPrevious() {
		return getPreviousTask() != null;
	}

	public void clear() {
		history.clear();
		previousIndex = -1;
	}

	public int indexOf(ITask task) {
		return history.indexOf(task);

	}

	public int getSize() {
		return history.size();
	}

}

Back to the top