X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ftechtype%2Ftable%2FTechTypeTableSortModel.java;fp=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ftechtype%2Ftable%2FTechTypeTableSortModel.java;h=f6a02f4a6c8990e5ac3eed2673f548905b73546e;hb=97fca8781af176c8e0dfdb624f634e4a799704fe;hp=0000000000000000000000000000000000000000;hpb=80aef302ef8e28f72e861c93ac71421f289af8c0;p=simantics%2Fdistrict.git 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 index 00000000..f6a02f4a --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableSortModel.java @@ -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 getSortedColumnIndexes() { + List indexes = new ArrayList(); + 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 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[] ensureArraySize(T[] array, int l, Class 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; + } + +}