]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkUIUtil.java
Sorting support for tech type table columns
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / DistrictNetworkUIUtil.java
1 package org.simantics.district.network.ui;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.function.Consumer;
7
8 import org.eclipse.jface.viewers.ISelection;
9 import org.eclipse.swt.widgets.Display;
10 import org.eclipse.ui.IEditorPart;
11 import org.simantics.Simantics;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.UnaryRead;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.request.PossibleModel;
17 import org.simantics.db.layer0.variable.RVI;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.db.layer0.variable.Variables;
20 import org.simantics.district.network.DistrictNetworkUtil;
21 import org.simantics.g2d.canvas.ICanvasContext;
22 import org.simantics.g2d.diagram.DiagramHints;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.modeling.ModelingResources;
25 import org.simantics.modeling.actions.NavigateToTarget;
26 import org.simantics.scl.runtime.tuple.Tuple4;
27 import org.simantics.ui.selection.WorkbenchSelectionUtils;
28 import org.simantics.utils.threads.ThreadUtils;
29 import org.simantics.utils.ui.ISelectionUtils;
30 import org.simantics.utils.ui.SWTUtils;
31
32 /**
33  * @author Tuukka Lehtonen
34  * @since 1.35.0
35  */
36 public class DistrictNetworkUIUtil {
37
38     public static Resource getInputResource(ReadGraph graph, Object input) throws DatabaseException {
39         if (input instanceof Resource) {
40             return (Resource) input;
41         } else if (input instanceof Variable) {
42             return ((Variable) input).getPossibleRepresents(graph);
43         } else if (input instanceof ISelection) {
44             return ISelectionUtils.filterSingleSelection((ISelection) input, Resource.class);
45         } else {
46             return WorkbenchSelectionUtils.getPossibleResource(graph, input);
47         }
48     }
49
50     public static class GetInputResource extends UnaryRead<Object, Resource> {
51
52         public GetInputResource(Object input) {
53             super(input);
54         }
55
56         @Override
57         public Resource perform(ReadGraph graph) throws DatabaseException {
58             return getInputResource(graph, parameter);
59         }
60
61     }
62
63     public static class Input extends Tuple4 {
64         public Input(Resource model, Resource diagram, Resource element, RVI diagramCompositeRvi) {
65             super(model, diagram, element, diagramCompositeRvi);
66         }
67         public Resource model() {
68             return (Resource) get(0);
69         }
70         public Resource diagram() {
71             return (Resource) get(1);
72         }
73         public Resource element() {
74             return (Resource) get(2);
75         }
76         public RVI rvi() {
77             return (RVI) get(3);
78         }
79     }
80
81     public static class FindMappedDNElement extends UnaryRead<Object, Input> {
82
83         public FindMappedDNElement(Object parameter) {
84             super(parameter);
85         }
86
87         @Override
88         public Input perform(ReadGraph graph) throws DatabaseException {
89             Resource e = DistrictNetworkUtil.getMappedDNElement(graph,
90                     DistrictNetworkUtil.getDiagramElement(graph,
91                             getInputResource(graph, parameter)));
92             return e != null ? graph.syncRequest(new ElementToInput(e)) : null;
93         }
94
95     }
96
97     public static class FindMappedComponent extends UnaryRead<Object, Input> {
98
99         public FindMappedComponent(Object parameter) {
100             super(parameter);
101         }
102
103         @Override
104         public Input perform(ReadGraph graph) throws DatabaseException {
105             Resource e = DistrictNetworkUtil.getMappedElement(graph,
106                     getInputResource(graph, parameter));
107             return e != null ? graph.syncRequest(new ElementToInput(e)) : null;
108         }
109
110     }
111
112     public static class ElementToInput extends UnaryRead<Resource, Input> {
113
114         public ElementToInput(Resource element) {
115             super(element);
116         }
117
118         @Override
119         public Input perform(ReadGraph graph) throws DatabaseException {
120             Layer0 L0 = Layer0.getInstance(graph);
121             Resource diagram = graph.getPossibleObject(parameter, L0.PartOf);
122             if (diagram == null)
123                 return null;
124
125             Resource model = graph.syncRequest(new PossibleModel(diagram));
126             if (model == null)
127                 return null;
128
129             RVI rvi = getDiagramCompositeRvi(graph, diagram);
130             if (rvi == null)
131                 return null;
132
133             return new Input(model, diagram, parameter, rvi);
134         }
135
136         private static RVI getDiagramCompositeRvi(ReadGraph graph, Resource diagram) throws DatabaseException {
137             ModelingResources MOD = ModelingResources.getInstance(graph);
138             Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
139             if (composite == null)
140                 return null;
141             Variable v = Variables.getPossibleVariable(graph, composite);
142             return v != null ? v.getPossibleRVI(graph) : null;
143         }
144
145     }
146
147     public static Consumer<IEditorPart> editorActivationCallback(final Collection<? extends Object> selectedObjects) {
148         return part -> {
149             final ICanvasContext openedCanvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
150             assert openedCanvas != null;
151             // CanvasContext-wide denial of initial zoom-to-fit on diagram open.
152             openedCanvas.getDefaultHintContext().setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.FALSE);
153             ThreadUtils.asyncExec(openedCanvas.getThreadAccess(),
154                     NavigateToTarget.elementSelectorZoomer(openedCanvas, selectedObjects, false));
155         };
156     }
157
158     public static void openEditorWithSelection(String editorId, Input input, Object... selection) {
159         NavigateToTarget.editorActivator(
160                 editorId,
161                 input.diagram(),
162                 input.model(),
163                 input.rvi(),
164                 DistrictNetworkUIUtil.editorActivationCallback(Arrays.asList(selection)))
165         .run();
166     }
167
168     public static void openDNDiagramEditorWithSelection(Input input, Object... selection) {
169         openEditorWithSelection(DistrictDiagramEditor.ID, input, selection);
170     }
171
172     public static boolean openDNDiagramWithSelection(Display display, List<Resource> dnElements) throws DatabaseException {
173         if (dnElements.isEmpty())
174             return false;
175
176         Input in = Simantics.getSession().syncRequest(
177                 new ElementToInput(dnElements.get(0)));
178         if (in == null)
179             return false;
180
181         SWTUtils.asyncExec(display,
182                 () -> openDNDiagramEditorWithSelection(in, dnElements.toArray()));
183         return true;
184     }
185
186 }