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;
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;
private ListenerList<EnableListener> enableListeners = new ListenerList<EnableListener>();
+ private int[] sortedRows;
+
+ private static final Comparator<? super String> 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);
}
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 "<empty>";
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);
}
}
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();
}
}
}
+ enabled = new boolean[records.size()];
+ sortedRows = IntStream.range(0, records.size()).toArray();
+
setFilter(null);
}
}
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<Integer> 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
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
@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
}
@Override
+ @SuppressWarnings("rawtypes")
public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
return Collections.singletonList(AlphanumComparator.COMPARATOR);
}
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;
}