]> gerrit.simantics Code Review - simantics/district.git/blob
33a428f41e71fddb25a729aed13b5467aa8f2fed
[simantics/district.git] /
1 package org.simantics.district.selection.ui;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.List;
7
8 import org.simantics.Simantics;
9 import org.simantics.browsing.ui.common.AdaptableHintContext;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.request.ResourceRead;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.QueryIndexUtils;
15 import org.simantics.db.layer0.SelectionHints;
16 import org.simantics.db.layer0.request.ActiveModels;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.request.Read;
19 import org.simantics.district.selection.ElementSelectionResource;
20 import org.simantics.district.selection.ElementSelector;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.ui.selection.AnyResource;
23 import org.simantics.ui.selection.AnyVariable;
24 import org.simantics.ui.selection.WorkbenchSelectionContentType;
25
26 public class ElementSelectionTools {
27
28         public static final class SelectionElement extends AdaptableHintContext {
29                 public SelectionElement(Key[] keys) {
30                         super(keys);
31                 }
32
33                 @SuppressWarnings("unchecked")
34                 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
35                         Resource element = getHint(SelectionHints.KEY_MAIN);
36                         if (contentType instanceof AnyResource) {
37                                 return (T)element;
38                         }
39                         else if (contentType instanceof AnyVariable) {
40                                 AnyVariable type = (AnyVariable) contentType;
41                                 try {
42                                         return (T) type.processor.syncRequest(new ResourceRead<Variable>(element) {
43                                                 public Variable perform(ReadGraph graph) throws DatabaseException {
44                                                         return ElementSelector.getVariableForElement(graph, resource);
45                                                 }
46                                         });
47                                 } catch (DatabaseException e) {
48                                         return null;
49                                 }
50                         }
51                         
52                         return null;
53                 }
54         }
55
56         public static final class SelectionsRequest implements Read<Collection<ElementSelector>> {
57                 @Override
58                 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
59                         Layer0 L0 = Layer0.getInstance(graph);
60                         ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
61                         
62                         Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
63                         if (model == null) {
64                                 return Collections.emptyList();
65                         }
66                         
67                         List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
68                         if (libs.isEmpty())
69                                 return Collections.emptyList();
70                         
71                         Resource lib = libs.get(0);
72                         
73                         List<ElementSelector> result = new ArrayList<>();
74                         for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
75                                 if (!graph.isInstanceOf(selection, ES.Selection))
76                                         continue;
77                                 
78                                 result.add(ElementSelector.getSelector(graph, selection));
79                         }
80                         
81                         return result;
82                 }
83         }
84
85 }