1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.common.utils.ListUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.diagram.content.ConnectionUtil;
24 import org.simantics.diagram.flag.FlagUtil;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.issues.ontology.IssueResource;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.modeling.ui.Activator;
30 import org.simantics.structural.stubs.StructuralResource2;
31 import org.simantics.ui.SimanticsUI;
32 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
33 import org.simantics.utils.datastructures.Pair;
36 * @author Tuukka Lehtonen
38 public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter {
40 private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
42 public OpenDiagramFromIssue() {
43 super("Open Diagram Containing Referenced Element", Activator.COMPOSITE_ICON);
46 protected String getEditorId() {
51 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
52 return findElement(g, r) != null;
56 public void openEditor(final Resource r) throws Exception {
57 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
59 public void run(ReadGraph g) throws DatabaseException {
61 Layer0 L0 = Layer0.getInstance(g);
62 ModelingResources MOD = ModelingResources.getInstance(g);
64 Resource element = findElement(g, r);
65 if(element == null) return;
67 System.err.println("element " + g.getURI(element));
69 Resource diagram = g.getSingleObject(element, L0.PartOf);
70 Resource composite = g.getSingleObject(diagram, MOD.DiagramToComposite);
72 OpenDiagramFromConfigurationAdapter.openEditor(g, composite, getEditorId(), Collections.<Object>singleton(element));
78 private static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
80 Layer0 L0 = Layer0.getInstance(graph);
81 ModelingResources MOD = ModelingResources.getInstance(graph);
83 Resource diagram = graph.getPossibleObject(element, L0.PartOf);
84 if (diagram == null) return null;
86 return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
90 private static Resource findElement(ReadGraph graph, Resource issue) throws DatabaseException {
92 IssueResource ISSUE = IssueResource.getInstance(graph);
93 DiagramResource DIA = DiagramResource.getInstance(graph);
95 if(!graph.isInstanceOf(issue, ISSUE.Issue)) return null;
97 Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);
98 for(Resource context : ListUtils.toList(graph, list)) {
99 if(graph.isInstanceOf(context, DIA.Element) && !graph.isInstanceOf(context, DIA.Diagram)) {
109 public static Collection<Object> findElementObjects(ReadGraph g, Resource module) throws DatabaseException {
110 DiagramResource DIA = DiagramResource.getInstance(g);
111 ModelingResources MOD = ModelingResources.getInstance(g);
112 final Collection<Object> selectedObjects = new ArrayList<Object>();
113 for (Resource element : g.getObjects(module, MOD.ComponentToElement)) {
114 if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) {
115 // Use external flag primarily if one exists in the correspondences
116 selectedObjects.clear();
117 selectedObjects.add(element);
119 } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) {
120 selectedObjects.add(element);
121 } else if (g.isInstanceOf(element, DIA.Connection)) {
122 // Okay, we need to find a part of the connection
123 ConnectionUtil cu = new ConnectionUtil(g);
124 cu.gatherConnectionParts(element, selectedObjects);
126 selectedObjects.add(element);
129 for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) {
130 selectedObjects.add(element);
132 return selectedObjects;