Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a17aa0399fdb469eba218c39b695d043687c9116 (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
/*******************************************************************************
 * Copyright (C) 2016, Max Hohenegger <eclipse@hohenegger.eu>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.gitflow.ui.internal.dialogs;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TreeColumn;

/**
 * Comparator for {@link FilteredBranchesWidget}.
 *
 */
public class BranchComparator extends ViewerComparator {
	private TreeColumn currentColumn;

	private static final int DESCENDING = SWT.DOWN;

	private static final int ASCENDING = SWT.UP;

	private int direction = DESCENDING;

	private ColumnLabelProvider labelProvider;

	/**
	 * Direction indicator to be supplied to
	 * {@link org.eclipse.swt.widgets.Tree#setSortDirection(int)}
	 *
	 * @return one of <code>UP</code>, <code>DOWN</code> or <code>NONE</code>.
	 */
	public int getDirection() {
		return direction;
	}

	/**
	 * Set the column to sort by, flipping sort direction, if the same column
	 * was set before.
	 *
	 * @param column
	 *            to sort by
	 * @param labelProvider
	 *            to convert cells from selected column into text
	 */
	public void setColumn(TreeColumn column, ColumnLabelProvider labelProvider) {
		this.labelProvider = labelProvider;
		if (column.equals(currentColumn)) {
			flipSortDirection();
		} else {
			currentColumn = column;
			direction = DESCENDING;
		}
	}

	private void flipSortDirection() {
		direction = (direction == DESCENDING) ? ASCENDING : DESCENDING;
	}

	@Override
	public int compare(Viewer viewer, Object e1, Object e2) {
		int rc = 0;

		String firstCell = labelProvider.getText(e1).toLowerCase();
		String secondCell = labelProvider.getText(e2).toLowerCase();
		if (direction == DESCENDING) {
			rc = secondCell.compareTo(firstCell);
		} else {
			rc = firstCell.compareTo(secondCell);
		}

		return rc;
	}
}

Back to the top