]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/techtype/variable/Functions.java
Limit tech type code enumeration to enabled items
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / variable / Functions.java
index beb92684693ba96a00f683712a5741c235ad8b8d..d23a9a00e82a44212688bf2612ca45064594b6dc 100644 (file)
@@ -1,14 +1,23 @@
 package org.simantics.district.network.techtype.variable;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Map;
+
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.type.Datatype;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.AbstractChildVariable;
 import org.simantics.db.layer0.variable.ValueAccessor;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.district.network.techtype.TechTypeUtils;
+import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
+import org.simantics.district.network.techtype.requests.TechTypeTableData;
 import org.simantics.scl.reflection.annotations.SCLValue;
 import org.simantics.structural.stubs.StructuralResource2;
 
@@ -56,4 +65,32 @@ public class Functions {
                
                TechTypeUtils.updateComponent(graph, component.getRepresents(graph));
        }
+       
+       @SCLValue(type = "ReadGraph -> Resource -> a -> b")
+       public static Object techTypeKeys(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
+               if (!(context instanceof Variable))
+                       return Collections.<String>emptyList();
+               
+               Variable var = (Variable)context;
+               // Typically 'var' is <component>#<property>#HasDisplayValue#HasEnumerationValues
+               // We want the component part
+               while (!(var instanceof AbstractChildVariable))
+                       var = var.getParent(graph);
+               
+               Resource type = var.getType(graph, StructuralResource2.getInstance(graph).Component);
+               Resource model = var.getIndexRoot(graph);
+               if (model == null)
+                       return Collections.<String>emptyList();
+               Resource table = graph.syncRequest(new PossibleTechTypeTable(model , type), TransientCacheListener.instance());
+               if (table == null)
+                       return Collections.<String>emptyList();
+               
+               Map<String, Map<String, String>> data = graph.syncRequest(new TechTypeTableData(table, true), TransientCacheListener.instance());
+               ArrayList<String> result = new ArrayList<String>(data.keySet());
+               
+               // Sort so that all numbers are in growing order
+               result.sort(TechTypeUtils::compareNatural);
+               
+               return result;
+       }
 }