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 {
17 * As this implementation only supports single column sorting,
18 * this property contains the the column index of the column that
19 * is currently used for sorting.
20 * Initial value = -1 for no sort column
22 protected int currentSortColumn = -1;
25 * As this implementation only supports single column sorting,
26 * this property contains the current sort direction of the column that
27 * is currently used for sorting.
29 protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;
31 private TechTypeTableDataProvider bodyDataProvider;
33 public TechTypeTableSortModel(TechTypeTableDataProvider bodyDataProvider) {
34 this.bodyDataProvider = bodyDataProvider;
39 public List<Integer> getSortedColumnIndexes() {
40 List<Integer> indexes = new ArrayList<Integer>();
41 if (currentSortColumn > -1) {
42 indexes.add(Integer.valueOf(currentSortColumn));
48 public boolean isColumnIndexSorted(int columnIndex) {
49 return columnIndex == currentSortColumn && !currentSortDirection.equals(SortDirectionEnum.NONE);
53 public SortDirectionEnum getSortDirection(int columnIndex) {
54 return columnIndex == currentSortColumn ? currentSortDirection : SortDirectionEnum.NONE;
58 public int getSortOrder(int columnIndex) {
63 @SuppressWarnings("rawtypes")
64 public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
65 return Collections.singletonList(AlphanumComparator.COMPARATOR);
69 public Comparator<?> getColumnComparator(int columnIndex) {
70 return AlphanumComparator.COMPARATOR;
74 public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {
76 if (!isColumnIndexSorted(columnIndex)) {
80 currentSortColumn = columnIndex;
81 currentSortDirection = sortDirection;
83 bodyDataProvider.sortBy(columnIndex, sortDirection);
88 this.currentSortColumn = -1;
89 this.currentSortDirection = SortDirectionEnum.NONE;
92 @SuppressWarnings("unchecked")
93 public static <T> T[] ensureArraySize(T[] array, int l, Class<T> clazz, T defaultValue) {
95 if (array == null || array.length != l) {
96 array = (T[]) Array.newInstance(clazz, l);
97 fill = defaultValue != null;
100 Arrays.fill(array, defaultValue);