Skip to main content
summaryrefslogtreecommitdiffstats
blob: 55bd2fa2a766bc437ee1d8512c7bab103bd16d4f (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
148
149
150
/*******************************************************************************
 * Copyright (c) 2004 - 2005 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
 *******************************************************************************/
/*
 * Created on Dec 22, 2004
  */
package org.eclipse.mylar.tasks;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.mylar.tasks.BugzillaTask.BugTaskState;

 
/**
 * @author Mik Kersten
 */
public class TaskList implements Serializable {

    private static final long serialVersionUID = 3618984485791021105L;

    private List<ITask> rootTasks = new ArrayList<ITask>();
    private transient List<ITask> activeTasks = new ArrayList<ITask>();
    
    public void addRootTask(ITask task) {
        rootTasks.add(task); 
    }
    
    public void setActive(ITask task, boolean active) {
        task.setActive(active);
        if (active) {
            activeTasks.add(task);
        } else {
            activeTasks.remove(task);
        }
    }
    
    /**
     * TODO: make data structure handle this traversal
     */
    public ITask getTaskForId(String id) {
        return setActiveHelper(rootTasks, id);
    } 
    
    private ITask setActiveHelper(List<ITask> tasks, String id) {
        for (ITask task : tasks) {
            if (task.getHandle() == id) {
                return task;
            } else {
                ITask child = setActiveHelper(task.getChildren(), id);
                if (child != null) return child;
            }
        }
        return null;
    }
    
    public List<ITask> getActiveTasks() {
        return activeTasks;
    }
 
    public List<ITask> getTaskFor(Category category) {
        List<ITask> categoryTasks = new ArrayList<ITask>();
        for (ITask task : rootTasks) {
            if (task.getCategories().contains(category)) categoryTasks.add(task);
        } 
        return categoryTasks;
    }
    
    public List<ITask> getRootTasks() {
        return rootTasks;
    }
    
    public Set<Category> getCategories() {
        Set<Category> categories = new HashSet<Category>();
        for (ITask task : rootTasks) {
            categories.addAll(task.getCategories());
        }  
        return categories;        
    }
    
    public void refreshRestoredTasks() {
    	activeTasks = new ArrayList<ITask>();
    	activateRestoredTasks(rootTasks);
    	restoreParents(rootTasks, null);
    	refreshBugReports(rootTasks);
    }
    private void activateRestoredTasks(List<ITask> tasks) {
    	for (ITask task : tasks) {
    		if (task.isActive()) {
    			setActive(task, true);
    		}
    		activateRestoredTasks(task.getChildren());
    	}
    }
    private void restoreParents(List<ITask> tasks, ITask parent) {
    	for (ITask task : tasks) {
    		task.setParent(parent);    	
    		restoreParents(task.getChildren(), task);
    	}
    }
    private void refreshBugReports(List<ITask> tasks) {
    	for (ITask task : tasks) {
    		if (task instanceof BugzillaTask) {
    				((BugzillaTask)task).readBugReport();
    				((BugzillaTask)task).setState(BugTaskState.FREE);
    		}
    		refreshBugReports(task.getChildren());
    	}
    }
    public List<ITask> getTasksInProgress() {
		List<ITask> inprogress = new ArrayList<ITask>();
		for (ITask task : rootTasks) {
            if (!task.isCompleted()) {
            	inprogress.add(task);
            }
        }
		return inprogress;
	}
    public List<ITask> getCompletedTasks() {
    	List <ITask> complete = new ArrayList<ITask>();
    	for (ITask task : rootTasks) {
    		if (task.isCompleted()) {
    			complete.add(task);
    		} else if (task.hasCompletedSubTasks(false)) {
    			complete.add(task);
    		}
    	}
    	return complete;
    }
    public int findLargestTaskHandle() {
    	int max = 0;
    	for (ITask t : rootTasks) {
    		int maxSub = t.findLargestTaskHandle();
    		if (maxSub > max) {
    			max = maxSub; 
    		}
    	}
    	return max;
    }
}

Back to the top