]> gerrit.simantics Code Review - simantics/platform.git/blob
8595d737440320c24e43816ed400ba622a159143
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17
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;
34
35 /**
36  * @author Tuukka Lehtonen
37  */
38 public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter {
39
40     private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
41
42     public OpenDiagramFromIssue() {
43         super("Open Diagram Containing Referenced Element", Activator.COMPOSITE_ICON);
44     }
45
46     protected String getEditorId() {
47         return EDITOR_ID;
48     }
49
50     @Override
51     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
52         return findElement(g, r) != null;
53     }
54
55     @Override
56     public void openEditor(final Resource r) throws Exception {
57         SimanticsUI.getSession().asyncRequest(new ReadRequest() {
58             @Override
59             public void run(ReadGraph g) throws DatabaseException {
60                 
61                 Layer0 L0 = Layer0.getInstance(g);
62                 ModelingResources MOD = ModelingResources.getInstance(g);
63                 
64                 Resource element = findElement(g, r);
65                 if(element == null) return;
66                 
67                 System.err.println("element " + g.getURI(element));
68                 
69                 Resource diagram = g.getSingleObject(element, L0.PartOf);
70                 Resource composite = g.getSingleObject(diagram, MOD.DiagramToComposite);
71                                 
72                 OpenDiagramFromConfigurationAdapter.openEditor(g, composite, getEditorId(), Collections.<Object>singleton(element));
73                 
74             }
75         });
76     }
77
78     private static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
79
80         Layer0 L0 = Layer0.getInstance(graph);
81         ModelingResources MOD = ModelingResources.getInstance(graph);
82
83         Resource diagram = graph.getPossibleObject(element, L0.PartOf);
84         if (diagram == null) return null;
85
86         return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
87         
88     }
89     
90     private static Resource findElement(ReadGraph graph, Resource issue) throws DatabaseException {
91         
92         IssueResource ISSUE = IssueResource.getInstance(graph);
93         DiagramResource DIA = DiagramResource.getInstance(graph);
94         
95         if(!graph.isInstanceOf(issue, ISSUE.Issue)) return null;
96         
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)) {
100                         return context;
101                 }
102                 
103         }
104         
105         return null;
106         
107     }
108
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);
118                 break;
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);
125             } else {
126                 selectedObjects.add(element);
127             }
128         }
129         for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) {
130                 selectedObjects.add(element);
131         }
132         return selectedObjects;
133     }    
134     
135 }