]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromIssue.java
Open diagram from issue
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / OpenDiagramFromIssue.java
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 import java.util.List;
18 import java.util.Set;
19
20 import org.simantics.Simantics;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.ReadRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.diagram.content.ConnectionUtil;
28 import org.simantics.diagram.flag.FlagUtil;
29 import org.simantics.diagram.stubs.DiagramResource;
30 import org.simantics.issues.ontology.IssueResource;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.modeling.ModelingResources;
33 import org.simantics.modeling.ui.Activator;
34 import org.simantics.structural.stubs.StructuralResource2;
35 import org.simantics.ui.selection.AnyResource;
36 import org.simantics.ui.selection.AnyVariable;
37 import org.simantics.ui.selection.WorkbenchSelectionContentType;
38 import org.simantics.ui.selection.WorkbenchSelectionElement;
39 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
40 import org.simantics.utils.datastructures.Pair;
41 import org.simantics.utils.ui.ISelectionUtils;
42
43 /**
44  * @author Tuukka Lehtonen
45  */
46 public class OpenDiagramFromIssue extends AbstractResourceEditorAdapter {
47
48     private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
49
50     public OpenDiagramFromIssue() {
51         super("Open Diagram Containing Referenced Component", Activator.COMPOSITE_ICON);
52     }
53
54     protected String getEditorId() {
55         return EDITOR_ID;
56     }
57
58     @Override
59     public boolean canHandle(ReadGraph g, Object input) throws DatabaseException {
60         return extractContext(g, input) != null;
61     }
62
63     @Override
64     public void openEditor(Object input) throws Exception {
65         Simantics.getSession().asyncRequest(new ReadRequest() {
66             @Override
67             public void run(ReadGraph g) throws DatabaseException {
68                 Pair<Resource, Collection<Object>> data = extractContext(g, input);
69                 if (data != null)
70                     OpenDiagramFromConfigurationAdapter.openEditor(g, data.first, getEditorId(), data.second);
71             }
72         });
73     }
74
75     private static Pair<Resource, Collection<Object>> extractContext(ReadGraph graph, Object input) throws DatabaseException {
76         Pair<Variable, Resource> p = extractInput(graph, input);
77         Pair<Resource, Collection<Object>> result = p.first != null ? findConfigurationAndObjects(graph, p.first) : null;
78         if (result == null && p.second != null)
79             result = findConfigurationAndObjects(graph, p.second);
80         return result;
81     }
82
83     protected static <T> T extractContent(ReadGraph graph, Object input, WorkbenchSelectionContentType<T> contentType, Class<T> contentClass) throws DatabaseException {
84         if (contentClass.isInstance(input))
85             return contentClass.cast(input);
86         if (input instanceof WorkbenchSelectionElement) {
87             WorkbenchSelectionElement single = (WorkbenchSelectionElement) input;
88             if (single != null)
89                 return single.getContent(contentType);
90         }
91         return ISelectionUtils.filterSingleSelection(input, contentClass);
92     }
93
94     protected static Pair<Variable, Resource> extractInput(ReadGraph graph, Object input) throws DatabaseException {
95         return Pair.make(
96                 extractContent(graph, input, new AnyVariable(graph), Variable.class),
97                 extractContent(graph, input, new AnyResource(graph), Resource.class));
98     }
99
100     protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, Resource issue) throws DatabaseException {
101         IssueResource ISSUE = IssueResource.getInstance(graph);
102         if (!graph.isInstanceOf(issue, ISSUE.Issue))
103             return null;
104         List<Resource> contexts = graph.getRelatedValue2(issue, ISSUE.Issue_contexts);
105         return contexts != null ? findConfigurationAndObjects(graph, contexts) : null;
106     }
107
108     protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, Variable v) throws DatabaseException {
109         List<Resource> contexts = v.getPossiblePropertyValue(graph, IssueResource.getInstance(graph).Issue_contexts);
110         return contexts != null ? findConfigurationAndObjects(graph, contexts) : null;
111     }
112
113     protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, List<Resource> contexts) throws DatabaseException {
114         Layer0 L0 = Layer0.getInstance(graph);
115         DiagramResource DIA = DiagramResource.getInstance(graph);
116         StructuralResource2 STR = StructuralResource2.getInstance(graph);
117         ModelingResources MOD = ModelingResources.getInstance(graph);
118
119         for(Resource context : contexts) {
120             Set<Resource> types = graph.getTypes(context);
121             if (types.contains(DIA.Element)) {
122                 Resource config = findConfigurationForElement(graph, context);
123                 if (config != null) {
124                     Collection<Object> elements = Collections.<Object>singleton(context);
125                     return Pair.make(config, elements);
126                 }
127             } else if (types.contains(STR.Component) && !types.contains(STR.Composite)) {
128                 Resource config = graph.getPossibleObject(context, L0.PartOf);
129                 if (config != null) {
130                     return Pair.make(config, findElementObjects(graph, context));
131                 }
132             } else if (types.contains(STR.Connection)) {
133                 Resource element = graph.getPossibleObject(context, MOD.ConnectionToDiagramConnection);
134                 if (element != null) {
135                     Resource config = findConfigurationForElement(graph, element);
136                     if (config != null) {
137                         return Pair.make(config, Collections.<Object>singleton(element));
138                     }
139                 }
140             }
141         }
142
143         return null;
144     }
145
146     protected static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
147         Layer0 L0 = Layer0.getInstance(graph);
148         ModelingResources MOD = ModelingResources.getInstance(graph);
149         Resource diagram = graph.getPossibleObject(element, L0.PartOf);
150         if (diagram == null) return null;
151         return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
152     }
153
154     protected static Collection<Object> findElementObjects(ReadGraph g, Resource component) throws DatabaseException {
155         Collection<Object> result = findElementObjects(g, component, "");
156         ModelingResources MOD = ModelingResources.getInstance(g);
157         for (Resource element : g.getObjects(component, MOD.HasParentComponent_Inverse))
158             result.add(element);
159         return result;
160     }
161     
162     public static Collection<Object> findElementObjects(ReadGraph g, Resource component, String rviFromComponent) throws DatabaseException {
163         Layer0 L0 = Layer0.getInstance(g);
164         DiagramResource DIA = DiagramResource.getInstance(g);
165         ModelingResources MOD = ModelingResources.getInstance(g);
166         final Collection<Object> selectedObjects = new ArrayList<>(4);
167         if (rviFromComponent.isEmpty()) {
168             // The selected objects are configuration objects
169             for (Resource element : g.getObjects(component, MOD.ComponentToElement)) {
170                 if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) {
171                     // Use external flag primarily if one exists in the correspondences
172                     selectedObjects.clear();
173                     selectedObjects.add(element);
174                     break;
175                 } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) {
176                     selectedObjects.add(element);
177                 } else if (g.isInstanceOf(element, DIA.Connection)) {
178                     // Okay, we need to find a part of the connection
179                     ConnectionUtil cu = new ConnectionUtil(g);
180                     cu.gatherConnectionParts(element, selectedObjects);
181                 } else {
182                     selectedObjects.add(element);
183                 }
184             }
185         } else {
186             // The selected objects are generated components
187             for (Resource refElement : g.getObjects(component, MOD.HasParentComponent_Inverse)) {
188                 Resource relation = g.getPossibleObject(refElement, MOD.HasReferenceRelation);
189                 if (relation != null) {
190                     String suffix = g.getPossibleRelatedValue(relation, L0.HasName, Bindings.STRING);
191                     if (suffix != null) {
192                         if (rviFromComponent.equals(suffix)) {
193                             selectedObjects.add(refElement);
194                         }
195                     }
196                 }
197             }
198         }
199         return selectedObjects;
200     }
201     
202 }