]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java
Hide "enabled" column for non-component type tech type tables
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / table / TechTypeTableDataProvider.java
index c23a6079952a82eb6927c1cdc28ef5de1a7853f0..4d4244d7caf43d677f7132ba77617be34917a9e5 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,14 +32,29 @@ public class TechTypeTableDataProvider implements IDataProvider {
 
     private ListenerList<EnableListener> enableListeners = new ListenerList<EnableListener>();
 
+    private int[] sortedRows;
+    
+       private boolean showEnabled = true;
+
+       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);
+        showEnabled = enabledList != null;
     }
     
     public TechTypeTableDataProvider(String data) {
-        // load csv
         setData(data);
+        showEnabled = false;
     }
     
     public void setEnabledFlags(int[] enabledList) {
@@ -47,47 +65,63 @@ public class TechTypeTableDataProvider implements IDataProvider {
                     enabled[i] = true;
             }
         }
+        
+        showEnabled = enabledList != null;
+    }
+    
+    public boolean isCheckBoxColumn(int columnIndex) {
+       return isEnabledColumn(columnIndex);
     }
 
     public String getVariableName(int columnIndex) {
-        return variables != null && columnIndex > 0 && columnIndex <= variables.size() ? variables.get(columnIndex - 1) : null;
+        return variables != null && columnIndex > 0 && columnIndex <= variables.size()
+                       ? variables.get(columnIndex - columnOffset())
+                               : null;
     }
 
+       private int columnOffset() {
+               return showEnabled ? 1 : 0;
+       }
+
     public int getVariableIndex(String variableName) {
-        return variables != null ? variables.indexOf(variableName) + 1 : -1;
+        return variables != null ? variables.indexOf(variableName) + columnOffset() : -1;
     }
     
     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>";
-        } else if (columnIndex == 0) {
+        } else if (isEnabledColumn(columnIndex)) {
             return "Enabled";
         } else {
-            return headers.get(columnIndex - 1);
+            return headers.get(columnIndex - columnOffset());
         }
     }
 
     @Override
     public Object getDataValue(int columnIndex, int rowIndex) {
-        if (columnIndex == 0) {
+        if (isEnabledColumn(columnIndex)) {
             return isEnabled(rowIndex);
         }
-        return getRecord(rowIndex).get(columnIndex - 1);
+        return getRecord(rowIndex).get(columnIndex - columnOffset());
     }
 
     @Override
     public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
-        if (columnIndex == 0) {
+        if (isEnabledColumn(columnIndex)) {
             boolean value = Boolean.parseBoolean((String) newValue);
-            enabled[filteredRows[rowIndex]] = value;
+            enabled[recordIndex(rowIndex)] = value;
             fireEnableEvent(rowIndex, value);
         }
     }
@@ -105,7 +139,7 @@ public class TechTypeTableDataProvider implements IDataProvider {
         if (records.isEmpty()) {
             return 0;
         }
-        return records.get(0).size() + 1;
+        return records.get(0).size() + columnOffset();
     }
 
     @Override
@@ -114,14 +148,18 @@ public class TechTypeTableDataProvider implements IDataProvider {
     }
 
     public boolean isEditable(int columnIndex, int rowIndex) {
-        return columnIndex == 0;
+        return isEnabledColumn(columnIndex);
     }
 
+       private boolean isEnabledColumn(int columnIndex) {
+               return showEnabled && columnIndex == 0;
+       }
+
     public void setFilter(String text) {
         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 +199,9 @@ public class TechTypeTableDataProvider implements IDataProvider {
             }
         }
 
+        enabled = new boolean[records.size()];
+        sortedRows = IntStream.range(0, records.size()).toArray();
+        
         setFilter(null);
     }
 
@@ -208,8 +249,30 @@ 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)) {
+               int offset = columnOffset();
+            Comparator<Integer> comparator = isEnabledColumn(columnIndex) ?
+                    Comparator.comparing(k -> enabled[sortedRows[(int) k]]) :
+                    Comparator.comparing(k -> records.get(sortedRows[(int) k]).get(columnIndex-offset), 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