]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableSortModel.java
First testing version of TechTypeTable
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / table / TechTypeTableSortModel.java
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableSortModel.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableSortModel.java
new file mode 100644 (file)
index 0000000..f6a02f4
--- /dev/null
@@ -0,0 +1,127 @@
+package org.simantics.district.network.ui.techtype.table;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.eclipse.nebula.widgets.nattable.sort.ISortModel;
+import org.eclipse.nebula.widgets.nattable.sort.SortDirectionEnum;
+import org.simantics.utils.strings.AlphanumComparator;
+
+public class TechTypeTableSortModel implements ISortModel  {
+
+    private static final SortDirectionEnum[] NO_DIRECTIONS = {};
+    private static final boolean[] NO_BOOLEANS = {};
+
+    /**
+     * Array that contains the sort direction for every column.
+     * Needed to access the current sort state of a column.
+     */
+    protected SortDirectionEnum[] sortDirections = NO_DIRECTIONS;
+
+    /**
+     * Array that contains the sorted flags for every column.
+     * Needed to access the current sort state of a column.
+     */
+    protected boolean[] sorted = NO_BOOLEANS;
+
+    /**
+     * As this implementation only supports single column sorting,
+     * this property contains the the column index of the column that
+     * is currently used for sorting.
+     * Initial value = -1 for no sort column
+     */
+    protected int currentSortColumn = -1;
+
+    /**
+     * As this implementation only supports single column sorting,
+     * this property contains the current sort direction of the column that
+     * is currently used for sorting.
+     */
+    protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;
+    
+    private TechTypeTableDataProvider bodyDataProvider;
+
+    public TechTypeTableSortModel(TechTypeTableDataProvider bodyDataProvider) {
+        this.bodyDataProvider = bodyDataProvider;
+    }
+    
+    
+    @Override
+    public List<Integer> getSortedColumnIndexes() {
+        List<Integer> indexes = new ArrayList<Integer>();
+        if (currentSortColumn > -1) {
+            indexes.add(Integer.valueOf(currentSortColumn));
+        }
+        return indexes;
+    }
+
+    @Override
+    public boolean isColumnIndexSorted(int columnIndex) {
+        if (sorted.length <= columnIndex)
+            return false;
+        return sorted[columnIndex];
+    }
+
+    @Override
+    public SortDirectionEnum getSortDirection(int columnIndex) {
+        if (sortDirections.length <= columnIndex)
+            return SortDirectionEnum.NONE;
+        return sortDirections[columnIndex];
+    }
+
+    @Override
+    public int getSortOrder(int columnIndex) {
+        return 0;
+    }
+
+    @Override
+    public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
+        return Collections.singletonList(AlphanumComparator.COMPARATOR);
+    }
+
+    @Override
+    public Comparator<?> getColumnComparator(int columnIndex) {
+        return AlphanumComparator.COMPARATOR;
+    }
+
+    @Override
+    public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {
+        
+        if (!isColumnIndexSorted(columnIndex)) {
+            clear();
+        }
+        int columnCount = bodyDataProvider.getColumnCount();
+        sortDirections = ensureArraySize(sortDirections, columnCount, SortDirectionEnum.class, SortDirectionEnum.NONE);
+        
+        
+        sortDirections[columnIndex] = sortDirection;
+        sorted[columnIndex] = !sortDirection.equals(SortDirectionEnum.NONE);
+        currentSortColumn = columnIndex;
+        currentSortDirection = sortDirection;
+    }
+
+    @Override
+    public void clear() {
+        Arrays.fill(this.sortDirections, SortDirectionEnum.NONE);
+        Arrays.fill(this.sorted, false);
+        this.currentSortColumn = -1;
+        this.currentSortDirection = SortDirectionEnum.NONE;
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <T> T[] ensureArraySize(T[] array, int l, Class<T> clazz, T defaultValue) {
+        boolean fill = true;
+        if (array == null || array.length != l) {
+            array = (T[]) Array.newInstance(clazz, l);
+            fill = defaultValue != null;
+        }
+        if (fill)
+            Arrays.fill(array, defaultValue);
+        return array;
+    }
+
+}