Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8a09d59cc5bb77c08b696cda4ea8c8d88267cea0 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * 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.mylyn.internal.tasks.ui.views;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.mylyn.context.core.ContextCorePlugin;
import org.eclipse.mylyn.monitor.core.InteractionEvent;
import org.eclipse.mylyn.monitor.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;

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

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

	private int currentIndex = -1;

	/**
	 * The number of tasks from the previous Eclipse session to load into the
	 * history at startup. (This is not the maximum size of the history, which
	 * is currently unbounded)
	 */
	private static final int NUM_SAVED_HISTORY_ITEMS_TO_LOAD = 12;

	private boolean persistentHistoryLoaded = false;

	/**
	 * Load in a number of saved history tasks from previous session. Should be
	 * called from constructor but ContextManager doesn't seem to be able to
	 * provide activity history at that point
	 * 
	 * @author Wesley Coelho
	 */
	public void loadPersistentHistory() {
		int tasksAdded = 0;
		history.clear();
		for (int i = ContextCorePlugin.getContextManager().getActivityMetaContext().getInteractionHistory()
				.size() - 1; i >= 0; i--) {
			AbstractTask prevTask = getHistoryTaskAt(i);

			if (prevTask != null && !history.contains(prevTask)) {
				// !isDuplicate(prevTask, i + 1)) {
				history.add(0, prevTask);
				currentIndex++;
				tasksAdded++;
				if (tasksAdded == NUM_SAVED_HISTORY_ITEMS_TO_LOAD) {
					break;
				}
			}
		}
		persistentHistoryLoaded = true;
	}

	/**
	 * Returns the task corresponding to the interaction event history item at
	 * the specified position
	 */
	protected AbstractTask getHistoryTaskAt(int pos) {
		InteractionEvent event = ContextCorePlugin.getContextManager().getActivityMetaContext()
				.getInteractionHistory().get(pos);
		return TasksUiPlugin.getTaskListManager().getTaskList().getTask(event.getStructureHandle());
	}

	public void addTask(AbstractTask task) {
		try {
			if (!persistentHistoryLoaded) {
				loadPersistentHistory();
				persistentHistoryLoaded = true;
			}

			history.remove(task);
			history.add(task);
			currentIndex = history.size() - 1;
		} catch (RuntimeException e) {
			StatusHandler.fail(e, "could not add task to history", false);
		}
	}

	public AbstractTask getPreviousTask() {
		try {
			boolean active = false;
			for (AbstractTask task: history) {
				if(task.isActive()) {
					active = true;
					break;
				}
			}
			
			if (hasPrevious()) {
				if ((currentIndex == 0 && !history.get(currentIndex).isActive()) || !active) {
					return history.get(currentIndex);
				} else {
					return history.get(--currentIndex);
				}
			} else {
				return null;
			}
		} catch (RuntimeException e) {
			StatusHandler.fail(e, "could not get previous task from history", false);
			return null;
		}
	}

	public List<AbstractTask> getPreviousTasks() {
		return history;
	}

	public boolean hasPrevious() {
		try {
			if (!persistentHistoryLoaded) {
				loadPersistentHistory();
				persistentHistoryLoaded = true;
			}

			return (currentIndex == 0 && !history.get(currentIndex).isActive()) || currentIndex > 0;
		} catch (RuntimeException e) {
			StatusHandler.fail(e, "could determine previous task", false);
			return false;
		}
	}

	public void clear() {
		try {
			history.clear();
			currentIndex = -1;
		} catch (RuntimeException e) {
			StatusHandler.fail(e, "could not clear history", false);
		}
	}
	
}

Back to the top