X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2FDistrictNetworkUIUtil.java;fp=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2FDistrictNetworkUIUtil.java;h=9ce6937a50b5fbea4866cb222e3ab22da442e17e;hb=6a4259eb4fee7e5b8bfbe818f7325d73d99a7803;hp=0000000000000000000000000000000000000000;hpb=65fd04915f975756a22bdc09d2b8068105993631;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkUIUtil.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkUIUtil.java new file mode 100644 index 00000000..9ce6937a --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkUIUtil.java @@ -0,0 +1,168 @@ +package org.simantics.district.network.ui; + +import java.util.Arrays; +import java.util.Collection; +import java.util.function.Consumer; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.IEditorPart; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.PossibleModel; +import org.simantics.db.layer0.variable.RVI; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.district.network.DistrictNetworkUtil; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.actions.NavigateToTarget; +import org.simantics.scl.runtime.tuple.Tuple4; +import org.simantics.ui.selection.WorkbenchSelectionUtils; +import org.simantics.utils.threads.ThreadUtils; +import org.simantics.utils.ui.ISelectionUtils; + +/** + * @author Tuukka Lehtonen + * @since 1.35.0 + */ +public class DistrictNetworkUIUtil { + + public static Resource getInputResource(ReadGraph graph, Object input) throws DatabaseException { + if (input instanceof Resource) { + return (Resource) input; + } else if (input instanceof Variable) { + return ((Variable) input).getPossibleRepresents(graph); + } else if (input instanceof ISelection) { + return ISelectionUtils.filterSingleSelection((ISelection) input, Resource.class); + } else { + return WorkbenchSelectionUtils.getPossibleResource(graph, input); + } + } + + public static class GetInputResource extends UnaryRead { + + public GetInputResource(Object input) { + super(input); + } + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + return getInputResource(graph, parameter); + } + + } + + public static class Input extends Tuple4 { + public Input(Resource model, Resource diagram, Resource element, RVI diagramCompositeRvi) { + super(model, diagram, element, diagramCompositeRvi); + } + public Resource model() { + return (Resource) get(0); + } + public Resource diagram() { + return (Resource) get(1); + } + public Resource element() { + return (Resource) get(2); + } + public RVI rvi() { + return (RVI) get(3); + } + } + + public static class FindMappedDNElement extends UnaryRead { + + public FindMappedDNElement(Object parameter) { + super(parameter); + } + + @Override + public Input perform(ReadGraph graph) throws DatabaseException { + Resource e = DistrictNetworkUtil.getMappedDNElement(graph, + DistrictNetworkUtil.getDiagramElement(graph, + getInputResource(graph, parameter))); + return e != null ? graph.syncRequest(new ElementToInput(e)) : null; + } + + } + + public static class FindMappedComponent extends UnaryRead { + + public FindMappedComponent(Object parameter) { + super(parameter); + } + + @Override + public Input perform(ReadGraph graph) throws DatabaseException { + Resource e = DistrictNetworkUtil.getMappedElement(graph, + getInputResource(graph, parameter)); + return e != null ? graph.syncRequest(new ElementToInput(e)) : null; + } + + } + + public static class ElementToInput extends UnaryRead { + + public ElementToInput(Resource element) { + super(element); + } + + @Override + public Input perform(ReadGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + Resource diagram = graph.getPossibleObject(parameter, L0.PartOf); + if (diagram == null) + return null; + + Resource model = graph.syncRequest(new PossibleModel(diagram)); + if (model == null) + return null; + + RVI rvi = getDiagramCompositeRvi(graph, diagram); + if (rvi == null) + return null; + + return new Input(model, diagram, parameter, rvi); + } + + private static RVI getDiagramCompositeRvi(ReadGraph graph, Resource diagram) throws DatabaseException { + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite); + if (composite == null) + return null; + Variable v = Variables.getPossibleVariable(graph, composite); + return v != null ? v.getPossibleRVI(graph) : null; + } + + } + + public static Consumer editorActivationCallback(final Collection selectedObjects) { + return part -> { + final ICanvasContext openedCanvas = (ICanvasContext) part.getAdapter(ICanvasContext.class); + assert openedCanvas != null; + // CanvasContext-wide denial of initial zoom-to-fit on diagram open. + openedCanvas.getDefaultHintContext().setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.FALSE); + ThreadUtils.asyncExec(openedCanvas.getThreadAccess(), + NavigateToTarget.elementSelectorZoomer(openedCanvas, selectedObjects, false)); + }; + } + + public static void openEditorWithSelection(String editorId, Input input, Object... selection) { + NavigateToTarget.editorActivator( + editorId, + input.diagram(), + input.model(), + input.rvi(), + DistrictNetworkUIUtil.editorActivationCallback(Arrays.asList(selection))) + .run(); + } + + public static void openDNDiagramEditorWithSelection(Input input, Object... selection) { + openEditorWithSelection(DistrictDiagramEditor.ID, input, selection); + } + +}