1 package org.simantics.district.network.ui;
3 import java.util.Arrays;
4 import java.util.Collection;
6 import java.util.function.Consumer;
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;
33 * @author Tuukka Lehtonen
36 public class DistrictNetworkUIUtil {
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);
46 return WorkbenchSelectionUtils.getPossibleResource(graph, input);
50 public static class GetInputResource extends UnaryRead<Object, Resource> {
52 public GetInputResource(Object input) {
57 public Resource perform(ReadGraph graph) throws DatabaseException {
58 return getInputResource(graph, parameter);
63 public static class Input extends Tuple4 {
64 public Input(Resource model, Resource diagram, Resource element, RVI diagramCompositeRvi) {
65 super(model, diagram, element, diagramCompositeRvi);
67 public Resource model() {
68 return (Resource) get(0);
70 public Resource diagram() {
71 return (Resource) get(1);
73 public Resource element() {
74 return (Resource) get(2);
81 public static class FindMappedDNElement extends UnaryRead<Object, Input> {
83 public FindMappedDNElement(Object parameter) {
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;
97 public static class FindMappedComponent extends UnaryRead<Object, Input> {
99 public FindMappedComponent(Object parameter) {
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;
112 public static class ElementToInput extends UnaryRead<Resource, Input> {
114 public ElementToInput(Resource element) {
119 public Input perform(ReadGraph graph) throws DatabaseException {
120 Layer0 L0 = Layer0.getInstance(graph);
121 Resource diagram = graph.getPossibleObject(parameter, L0.PartOf);
125 Resource model = graph.syncRequest(new PossibleModel(diagram));
129 RVI rvi = getDiagramCompositeRvi(graph, diagram);
133 return new Input(model, diagram, parameter, rvi);
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)
141 Variable v = Variables.getPossibleVariable(graph, composite);
142 return v != null ? v.getPossibleRVI(graph) : null;
147 public static Consumer<IEditorPart> editorActivationCallback(final Collection<? extends Object> selectedObjects) {
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));
158 public static void openEditorWithSelection(String editorId, Input input, Object... selection) {
159 NavigateToTarget.editorActivator(
164 DistrictNetworkUIUtil.editorActivationCallback(Arrays.asList(selection)))
168 public static void openDNDiagramEditorWithSelection(Input input, Object... selection) {
169 openEditorWithSelection(DistrictDiagramEditor.ID, input, selection);
172 public static boolean openDNDiagramWithSelection(Display display, List<Resource> dnElements) throws DatabaseException {
173 if (dnElements.isEmpty())
176 Input in = Simantics.getSession().syncRequest(
177 new ElementToInput(dnElements.get(0)));
181 SWTUtils.asyncExec(display,
182 () -> openDNDiagramEditorWithSelection(in, dnElements.toArray()));