Skip to main content
summaryrefslogtreecommitdiffstats
blob: e55e0da7f6862e3ec3b22a9ca2d117b98af24f50 (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
/*******************************************************************************
 * 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
 *******************************************************************************/

package org.eclipse.mylar.tasklist.report.ui;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.mylar.tasklist.ITask;

/**
 * @author Ken Sueda
 */
public class PlanningGameSorter extends ViewerSorter {
	/**
	 * Constructor argument values that indicate to sort items by 
	 * different columns.
	 */
	public final static int DESCRIPTION = 1;
	public final static int PRIORITY = 2;
	public final static int CREATION_DATE = 3;
	public final static int COMPLETED_DATE = 4;
	public final static int DURATION = 5;

	// Criteria that the instance uses 
	private int criteria;

	/**
	 * Creates a resource sorter that will use the given sort criteria.
	 *
	 * @param criteria the sort criterion to use: one of <code>NAME</code> or 
	 *   <code>TYPE</code>
	 */
	public PlanningGameSorter(int criteria) {
		super();
		this.criteria = criteria;
	}

	@Override
	public int compare(Viewer viewer, Object obj1, Object obj2) {
		ITask t1 = (ITask) obj1;
		ITask t2 = (ITask) obj2;

		switch (criteria) {
			case DESCRIPTION:
				return compareDescription(t1, t2);
			case PRIORITY:
				return comparePriority(t1, t2);
			case CREATION_DATE:
				return compareCreationDate(t1, t2);
			case COMPLETED_DATE:
				return compareCompletedDate(t1, t2);
			case DURATION:
				return compareDuration(t1, t2);
			default:
				return 0;
		}
	}
	
	private int compareDescription(ITask task1, ITask task2) {
		return task1.getDescription(false).compareTo(task2.getDescription(false));
	}
	
	private int comparePriority(ITask task1, ITask task2) {
		return task1.getPriority().compareTo(task2.getPriority());
	}
	
	private int compareCompletedDate(ITask task1, ITask task2) {
		return task2.getEndDate().compareTo(task1.getEndDate());
	}

	private int compareCreationDate(ITask task1, ITask task2) {
		if(task1.getCreationDate() == null)
			return 1;
		else if(task2.getCreationDate() == null)
			return -1;
		else
			return task2.getCreationDate().compareTo(task1.getCreationDate());
	}
	
	private int compareDuration(ITask task1, ITask task2) {
		return task1.getElapsedTimeLong() < task2.getElapsedTimeLong() ? 1 : -1;
	}
}

Back to the top