Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 92f4a33abcdbe904c8ac7f8f72cada0aed51ccde (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
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package org.eclipse.papyrus.infra.nattable.glazedlists.copy;

import java.util.Iterator;
import java.util.List;

/**
 * @see ca.odell.glazedlists.gui.AbstractTableComparatorChooser#SINGLE_COLUMN
 * @see ca.odell.glazedlists.gui.AbstractTableComparatorChooser#MULTIPLE_COLUMN_MOUSE
 *
 * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
 */
public final class MouseOnlySortingStrategy implements SortingStrategy {

	/** if false, other sorting columns will be cleared before a click takes effect */
	private final boolean multipleColumnSort;

	/**
	 * Create a new {@link8 ca.odell.glazedlists.impl.gui.MouseOnlySortingStrategy}, sorting multiple
	 * columns or not as specified.
	 */
	public MouseOnlySortingStrategy(boolean multipleColumnSort) {
		this.multipleColumnSort = multipleColumnSort;
	}

	/**
	 * Adjust the sorting state based on receiving the specified clicks.
	 */
	public void columnClicked(SortingState sortingState, int column, int clicks, boolean shift, boolean control) {
		SortingState.SortingColumn clickedColumn = sortingState.getColumns().get(column);
		if (clickedColumn.getComparators().isEmpty())
			return;

		List<SortingState.SortingColumn> recentlyClickedColumns = sortingState.getRecentlyClickedColumns();

		// on a double click, clear all click counts
		if (clicks == 2) {
			for (Iterator<SortingState.SortingColumn> i = recentlyClickedColumns.iterator(); i.hasNext();) {
				SortingState.SortingColumn sortingColumn = i.next();
				sortingColumn.clear();
			}
			recentlyClickedColumns.clear();

			// if we're only sorting one column at a time, clear other columns
		} else if (!multipleColumnSort) {
			for (Iterator<SortingState.SortingColumn> i = recentlyClickedColumns.iterator(); i.hasNext();) {
				SortingState.SortingColumn sortingColumn = i.next();
				if (sortingColumn != clickedColumn) {
					sortingColumn.clear();
				}
			}
			recentlyClickedColumns.clear();
		}

		// add a click to the newly clicked column if it has any comparators
		int netClicks = 1 + clickedColumn.getComparatorIndex() * 2 + (clickedColumn.isReverse() ? 1 : 0);
		clickedColumn.setComparatorIndex((netClicks / 2) % clickedColumn.getComparators().size());
		clickedColumn.setReverse(netClicks % 2 == 1);
		if (!recentlyClickedColumns.contains(clickedColumn)) {
			recentlyClickedColumns.add(clickedColumn);
		}

		// rebuild the sorting state
		sortingState.fireSortingChanged();
	}
}

Back to the top