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.ui.SimanticsUI;
31 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
34 * @author Tuukka Lehtonen
36 public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter {
38 private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
40 public OpenDiagramFromIssue() {
41 super("Open Diagram Containing Referenced Element", Activator.COMPOSITE_ICON);
44 protected String getEditorId() {
49 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
50 return findElement(g, r) != null;
54 public void openEditor(final Resource r) throws Exception {
55 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
57 public void run(ReadGraph g) throws DatabaseException {
59 Layer0 L0 = Layer0.getInstance(g);
60 ModelingResources MOD = ModelingResources.getInstance(g);
62 Resource element = findElement(g, r);
63 if(element == null) return;
65 System.err.println("element " + g.getURI(element));
67 Resource diagram = g.getSingleObject(element, L0.PartOf);
68 Resource composite = g.getSingleObject(diagram, MOD.DiagramToComposite);
70 OpenDiagramFromConfigurationAdapter.openEditor(g, composite, getEditorId(), Collections.<Object>singleton(element));
76 private static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
78 Layer0 L0 = Layer0.getInstance(graph);
79 ModelingResources MOD = ModelingResources.getInstance(graph);
81 Resource diagram = graph.getPossibleObject(element, L0.PartOf);
82 if (diagram == null) return null;
84 return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
88 private static Resource findElement(ReadGraph graph, Resource issue) throws DatabaseException {
90 IssueResource ISSUE = IssueResource.getInstance(graph);
91 DiagramResource DIA = DiagramResource.getInstance(graph);
93 if(!graph.isInstanceOf(issue, ISSUE.Issue)) return null;
95 Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);
96 for(Resource context : ListUtils.toList(graph, list)) {
97 if(graph.isInstanceOf(context, DIA.Element) && !graph.isInstanceOf(context, DIA.Diagram)) {
107 public static Collection<Object> findElementObjects(ReadGraph g, Resource module) throws DatabaseException {
108 DiagramResource DIA = DiagramResource.getInstance(g);
109 ModelingResources MOD = ModelingResources.getInstance(g);
110 final Collection<Object> selectedObjects = new ArrayList<Object>();
111 for (Resource element : g.getObjects(module, MOD.ComponentToElement)) {
112 if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) {
113 // Use external flag primarily if one exists in the correspondences
114 selectedObjects.clear();
115 selectedObjects.add(element);
117 } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) {
118 selectedObjects.add(element);
119 } else if (g.isInstanceOf(element, DIA.Connection)) {
120 // Okay, we need to find a part of the connection
121 ConnectionUtil cu = new ConnectionUtil(g);
122 cu.gatherConnectionParts(element, selectedObjects);
124 selectedObjects.add(element);
127 for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) {
128 selectedObjects.add(element);
130 return selectedObjects;