]> gerrit.simantics Code Review - simantics/platform.git/blob
6af1ff84b95cee9abd1f6324680f29ccf3339889
[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.ui.SimanticsUI;
31 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
32
33 /**
34  * @author Tuukka Lehtonen
35  */
36 public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter {
37
38     private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
39
40     public OpenDiagramFromIssue() {
41         super("Open Diagram Containing Referenced Element", Activator.COMPOSITE_ICON);
42     }
43
44     protected String getEditorId() {
45         return EDITOR_ID;
46     }
47
48     @Override
49     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
50         return findElement(g, r) != null;
51     }
52
53     @Override
54     public void openEditor(final Resource r) throws Exception {
55         SimanticsUI.getSession().asyncRequest(new ReadRequest() {
56             @Override
57             public void run(ReadGraph g) throws DatabaseException {
58                 
59                 Layer0 L0 = Layer0.getInstance(g);
60                 ModelingResources MOD = ModelingResources.getInstance(g);
61                 
62                 Resource element = findElement(g, r);
63                 if(element == null) return;
64                 
65                 System.err.println("element " + g.getURI(element));
66                 
67                 Resource diagram = g.getSingleObject(element, L0.PartOf);
68                 Resource composite = g.getSingleObject(diagram, MOD.DiagramToComposite);
69                                 
70                 OpenDiagramFromConfigurationAdapter.openEditor(g, composite, getEditorId(), Collections.<Object>singleton(element));
71                 
72             }
73         });
74     }
75
76     private static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
77
78         Layer0 L0 = Layer0.getInstance(graph);
79         ModelingResources MOD = ModelingResources.getInstance(graph);
80
81         Resource diagram = graph.getPossibleObject(element, L0.PartOf);
82         if (diagram == null) return null;
83
84         return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
85         
86     }
87     
88     private static Resource findElement(ReadGraph graph, Resource issue) throws DatabaseException {
89         
90         IssueResource ISSUE = IssueResource.getInstance(graph);
91         DiagramResource DIA = DiagramResource.getInstance(graph);
92         
93         if(!graph.isInstanceOf(issue, ISSUE.Issue)) return null;
94         
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)) {
98                         return context;
99                 }
100                 
101         }
102         
103         return null;
104         
105     }
106
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);
116                 break;
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);
123             } else {
124                 selectedObjects.add(element);
125             }
126         }
127         for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) {
128                 selectedObjects.add(element);
129         }
130         return selectedObjects;
131     }    
132     
133 }