]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java
Sorting support for tech type table columns
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / requests / EnableTechTypeItem.java
1 package org.simantics.district.network.techtype.requests;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.Datatypes;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.binding.impl.ArrayListBinding;
11 import org.simantics.db.Resource;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.common.request.WriteRequest;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.util.Layer0Utils;
16 import org.simantics.district.network.ontology.DistrictNetworkResource;
17
18 /**
19  * Change the enabled/disabled state of a single tech type table record
20  */
21 public class EnableTechTypeItem extends WriteRequest {
22         
23         Resource table;
24         int itemIndex;
25         boolean enable;
26
27         public EnableTechTypeItem(Resource table, int itemIndex, boolean enable) {
28                 super();
29                 this.table = table;
30                 this.itemIndex = itemIndex;
31                 this.enable = enable;
32         }
33
34         @Override
35         public void perform(WriteGraph graph) throws DatabaseException {
36                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
37                 
38                 Binding binding = new ArrayListBinding(Datatypes.INTEGER_ARRAY, Bindings.INTEGER);
39                 List<Integer> enabled = graph.getPossibleRelatedValue2(table, DN.TechType_TechTypeTable_HasEnabledItems, binding);
40                 if (enabled == null)
41                         enabled = Collections.emptyList();
42                 
43                 if (enable) {
44                         if (!enabled.contains(itemIndex)) {
45                                 enabled = new ArrayList<>(enabled);
46                                 enabled.add((Integer) itemIndex);
47                                 graph.getSession().markUndoPoint();
48                                 graph.claimLiteral(table, DN.TechType_TechTypeTable_HasEnabledItems, enabled, binding);
49                                 Layer0Utils.addCommentMetadata(graph, "Enable tech type table item " + (itemIndex + 1));
50                         }
51                 } else {
52                         if (enabled.contains(itemIndex)) {
53                                 enabled = new ArrayList<>(enabled);
54                                 enabled.remove((Integer) itemIndex);  // note - without the cast, itemIndex is list index, not element value
55                                 graph.getSession().markUndoPoint();
56                                 graph.claimLiteral(table, DN.TechType_TechTypeTable_HasEnabledItems, enabled, binding);
57                                 Layer0Utils.addCommentMetadata(graph, "Disable tech type table item " + (itemIndex + 1));
58                         }
59                 }
60         }
61
62 }