]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Sorting support for tech type table columns 55/4555/2
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 12 Oct 2020 11:27:29 +0000 (14:27 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 14 Oct 2020 09:57:41 +0000 (09:57 +0000)
gitlab #104

Change-Id: Ibf4a63314fa2499ad8d6196f3373e1f9837f0efc
(cherry picked from commit 708dfb62d934f425087f983fecfea0f00c59840d)

org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableSortModel.java
org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java

index c23a6079952a82eb6927c1cdc28ef5de1a7853f0..af0e28d07bc84be4b9b01ab221105b2f3c5269a9 100644 (file)
@@ -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<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);
@@ -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 "<empty>";
@@ -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<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
index f6a02f4a6c8990e5ac3eed2673f548905b73546e..95c9008e48336d2b8320acbc3c674279be07e529 100644 (file)
@@ -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<Comparator> 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;
     }
index c202325ee506829e04c29e51addef4bd50f0472c..9f7fe93b86c019f0087e99e929f831f3f36e6e33 100644 (file)
@@ -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;
 
 /**