Skip to main content
summaryrefslogtreecommitdiffstats
blob: de6d78417e4ab164430dec363098df9a9c75151d (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project 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.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;

/**
 * @author Ken Sueda (original prototype)
 * @author Wesley Coelho (Added persistent tasks)
 * @author Mik Kersten (hardening)
 * @author Rob Elves
 */
public class TaskActivationHistory {

	private final List<AbstractTask> history = new ArrayList<AbstractTask>();

	private int currentIndex = -1;

	public void addTask(AbstractTask task) {
		history.remove(task);
		history.add(task);
		currentIndex = history.size() - 1;
	}

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

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

	public AbstractTask getPreviousTask() {
		boolean active = false;
		for (AbstractTask task : history) {
			if (task.isActive()) {
				active = true;
				break;
			}
		}

		if (hasPrevious()) {
			if (!active) {
				return history.get(currentIndex);
			}

			if (currentIndex < history.size() - 1 && ((currentIndex == 0 && !history.get(currentIndex).isActive()))) {
				return history.get(currentIndex);
			} else if (currentIndex > 0 && currentIndex < history.size()) {
				return history.get(--currentIndex);
			}
		}
		return null;
	}

	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 (currentIndex == 0 && !history.get(currentIndex).isActive()) || currentIndex > 0;
	}

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

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

	}

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

}

Back to the top