Skip to main content
summaryrefslogtreecommitdiffstats
blob: 461e50b38bb19a1fad3f16045e95817adbc3a7a9 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (c) 2004, 2009 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.views;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.AbstractTaskContainer;
import org.eclipse.mylyn.internal.tasks.core.ScheduledTaskContainer;
import org.eclipse.mylyn.internal.tasks.core.UncategorizedTaskContainer;
import org.eclipse.mylyn.internal.tasks.core.UnmatchedTaskContainer;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TaskComparator;
import org.eclipse.mylyn.tasks.core.IRepositoryElement;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskContainer;

/**
 * @author Mik Kersten
 */
public class TaskListInterestSorter extends ViewerSorter {

	private final TaskKeyComparator taskKeyComparator = new TaskKeyComparator();

	@Override
	public int compare(Viewer compareViewer, Object o1, Object o2) {

		if (o1 instanceof ITaskContainer && o2 instanceof UnmatchedTaskContainer) {
			return -1;
		} else if (o2 instanceof ITaskContainer && o1 instanceof UnmatchedTaskContainer) {
			return 1;
		}
		if (o1 instanceof ScheduledTaskContainer && o2 instanceof ScheduledTaskContainer) {
			ScheduledTaskContainer dateRangeTaskContainer1 = (ScheduledTaskContainer) o1;
			ScheduledTaskContainer dateRangeTaskContainer2 = (ScheduledTaskContainer) o2;
			return dateRangeTaskContainer1.getDateRange().compareTo(dateRangeTaskContainer2.getDateRange());
		} else if (o1 instanceof ITaskContainer && o2 instanceof ScheduledTaskContainer) {
			return -1;
		} else if (o1 instanceof ScheduledTaskContainer && o2 instanceof ITaskContainer) {
			return 1;
		}

		if (o1 instanceof UncategorizedTaskContainer && o2 instanceof ITaskContainer) {
			return -1;
		} else if (o1 instanceof ITaskContainer && o2 instanceof UncategorizedTaskContainer) {
			return 1;
		}

		if (!(o1 instanceof ITask) && o2 instanceof ITask) {
			return 1;
		}

		if (!(o1 instanceof ITask)) {//o1 instanceof AbstractTaskContainer || o1 instanceof AbstractRepositoryQuery) {
			if (!(o2 instanceof ITask)) {//o2 instanceof AbstractTaskContainer || o2 instanceof AbstractRepositoryQuery) {
				return ((IRepositoryElement) o1).getSummary().compareToIgnoreCase(
						((IRepositoryElement) o2).getSummary());
			} else {
				return -1;
			}
		} else if (o1 instanceof ITaskContainer) {
			if (!(o2 instanceof ITask)) {//o2 instanceof AbstractTaskContainer || o2 instanceof AbstractRepositoryQuery) {
				return -1;
			} else if (o2 instanceof ITaskContainer) {
				IRepositoryElement element1 = (IRepositoryElement) o1;
				IRepositoryElement element2 = (IRepositoryElement) o2;

				AbstractTask task1 = null;
				AbstractTask task2 = null;
				if (element1 instanceof ITask) {
					task1 = (AbstractTask) element1;
				}
				if (element2 instanceof ITask) {
					task2 = (AbstractTask) element2;
				}

				if (task1 == null && task2 == null) {
					return comparePrioritiesAndKeys(element1, element2);
				} else if (task1 == null) {
					return 1;
				} else if (task2 == null) {
					return -1;
				}

				int complete = compareCompleted(task1, task2);
				if (complete != 0) {
					return complete;
				} else {
					int due = compareDueDates(task1, task2);
					if (due != 0) {
						return due;
					} else {
						int today = compareScheduledDate(task1, task2);
						if (today == 0) {
							return comparePrioritiesAndKeys(element1, element2);
						} else {
							return today;
						}
					}
				}
			}
		}
		return 0;
	}

	private int compareDueDates(ITask task1, ITask task2) {
		if (TasksUiPlugin.getTaskActivityManager().isOverdue(task1)
				&& !TasksUiPlugin.getTaskActivityManager().isOverdue(task2)) {
			return -1;
		} else if (!TasksUiPlugin.getTaskActivityManager().isOverdue(task1)
				&& TasksUiPlugin.getTaskActivityManager().isOverdue(task2)) {
			return 1;
		}
		return 0;
	}

	private int compareScheduledDate(AbstractTask task1, AbstractTask task2) {
		if (isToday(task1) && !isToday(task2)) {
			return -1;
		} else if (!isToday(task1) && isToday(task2)) {
			return 1;
		} else {
			return 0;
		}
	}

	private boolean isToday(AbstractTask task) {
		return TasksUiPlugin.getTaskActivityManager().isPastReminder(task)
				|| TasksUiPlugin.getTaskActivityManager().isScheduledForToday(task);
	}

	private int compareCompleted(ITask task1, ITask task2) {
		if (task1.isCompleted() && !task2.isCompleted()) {
			return 1;
		} else if (!task1.isCompleted() && task2.isCompleted()) {
			return -1;
		} else {
			return 0;
		}
	}

	private int comparePrioritiesAndKeys(IRepositoryElement element1, IRepositoryElement element2) {
		int priority = comparePriorities(element1, element2);
		if (priority != 0) {
			return priority;
		}

		int description = compareKeys(element1, element2);
		if (description != 0) {
			return description;
		}
		return 0;
	}

	private int compareKeys(IRepositoryElement element1, IRepositoryElement element2) {
		return taskKeyComparator.compare(TaskComparator.getSortableFromElement(element1),
				TaskComparator.getSortableFromElement(element2));
	}

	private int comparePriorities(IRepositoryElement element1, IRepositoryElement element2) {
		if (element1 instanceof AbstractTaskContainer && element2 instanceof AbstractTaskContainer) {
			return ((AbstractTaskContainer) element1).getPriority().compareTo(
					((AbstractTaskContainer) element2).getPriority());
		} else {
			// TODO: consider implementing
			return -1;
		}
	}

}

Back to the top