]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java
Selection of highlight color for District Finder queries
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / ElementSelectionTools.java
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 import org.simantics.utils.ui.color.Color;
26
27 public class ElementSelectionTools {
28
29         public static final class SelectionElement extends AdaptableHintContext {
30                 public SelectionElement(Key[] keys) {
31                         super(keys);
32                 }
33
34                 @SuppressWarnings("unchecked")
35                 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
36                         Resource element = getHint(SelectionHints.KEY_MAIN);
37                         if (contentType instanceof AnyResource) {
38                                 return (T)element;
39                         }
40                         else if (contentType instanceof AnyVariable) {
41                                 AnyVariable type = (AnyVariable) contentType;
42                                 try {
43                                         return (T) type.processor.syncRequest(new ResourceRead<Variable>(element) {
44                                                 public Variable perform(ReadGraph graph) throws DatabaseException {
45                                                         return ElementSelector.getVariableForElement(graph, resource);
46                                                 }
47                                         });
48                                 } catch (DatabaseException e) {
49                                         return null;
50                                 }
51                         }
52                         
53                         return null;
54                 }
55         }
56
57         public static final class SelectionsRequest implements Read<Collection<ElementSelector>> {
58                 @Override
59                 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
60                         Layer0 L0 = Layer0.getInstance(graph);
61                         ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
62                         
63                         Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
64                         if (model == null) {
65                                 return Collections.emptyList();
66                         }
67                         
68                         List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
69                         if (libs.isEmpty())
70                                 return Collections.emptyList();
71                         
72                         Resource lib = libs.get(0);
73                         
74                         List<ElementSelector> result = new ArrayList<>();
75                         for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
76                                 if (!graph.isInstanceOf(selection, ES.Selection))
77                                         continue;
78                                 
79                                 result.add(ElementSelector.getSelector(graph, selection));
80                         }
81                         
82                         return result;
83                 }
84         }
85
86         public static float[] colorToLiteral(Color color) {
87                 // BGRA float values out
88                 return new float[] {
89                                 (float)color.getB() / 255,
90                                 (float)color.getG() / 255,
91                                 (float)color.getR() / 255,
92                                 1.f,
93                         };
94         }
95
96         public static Color literalToColor(float[] color) {
97                 // BGRA float values in
98                 return new Color((int) (color[2] * 255), (int) (color[1] * 255), (int) (color[0] * 255));
99         }
100
101 }