]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableSortModel.java
Sorting support for tech type table columns
[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     /**
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
21      */
22     protected int currentSortColumn = -1;
23
24     /**
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.
28      */
29     protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;
30     
31     private TechTypeTableDataProvider bodyDataProvider;
32
33     public TechTypeTableSortModel(TechTypeTableDataProvider bodyDataProvider) {
34         this.bodyDataProvider = bodyDataProvider;
35     }
36     
37     
38     @Override
39     public List<Integer> getSortedColumnIndexes() {
40         List<Integer> indexes = new ArrayList<Integer>();
41         if (currentSortColumn > -1) {
42             indexes.add(Integer.valueOf(currentSortColumn));
43         }
44         return indexes;
45     }
46
47     @Override
48     public boolean isColumnIndexSorted(int columnIndex) {
49         return columnIndex == currentSortColumn && !currentSortDirection.equals(SortDirectionEnum.NONE);
50     }
51
52     @Override
53     public SortDirectionEnum getSortDirection(int columnIndex) {
54         return columnIndex == currentSortColumn ? currentSortDirection : SortDirectionEnum.NONE;
55     }
56
57     @Override
58     public int getSortOrder(int columnIndex) {
59         return 0;
60     }
61
62     @Override
63     @SuppressWarnings("rawtypes")
64     public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
65         return Collections.singletonList(AlphanumComparator.COMPARATOR);
66     }
67
68     @Override
69     public Comparator<?> getColumnComparator(int columnIndex) {
70         return AlphanumComparator.COMPARATOR;
71     }
72
73     @Override
74     public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {
75         
76         if (!isColumnIndexSorted(columnIndex)) {
77             clear();
78         }
79         
80         currentSortColumn = columnIndex;
81         currentSortDirection = sortDirection;
82         
83         bodyDataProvider.sortBy(columnIndex, sortDirection);
84     }
85
86     @Override
87     public void clear() {
88         this.currentSortColumn = -1;
89         this.currentSortDirection = SortDirectionEnum.NONE;
90     }
91
92     @SuppressWarnings("unchecked")
93     public static <T> T[] ensureArraySize(T[] array, int l, Class<T> clazz, T defaultValue) {
94         boolean fill = true;
95         if (array == null || array.length != l) {
96             array = (T[]) Array.newInstance(clazz, l);
97             fill = defaultValue != null;
98         }
99         if (fill)
100             Arrays.fill(array, defaultValue);
101         return array;
102     }
103
104 }