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