From: xsimomjan Date: Wed, 26 Jul 2017 10:42:20 +0000 (+0300) Subject: Open diagram from issue X-Git-Tag: v1.31.0~264^2~23 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F67%2F767%2F2;p=simantics%2Fplatform.git Open diagram from issue refs #7385 Change-Id: I23b599ec2708146c7de19186576d5a0e8d80fc56 --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromIssue.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromIssue.java index 6af1ff84b..7fd13bbdd 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromIssue.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromIssue.java @@ -14,12 +14,16 @@ package org.simantics.modeling.ui.diagramEditor; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.List; +import java.util.Set; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; import org.simantics.diagram.content.ConnectionUtil; import org.simantics.diagram.flag.FlagUtil; import org.simantics.diagram.stubs.DiagramResource; @@ -27,8 +31,14 @@ import org.simantics.issues.ontology.IssueResource; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.modeling.ui.Activator; -import org.simantics.ui.SimanticsUI; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.ui.selection.AnyResource; +import org.simantics.ui.selection.AnyVariable; +import org.simantics.ui.selection.WorkbenchSelectionContentType; +import org.simantics.ui.selection.WorkbenchSelectionElement; import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.ui.ISelectionUtils; /** * @author Tuukka Lehtonen @@ -38,7 +48,7 @@ public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter { private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor"; public OpenDiagramFromIssue() { - super("Open Diagram Containing Referenced Element", Activator.COMPOSITE_ICON); + super("Open Diagram Containing Referenced Component", Activator.COMPOSITE_ICON); } protected String getEditorId() { @@ -46,88 +56,147 @@ public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter { } @Override - public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException { - return findElement(g, r) != null; + public boolean canHandle(ReadGraph g, Object input) throws DatabaseException { + return extractContext(g, input) != null; } @Override - public void openEditor(final Resource r) throws Exception { - SimanticsUI.getSession().asyncRequest(new ReadRequest() { + public void openEditor(Object input) throws Exception { + Simantics.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(g); - ModelingResources MOD = ModelingResources.getInstance(g); - - Resource element = findElement(g, r); - if(element == null) return; - - System.err.println("element " + g.getURI(element)); - - Resource diagram = g.getSingleObject(element, L0.PartOf); - Resource composite = g.getSingleObject(diagram, MOD.DiagramToComposite); - - OpenDiagramFromConfigurationAdapter.openEditor(g, composite, getEditorId(), Collections.singleton(element)); - + Pair> data = extractContext(g, input); + if (data != null) + OpenDiagramFromConfigurationAdapter.openEditor(g, data.first, getEditorId(), data.second); } }); } - private static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException { + private static Pair> extractContext(ReadGraph graph, Object input) throws DatabaseException { + Pair p = extractInput(graph, input); + Pair> result = p.first != null ? findConfigurationAndObjects(graph, p.first) : null; + if (result == null && p.second != null) + result = findConfigurationAndObjects(graph, p.second); + return result; + } - Layer0 L0 = Layer0.getInstance(graph); - ModelingResources MOD = ModelingResources.getInstance(graph); + protected static T extractContent(ReadGraph graph, Object input, WorkbenchSelectionContentType contentType, Class contentClass) throws DatabaseException { + if (contentClass.isInstance(input)) + return contentClass.cast(input); + if (input instanceof WorkbenchSelectionElement) { + WorkbenchSelectionElement single = (WorkbenchSelectionElement) input; + if (single != null) + return single.getContent(contentType); + } + return ISelectionUtils.filterSingleSelection(input, contentClass); + } - Resource diagram = graph.getPossibleObject(element, L0.PartOf); - if (diagram == null) return null; + protected static Pair extractInput(ReadGraph graph, Object input) throws DatabaseException { + return Pair.make( + extractContent(graph, input, new AnyVariable(graph), Variable.class), + extractContent(graph, input, new AnyResource(graph), Resource.class)); + } - return graph.getPossibleObject(diagram, MOD.DiagramToComposite); - + protected static Pair> findConfigurationAndObjects(ReadGraph graph, Resource issue) throws DatabaseException { + IssueResource ISSUE = IssueResource.getInstance(graph); + if (!graph.isInstanceOf(issue, ISSUE.Issue)) + return null; + List contexts = graph.getRelatedValue2(issue, ISSUE.Issue_contexts); + return contexts != null ? findConfigurationAndObjects(graph, contexts) : null; } - - private static Resource findElement(ReadGraph graph, Resource issue) throws DatabaseException { - - IssueResource ISSUE = IssueResource.getInstance(graph); - DiagramResource DIA = DiagramResource.getInstance(graph); - - if(!graph.isInstanceOf(issue, ISSUE.Issue)) return null; - - Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts); - for(Resource context : ListUtils.toList(graph, list)) { - if(graph.isInstanceOf(context, DIA.Element) && !graph.isInstanceOf(context, DIA.Diagram)) { - return context; - } - - } - - return null; - + + protected static Pair> findConfigurationAndObjects(ReadGraph graph, Variable v) throws DatabaseException { + List contexts = v.getPossiblePropertyValue(graph, IssueResource.getInstance(graph).Issue_contexts); + return contexts != null ? findConfigurationAndObjects(graph, contexts) : null; + } + + protected static Pair> findConfigurationAndObjects(ReadGraph graph, List contexts) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + StructuralResource2 STR = StructuralResource2.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + + for(Resource context : contexts) { + Set types = graph.getTypes(context); + if (types.contains(DIA.Element)) { + Resource config = findConfigurationForElement(graph, context); + if (config != null) { + Collection elements = Collections.singleton(context); + return Pair.make(config, elements); + } + } else if (types.contains(STR.Component) && !types.contains(STR.Composite)) { + Resource config = graph.getPossibleObject(context, L0.PartOf); + if (config != null) { + return Pair.make(config, findElementObjects(graph, context)); + } + } else if (types.contains(STR.Connection)) { + Resource element = graph.getPossibleObject(context, MOD.ConnectionToDiagramConnection); + if (element != null) { + Resource config = findConfigurationForElement(graph, element); + if (config != null) { + return Pair.make(config, Collections.singleton(element)); + } + } + } + } + + return null; } - public static Collection findElementObjects(ReadGraph g, Resource module) throws DatabaseException { + protected static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource diagram = graph.getPossibleObject(element, L0.PartOf); + if (diagram == null) return null; + return graph.getPossibleObject(diagram, MOD.DiagramToComposite); + } + + protected static Collection findElementObjects(ReadGraph g, Resource component) throws DatabaseException { + Collection result = findElementObjects(g, component, ""); + ModelingResources MOD = ModelingResources.getInstance(g); + for (Resource element : g.getObjects(component, MOD.HasParentComponent_Inverse)) + result.add(element); + return result; + } + + public static Collection findElementObjects(ReadGraph g, Resource component, String rviFromComponent) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(g); 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); + final Collection selectedObjects = new ArrayList<>(4); + if (rviFromComponent.isEmpty()) { + // The selected objects are configuration objects + for (Resource element : g.getObjects(component, 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); + } + } + } else { + // The selected objects are generated components + for (Resource refElement : g.getObjects(component, MOD.HasParentComponent_Inverse)) { + Resource relation = g.getPossibleObject(refElement, MOD.HasReferenceRelation); + if (relation != null) { + String suffix = g.getPossibleRelatedValue(relation, L0.HasName, Bindings.STRING); + if (suffix != null) { + if (rviFromComponent.equals(suffix)) { + selectedObjects.add(refElement); + } + } + } } - } - for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) { - selectedObjects.add(element); } return selectedObjects; - } + } }