Skip to main content
summaryrefslogtreecommitdiffstats
blob: ea9249a95f4b2292ab616ba8d62b9b0ac2f75dab (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
/*******************************************************************************
 * Copyright (c) 2012 Ericsson
 * 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:
 *     Miles Parker (Tasktop Technologies) - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.reviews.ui.providers;

import java.util.Date;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.mylyn.reviews.core.model.IDated;
import org.eclipse.mylyn.reviews.core.model.IIndexed;
import org.eclipse.mylyn.reviews.core.model.IReviewItem;

/**
 * Default ordering for review items. Order is in general:
 * <ol>
 * <li>Global Comments</i>
 * <li>Patch Sets, ReviewItemSets, other containers</li>
 * <li>Dated Items by Change Date (Includes e.g. patch sets, artifacts, etc..)</li>
 * <li>Orderable Items (Locations, Comments without dates)</li>
 * <li>Other Review Items (Files, by full path name)</li>
 * </ol>
 * 
 * @author Miles Parker
 */
public class ReviewsSorter extends ViewerSorter {

	@Override
	public int compare(Viewer viewer, Object e1, Object e2) {
		if (e1 instanceof GlobalCommentsNode) {
			return -1;
		}
		if (e2 instanceof GlobalCommentsNode) {
			return 1;
		}
		Date d1 = null;
		if (e1 instanceof IDated) {
			IDated ed1 = (IDated) e1;
			d1 = ed1.getModificationDate();
			if (d1 == null) {
				d1 = ed1.getCreationDate();
			}
		}
		Date d2 = null;
		if (e2 instanceof IDated) {
			IDated ed2 = (IDated) e2;
			d2 = ed2.getModificationDate();
			if (d2 == null) {
				d2 = ed2.getCreationDate();
			}
		}

		if (d1 != null && d2 != null) {
			return d1.compareTo(d2);
		}

		if (e1 instanceof IIndexed && e2 instanceof IIndexed) {
			return IIndexed.COMPARATOR.compare((IIndexed) e1, (IIndexed) e2);
		}

		if (d1 != null) {
			return -1;
		}
		if (d2 != null) {
			return 1;
		}
		if (e1 instanceof IIndexed) {
			return -1;
		}
		if (e2 instanceof IIndexed) {
			return 1;
		}

		//We want to use full path, not the shortened name in UI.
		if (e1 instanceof IReviewItem && e2 instanceof IReviewItem) {
			return super.compare(viewer, ((IReviewItem) e1).getName(), ((IReviewItem) e2).getName());
		}
		return super.compare(viewer, e1, e2);
	}

	@Override
	public int category(Object element) {
		if (element instanceof IIndexed) {
			return 1;
		}
		if (element instanceof IDated) {
			return 2;
		}
		return super.category(element);
	}
}

Back to the top