1 package org.simantics.district.network.ui.techtype.table;
3 import java.lang.reflect.Array;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.Comparator;
10 import org.eclipse.nebula.widgets.nattable.sort.ISortModel;
11 import org.eclipse.nebula.widgets.nattable.sort.SortDirectionEnum;
12 import org.simantics.utils.strings.AlphanumComparator;
14 public class TechTypeTableSortModel implements ISortModel {
16 private static final SortDirectionEnum[] NO_DIRECTIONS = {};
17 private static final boolean[] NO_BOOLEANS = {};
20 * Array that contains the sort direction for every column.
21 * Needed to access the current sort state of a column.
23 protected SortDirectionEnum[] sortDirections = NO_DIRECTIONS;
26 * Array that contains the sorted flags for every column.
27 * Needed to access the current sort state of a column.
29 protected boolean[] sorted = NO_BOOLEANS;
32 * As this implementation only supports single column sorting,
33 * this property contains the the column index of the column that
34 * is currently used for sorting.
35 * Initial value = -1 for no sort column
37 protected int currentSortColumn = -1;
40 * As this implementation only supports single column sorting,
41 * this property contains the current sort direction of the column that
42 * is currently used for sorting.
44 protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;
46 private TechTypeTableDataProvider bodyDataProvider;
48 public TechTypeTableSortModel(TechTypeTableDataProvider bodyDataProvider) {
49 this.bodyDataProvider = bodyDataProvider;
54 public List<Integer> getSortedColumnIndexes() {
55 List<Integer> indexes = new ArrayList<Integer>();
56 if (currentSortColumn > -1) {
57 indexes.add(Integer.valueOf(currentSortColumn));
63 public boolean isColumnIndexSorted(int columnIndex) {
64 if (sorted.length <= columnIndex)
66 return sorted[columnIndex];
70 public SortDirectionEnum getSortDirection(int columnIndex) {
71 if (sortDirections.length <= columnIndex)
72 return SortDirectionEnum.NONE;
73 return sortDirections[columnIndex];
77 public int getSortOrder(int columnIndex) {
82 public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
83 return Collections.singletonList(AlphanumComparator.COMPARATOR);
87 public Comparator<?> getColumnComparator(int columnIndex) {
88 return AlphanumComparator.COMPARATOR;
92 public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {
94 if (!isColumnIndexSorted(columnIndex)) {
97 int columnCount = bodyDataProvider.getColumnCount();
98 sortDirections = ensureArraySize(sortDirections, columnCount, SortDirectionEnum.class, SortDirectionEnum.NONE);
101 sortDirections[columnIndex] = sortDirection;
102 sorted[columnIndex] = !sortDirection.equals(SortDirectionEnum.NONE);
103 currentSortColumn = columnIndex;
104 currentSortDirection = sortDirection;
108 public void clear() {
109 Arrays.fill(this.sortDirections, SortDirectionEnum.NONE);
110 Arrays.fill(this.sorted, false);
111 this.currentSortColumn = -1;
112 this.currentSortDirection = SortDirectionEnum.NONE;
115 @SuppressWarnings("unchecked")
116 public static <T> T[] ensureArraySize(T[] array, int l, Class<T> clazz, T defaultValue) {
118 if (array == null || array.length != l) {
119 array = (T[]) Array.newInstance(clazz, l);
120 fill = defaultValue != null;
123 Arrays.fill(array, defaultValue);