Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 63c91f990b8d4b539219ae30e88d0214bf0b83ab (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
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize;

import java.util.Date;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.team.internal.core.subscribers.ActiveChangeSet;
import org.eclipse.team.internal.core.subscribers.ChangeSet;
import org.eclipse.team.internal.core.subscribers.CheckedInChangeSet;
import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;

/**
 * Sorter for the change log model provider.
 *
 * @since 3.0
 */
public class ChangeSetModelSorter extends ViewerSorter {

	private int commentCriteria;
	private ChangeSetModelProvider provider;

	// Comment sorting options
	public final static int DATE = 1;
	public final static int COMMENT = 2;
	public final static int USER = 3;

	public ChangeSetModelSorter(ChangeSetModelProvider provider, int commentCriteria) {
		this.provider = provider;
		this.commentCriteria = commentCriteria;
	}

	protected int classComparison(Object element) {
		if (element instanceof ChangeSetDiffNode) {
		    ChangeSet set = ((ChangeSetDiffNode)element).getSet();
		    if (set instanceof ActiveChangeSet) {
		        return 0;
		    }
		    return 1;
		}
		return 2;
	}

	protected int compareClass(Object element1, Object element2) {
		return classComparison(element1) - classComparison(element2);
	}

	protected int compareNames(String s1, String s2) {
		return getComparator().compare(s1, s2);
	}

	private int compareDates(Date d1, Date d2) {
		if (d1 == null)
			d1 = new Date(0);
		if (d2 == null)
			d2 = new Date(0);
		return d1.compareTo(d2);
	}

	@Override
	public int compare(Viewer viewer, Object o1, Object o2) {
		//have to deal with non-resources in navigator
		//if one or both objects are not resources, returned a comparison
		//based on class.
		if (o1 instanceof  ChangeSetDiffNode && o2 instanceof ChangeSetDiffNode) {
		    ChangeSet s1 = ((ChangeSetDiffNode) o1).getSet();
		    ChangeSet s2 = ((ChangeSetDiffNode) o2).getSet();
		    if (s1 instanceof ActiveChangeSet && s2 instanceof ActiveChangeSet) {
		        return compareNames(((ActiveChangeSet)s1).getTitle(), ((ActiveChangeSet)s2).getTitle());
		    }
		    if (s1 instanceof CheckedInChangeSet && s2 instanceof CheckedInChangeSet) {
		        CheckedInChangeSet r1 = (CheckedInChangeSet)s1;
		        CheckedInChangeSet r2 = (CheckedInChangeSet)s2;
				if (commentCriteria == DATE)
					return compareDates(r1.getDate(), r2.getDate());
				else if (commentCriteria == COMMENT)
					return compareNames(r1.getComment(), r2.getComment());
				else if (commentCriteria == USER)
					return compareNames(r1.getAuthor(), r2.getAuthor());
				else
					return 0;
		    }
		    if (s1 instanceof ActiveChangeSet) {
		        return -1;
		    } else if (s2 instanceof ActiveChangeSet) {
		        return 1;
		    }
		    if (s1 instanceof CheckedInChangeSet) {
		        return -1;
		    } else if (s2 instanceof CheckedInChangeSet) {
		        return 1;
		    }
		}

		if (o1 instanceof ISynchronizeModelElement && o2 instanceof ISynchronizeModelElement) {
			ViewerSorter embeddedSorter = provider.getEmbeddedSorter();
			if (embeddedSorter != null) {
			    return embeddedSorter.compare(viewer, o1, o2);
			} else {
			    return compareNames(((ISynchronizeModelElement)o1).getName(), ((ISynchronizeModelElement)o2).getName());
			}
		} else if (o1 instanceof ISynchronizeModelElement)
			return 1;
		else if (o2 instanceof ISynchronizeModelElement)
			return -1;

		return 0;
	}

	public int getCommentCriteria() {
		return commentCriteria;
	}
}

Back to the top