]> gerrit.simantics Code Review - simantics/district.git/blob - 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
1 package org.simantics.district.network.techtype.variable;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Map;
6
7 import org.simantics.databoard.binding.Binding;
8 import org.simantics.databoard.type.Datatype;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
13 import org.simantics.db.common.request.PossibleIndexRoot;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.variable.AbstractChildVariable;
16 import org.simantics.db.layer0.variable.ValueAccessor;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.district.network.techtype.TechTypeUtils;
19 import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
20 import org.simantics.district.network.techtype.requests.TechTypeTableData;
21 import org.simantics.scl.reflection.annotations.SCLValue;
22 import org.simantics.structural.stubs.StructuralResource2;
23
24 public class Functions {
25         @SCLValue(type = "ValueAccessor")
26         public static final ValueAccessor techTypeCodeValueAccessor = new ValueAccessor() {
27
28                 @Override
29                 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
30                         return org.simantics.db.layer0.function.All.standardValueAccessor.getValue(graph, context);
31                 }
32
33                 @Override
34                 public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
35                         return org.simantics.db.layer0.function.All.standardValueAccessor.getValue(graph, context, binding);
36                 }
37
38                 @Override
39                 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
40                         org.simantics.db.layer0.function.All.standardValueAccessor.setValue(graph, context, value);
41                         
42                         updateComponentProperties(graph, context, value);
43                 }
44
45                 @Override
46                 public void setValue(WriteGraph graph, Variable context, Object value, Binding binding)
47                                 throws DatabaseException {
48                         org.simantics.db.layer0.function.All.standardValueAccessor.setValue(graph, context, value, binding);
49                         
50                         updateComponentProperties(graph, context, value);
51                 }
52
53                 @Override
54                 public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
55                         return org.simantics.db.layer0.function.All.standardValueAccessor.getDatatype(graph, context);
56                 }
57                 
58         };
59
60         protected static void updateComponentProperties(WriteGraph graph, Variable context, Object value) throws DatabaseException {
61                 Variable component = context.getParent(graph);
62                 Resource type = component.getPossibleType(graph, StructuralResource2.getInstance(graph).Component);
63                 if (type == null)
64                         return;
65                 
66                 TechTypeUtils.updateComponent(graph, component.getRepresents(graph));
67         }
68         
69         @SCLValue(type = "ReadGraph -> Resource -> a -> b")
70         public static Object techTypeKeys(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
71                 if (!(context instanceof Variable))
72                         return Collections.<String>emptyList();
73                 
74                 Variable var = (Variable)context;
75                 // Typically 'var' is <component>#<property>#HasDisplayValue#HasEnumerationValues
76                 // We want the component part
77                 while (!(var instanceof AbstractChildVariable))
78                         var = var.getParent(graph);
79                 
80                 Resource type = var.getType(graph, StructuralResource2.getInstance(graph).Component);
81                 Resource model = var.getIndexRoot(graph);
82                 if (model == null)
83                         return Collections.<String>emptyList();
84                 Resource table = graph.syncRequest(new PossibleTechTypeTable(model , type), TransientCacheListener.instance());
85                 if (table == null)
86                         return Collections.<String>emptyList();
87                 
88                 Map<String, Map<String, String>> data = graph.syncRequest(new TechTypeTableData(table, true), TransientCacheListener.instance());
89                 ArrayList<String> result = new ArrayList<String>(data.keySet());
90                 
91                 // Sort so that all numbers are in growing order
92                 result.sort(TechTypeUtils::compareNatural);
93                 
94                 return result;
95         }
96 }