From: Reino Ruusu Date: Mon, 12 Oct 2020 11:27:29 +0000 (+0300) Subject: Sorting support for tech type table columns X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F37%2F4537%2F1;p=simantics%2Fdistrict.git Sorting support for tech type table columns gitlab #104 Change-Id: Ibf4a63314fa2499ad8d6196f3373e1f9837f0efc --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java index c23a6079..af0e28d0 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java @@ -5,6 +5,7 @@ import java.io.StringReader; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.stream.IntStream; @@ -12,7 +13,9 @@ import java.util.stream.IntStream; import org.apache.commons.csv.CSVRecord; import org.eclipse.core.runtime.ListenerList; import org.eclipse.nebula.widgets.nattable.data.IDataProvider; +import org.eclipse.nebula.widgets.nattable.sort.SortDirectionEnum; import org.simantics.district.imports.DistrictImportUtils; +import org.simantics.district.network.techtype.TechTypeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,6 +32,18 @@ public class TechTypeTableDataProvider implements IDataProvider { private ListenerList enableListeners = new ListenerList(); + private int[] sortedRows; + + private static final Comparator VALUE_COMPARATOR = (a, b) -> { + try { + double da = Double.valueOf(a.replace(",", ".")); + double db = Double.valueOf(b.replace(",", ".")); + return Double.compare(da, db); + } catch (NumberFormatException e) { + return TechTypeUtils.compareNatural(a, b); + } + }; + public TechTypeTableDataProvider(String data, int[] enabledList) { setData(data); setEnabledFlags(enabledList); @@ -58,13 +73,17 @@ public class TechTypeTableDataProvider implements IDataProvider { } public CSVRecord getRecord(int rowIndex) { - return records.get(filteredRows[rowIndex]); + return records.get(recordIndex(rowIndex)); } - + public boolean isEnabled(int rowIndex) { - return enabled[filteredRows[rowIndex]]; + return enabled[recordIndex(rowIndex)]; } + private int recordIndex(int rowIndex) { + return sortedRows[filteredRows[rowIndex]]; + } + public String getHeaderValue(int columnIndex) { if (headers == null) { return ""; @@ -87,7 +106,7 @@ public class TechTypeTableDataProvider implements IDataProvider { public void setDataValue(int columnIndex, int rowIndex, Object newValue) { if (columnIndex == 0) { boolean value = Boolean.parseBoolean((String) newValue); - enabled[filteredRows[rowIndex]] = value; + enabled[recordIndex(rowIndex)] = value; fireEnableEvent(rowIndex, value); } } @@ -121,7 +140,7 @@ public class TechTypeTableDataProvider implements IDataProvider { this.filter = text != null ? text.toLowerCase() : null; filteredRows = IntStream.range(0, records.size()) - .filter(k -> isMatch(records.get(k), filter)) + .filter(k -> isMatch(records.get(sortedRows[k]), filter)) .toArray(); } @@ -161,6 +180,9 @@ public class TechTypeTableDataProvider implements IDataProvider { } } + enabled = new boolean[records.size()]; + sortedRows = IntStream.range(0, records.size()).toArray(); + setFilter(null); } @@ -208,8 +230,29 @@ public class TechTypeTableDataProvider implements IDataProvider { } enabled = new boolean[records.size()]; + sortedRows = IntStream.range(0, records.size()).toArray(); setFilter(null); } + public void sortBy(int columnIndex, SortDirectionEnum sortDirection) { + + if (columnIndex >= 0 && !sortDirection.equals(SortDirectionEnum.NONE)) { + Comparator comparator = columnIndex == 0 ? + Comparator.comparing(k -> enabled[sortedRows[(int) k]]) : + Comparator.comparing(k -> records.get(sortedRows[(int) k]).get(columnIndex-1), VALUE_COMPARATOR); + + if (sortDirection.equals(SortDirectionEnum.DESC)) + comparator = comparator.reversed(); + + sortedRows = IntStream.range(0, records.size()) + .mapToObj(i -> i) + .sorted(comparator) + .mapToInt(i -> sortedRows[i]) + .toArray(); + } else { + sortedRows = IntStream.range(0, records.size()).toArray(); + } + } + } \ No newline at end of file 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 index f6a02f4a..95c9008e 100644 --- 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 @@ -13,21 +13,6 @@ 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 @@ -61,16 +46,12 @@ public class TechTypeTableSortModel implements ISortModel { @Override public boolean isColumnIndexSorted(int columnIndex) { - if (sorted.length <= columnIndex) - return false; - return sorted[columnIndex]; + return columnIndex == currentSortColumn && !currentSortDirection.equals(SortDirectionEnum.NONE); } @Override public SortDirectionEnum getSortDirection(int columnIndex) { - if (sortDirections.length <= columnIndex) - return SortDirectionEnum.NONE; - return sortDirections[columnIndex]; + return columnIndex == currentSortColumn ? currentSortDirection : SortDirectionEnum.NONE; } @Override @@ -79,6 +60,7 @@ public class TechTypeTableSortModel implements ISortModel { } @Override + @SuppressWarnings("rawtypes") public List getComparatorsForColumnIndex(int columnIndex) { return Collections.singletonList(AlphanumComparator.COMPARATOR); } @@ -94,20 +76,15 @@ public class TechTypeTableSortModel implements ISortModel { 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; + + bodyDataProvider.sortBy(columnIndex, sortDirection); } @Override public void clear() { - Arrays.fill(this.sortDirections, SortDirectionEnum.NONE); - Arrays.fill(this.sorted, false); this.currentSortColumn = -1; this.currentSortDirection = SortDirectionEnum.NONE; } diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java index c202325e..9f7fe93b 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java +++ b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java @@ -11,10 +11,8 @@ import org.simantics.databoard.binding.impl.ArrayListBinding; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.common.utils.CommonDBUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; -import org.simantics.db.service.UndoRedoSupport; import org.simantics.district.network.ontology.DistrictNetworkResource; /**