]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.selection/src/org/simantics/district/selection/ElementSelectionUtils.java
Resolve tie situations in n lowest/highest element selection.
[simantics/district.git] / org.simantics.district.selection / src / org / simantics / district / selection / ElementSelectionUtils.java
1 package org.simantics.district.selection;
2
3 import java.util.List;
4 import java.util.UUID;
5
6 import org.simantics.Simantics;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.RequestProcessor;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.QueryIndexUtils;
13 import org.simantics.db.layer0.request.PossibleActiveModel;
14 import org.simantics.db.request.WriteResult;
15 import org.simantics.layer0.Layer0;
16
17 public class ElementSelectionUtils {
18         public static Resource ensureSelectionLibrary(RequestProcessor graph) throws DatabaseException {
19                 return graph.syncRequest(new WriteResult<Resource>() {
20                         @Override
21                         public Resource perform(WriteGraph graph) throws DatabaseException {
22                                 ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
23                                 
24                                 Resource model = graph.sync(new PossibleActiveModel(Simantics.getProjectResource()));
25                                 if (model == null) return null;
26                                 
27                                 List<Resource> libs = QueryIndexUtils.searchByTypeShallow(graph, model, ES.SelectionLibrary);
28                                 if (libs.isEmpty()) {
29                                         Resource lib = graph.newResource();
30                                         Layer0 L0 = Layer0.getInstance(graph);
31                                         graph.claim(lib, L0.InstanceOf, ES.SelectionLibrary);
32                                         graph.claim(model, L0.ConsistsOf, lib);
33                                         graph.claimLiteral(lib, L0.HasName, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
34                                         return lib;
35                                 }
36                                 else {
37                                         return libs.get(0);
38                                 }
39                         }
40                 });
41         }
42
43 }