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 { /** * 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) { return columnIndex == currentSortColumn && !currentSortDirection.equals(SortDirectionEnum.NONE); } @Override public SortDirectionEnum getSortDirection(int columnIndex) { return columnIndex == currentSortColumn ? currentSortDirection : SortDirectionEnum.NONE; } @Override public int getSortOrder(int columnIndex) { return 0; } @Override @SuppressWarnings("rawtypes") 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(); } currentSortColumn = columnIndex; currentSortDirection = sortDirection; bodyDataProvider.sortBy(columnIndex, sortDirection); } @Override public void clear() { 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; } }