From: miettinen Date: Thu, 22 Aug 2013 05:06:10 +0000 (+0000) Subject: Links in Sysdyn search show the component on the diagram (refs #3966). Commented... X-Git-Tag: 1.8.1~254 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=363fff931017a7169b3db6eb3173ac08edab94d7;p=simantics%2Fsysdyn.git Links in Sysdyn search show the component on the diagram (refs #3966). Commented out the ConsumeUnnecessaryEntersAction. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27767 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 13726ef9..31fc8981 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -46,6 +46,10 @@ groupId="org.simantics.diagramEditor.group" priority="100"> + + - - + --> rs = tryFindDiagram(graph, r, v); + return !rs.isEmpty(); + } + + private Resource tryGetResource(ReadGraph graph, Object input) throws DatabaseException { + Resource r = ResourceAdaptionUtils.toSingleResource(input); + if (r == null) { + Variable v = AdaptionUtils.adaptToSingle(input, Variable.class); + r = findResource(graph, v); + } + return r; + } + + private Resource findResource(ReadGraph graph, Variable v) throws DatabaseException { + while (v != null) { + Resource r = v.getPossibleRepresents(graph); + if (r != null) + return r; + v = v.browsePossible(graph, "."); + } + return null; + } + + @Override + public void openEditor(final Object input) throws Exception { + final Display d = Display.getCurrent(); + if (d == null) + return; + + SimanticsUI.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + Resource r = tryGetResource(graph, input); + if (r == null) + return; + + Variable v = AdaptionUtils.adaptToSingle(input, Variable.class); + + final Collection rs = tryFindDiagram(graph, r, v); + if (rs.isEmpty()) + return; + + SWTThread.getThreadAccess(d).asyncExec(new Runnable() { + @Override + public void run() { + for (Runnable runnable : rs) { + runnable.run(); + } + } + }); + } + }); + } + + private Collection tryFindDiagram(ReadGraph g, Resource module, Variable variable) throws DatabaseException { + try { + return findDiagram(g, module, variable); + } catch (DatabaseException e) { + return Collections.emptyList(); + } + } + + private Collection findDiagram(ReadGraph g, Resource module, Variable variable) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(g); + SysdynResource SYSDYN = SysdynResource.getInstance(g); + + if (g.isInstanceOf(module, SYSDYN.IndependentVariable) || g.isInstanceOf(module, SYSDYN.Input)) { + Collection result = new ArrayList(1); + + Resource composite = g.getSingleObject(module, l0.PartOf); + + final Resource diagram = ComponentUtils.getCompositeDiagram(g, composite); + final ResourceArray compositePath = StructuralVariables.getCompositeArray(g, composite); + final ResourceArray variablePath = compositePath.removeFromBeginning(1); + + Resource model = StructuralVariables.getModel(g, compositePath.head()); + if (model == null) + return Collections.emptyList(); + String modelURI = g.getURI(model); + + String rvi = StructuralVariables.getRVI(g, variablePath); + if (rvi == null) + return Collections.emptyList(); + + if (variable != null) { + // Get proper RVI from variable if it exists. + Variable context = Variables.getPossibleContext(g, variable); + if (context != null) { + // We want the composite's RVI, not the component in it. + Variable parent = variable.getParent(g); + if (parent != null) { + String contextURI = context.getURI(g); + String parentURI = parent.getURI(g); + rvi = parentURI.replace(contextURI, ""); + } + } + } + + final Collection selectedObjects = findElementObjects(g, module); + + result.add( + NavigateToTarget.editorActivator(EDITOR_ID, diagram, modelURI, rvi, new Callback() { + @Override + public void run(IEditorPart 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)); + } + })); + return result; + } + + // Nothing to open + return Collections.emptyList(); + } + + public static Collection findElementObjects(ReadGraph g, Resource module) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(g); + ModelingResources MOD = ModelingResources.getInstance(g); + final Collection selectedObjects = new ArrayList(); + for (Resource element : g.getObjects(module, MOD.ComponentToElement)) { + if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) { + // Use external flag primarily if one exists in the correspondences + selectedObjects.clear(); + selectedObjects.add(element); + break; + } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) { + selectedObjects.add(element); + } else if (g.isInstanceOf(element, DIA.Connection)) { + // Okay, we need to find a part of the connection + ConnectionUtil cu = new ConnectionUtil(g); + cu.gatherConnectionParts(element, selectedObjects); + } else { + selectedObjects.add(element); + } + } + for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) { + selectedObjects.add(element); + } + return selectedObjects; + } + +}