]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableSortModel.java
f6a02f4a6c8990e5ac3eed2673f548905b73546e
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / table / TechTypeTableSortModel.java
1 package org.simantics.district.network.ui.techtype.table;
2
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;
8 import java.util.List;
9
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;
13
14 public class TechTypeTableSortModel implements ISortModel  {
15
16     private static final SortDirectionEnum[] NO_DIRECTIONS = {};
17     private static final boolean[] NO_BOOLEANS = {};
18
19     /**
20      * Array that contains the sort direction for every column.
21      * Needed to access the current sort state of a column.
22      */
23     protected SortDirectionEnum[] sortDirections = NO_DIRECTIONS;
24
25     /**
26      * Array that contains the sorted flags for every column.
27      * Needed to access the current sort state of a column.
28      */
29     protected boolean[] sorted = NO_BOOLEANS;
30
31     /**
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
36      */
37     protected int currentSortColumn = -1;
38
39     /**
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.
43      */
44     protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;
45     
46     private TechTypeTableDataProvider bodyDataProvider;
47
48     public TechTypeTableSortModel(TechTypeTableDataProvider bodyDataProvider) {
49         this.bodyDataProvider = bodyDataProvider;
50     }
51     
52     
53     @Override
54     public List<Integer> getSortedColumnIndexes() {
55         List<Integer> indexes = new ArrayList<Integer>();
56         if (currentSortColumn > -1) {
57             indexes.add(Integer.valueOf(currentSortColumn));
58         }
59         return indexes;
60     }
61
62     @Override
63     public boolean isColumnIndexSorted(int columnIndex) {
64         if (sorted.length <= columnIndex)
65             return false;
66         return sorted[columnIndex];
67     }
68
69     @Override
70     public SortDirectionEnum getSortDirection(int columnIndex) {
71         if (sortDirections.length <= columnIndex)
72             return SortDirectionEnum.NONE;
73         return sortDirections[columnIndex];
74     }
75
76     @Override
77     public int getSortOrder(int columnIndex) {
78         return 0;
79     }
80
81     @Override
82     public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
83         return Collections.singletonList(AlphanumComparator.COMPARATOR);
84     }
85
86     @Override
87     public Comparator<?> getColumnComparator(int columnIndex) {
88         return AlphanumComparator.COMPARATOR;
89     }
90
91     @Override
92     public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {
93         
94         if (!isColumnIndexSorted(columnIndex)) {
95             clear();
96         }
97         int columnCount = bodyDataProvider.getColumnCount();
98         sortDirections = ensureArraySize(sortDirections, columnCount, SortDirectionEnum.class, SortDirectionEnum.NONE);
99         
100         
101         sortDirections[columnIndex] = sortDirection;
102         sorted[columnIndex] = !sortDirection.equals(SortDirectionEnum.NONE);
103         currentSortColumn = columnIndex;
104         currentSortDirection = sortDirection;
105     }
106
107     @Override
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;
113     }
114
115     @SuppressWarnings("unchecked")
116     public static <T> T[] ensureArraySize(T[] array, int l, Class<T> clazz, T defaultValue) {
117         boolean fill = true;
118         if (array == null || array.length != l) {
119             array = (T[]) Array.newInstance(clazz, l);
120             fill = defaultValue != null;
121         }
122         if (fill)
123             Arrays.fill(array, defaultValue);
124         return array;
125     }
126
127 }