]> gerrit.simantics Code Review - simantics/platform.git/blob
4317f59799f9c8819bb1459fe76d3997ac56dc90
[simantics/platform.git] /
1 package org.simantics.modeling.ui.diagramEditor;\r
2 \r
3 import java.util.ArrayList;\r
4 \r
5 import org.eclipse.jface.viewers.ISelection;\r
6 import org.eclipse.jface.viewers.StructuredSelection;\r
7 import org.eclipse.ui.IWorkbenchPartSite;\r
8 import org.simantics.Simantics;\r
9 import org.simantics.browsing.ui.swt.AdaptableHintContext;\r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.common.request.ResourceRead;\r
13 import org.simantics.db.common.request.ResourceRead2;\r
14 import org.simantics.db.common.utils.RequestUtil;\r
15 import org.simantics.db.exception.DatabaseException;\r
16 import org.simantics.db.layer0.SelectionHints;\r
17 import org.simantics.db.layer0.request.PossibleModel;\r
18 import org.simantics.db.layer0.variable.Variable;\r
19 import org.simantics.db.layer0.variable.Variables;\r
20 import org.simantics.diagram.Logger;\r
21 import org.simantics.diagram.stubs.DiagramResource;\r
22 import org.simantics.diagram.ui.DiagramModelHints;\r
23 import org.simantics.diagram.ui.WorkbenchSelectionProvider;\r
24 import org.simantics.g2d.diagram.IDiagram;\r
25 import org.simantics.g2d.element.ElementHints;\r
26 import org.simantics.g2d.element.ElementUtils;\r
27 import org.simantics.g2d.element.IElement;\r
28 import org.simantics.modeling.ModelingResources;\r
29 import org.simantics.ui.SimanticsUI;\r
30 import org.simantics.ui.selection.AnyResource;\r
31 import org.simantics.ui.selection.AnyVariable;\r
32 import org.simantics.ui.selection.WorkbenchSelectionContentType;\r
33 import org.simantics.utils.DataContainer;\r
34 import org.simantics.utils.threads.IThreadWorkQueue;\r
35 import org.simantics.utils.ui.ErrorLogger;\r
36 \r
37 /**\r
38  * @author Antti Villberg\r
39  */\r
40 public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {\r
41 \r
42     protected static class SelectionElement extends AdaptableHintContext {\r
43 \r
44         final public Resource runtime;\r
45         final public Resource element;\r
46 \r
47         public SelectionElement(Key[] keys, Resource runtime, Resource element) {\r
48             super(keys);\r
49             this.runtime = runtime;\r
50             this.element = element;\r
51         }\r
52 \r
53         @SuppressWarnings("unchecked")\r
54         @Override\r
55         public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {\r
56                 if(contentType instanceof AnyResource) return (T)element;\r
57                 else if(contentType instanceof AnyVariable) {\r
58                 AnyVariable type = (AnyVariable)contentType;\r
59                 try {\r
60                     return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {\r
61                         @Override\r
62                         public Variable perform(ReadGraph graph) throws DatabaseException {\r
63 \r
64                             DiagramResource DIA = DiagramResource.getInstance(graph);\r
65                             ModelingResources MOD = ModelingResources.getInstance(graph);\r
66 \r
67                             String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);\r
68                             if (uri == null)\r
69                                 return null;\r
70 \r
71                             Variable var = Variables.getPossibleVariable(graph, uri);\r
72                             if (var == null)\r
73                                 return null;\r
74 \r
75                             Resource config = graph.getPossibleObject(resource2, MOD.ElementToComponent);\r
76                             if (config == null) {\r
77                                 // Apros #9646: if resource2 is the diagram\r
78                                 // itself, return the diagram composite variable\r
79                                 // since it is generally more useful than the\r
80                                 // variable to the diagram.\r
81                                 Resource composite = graph.getPossibleObject(resource2, MOD.DiagramToComposite);\r
82                                 if (composite != null && composite.equals(var.getPossibleRepresents(graph))) {\r
83                                     //return Variables.getPossibleVariable(graph, resource2);\r
84                                     return var;\r
85                                 }\r
86 \r
87                                 return null;\r
88                             }\r
89 \r
90                             return var.browsePossible(graph, config);\r
91 \r
92                         }\r
93                     });\r
94                 } catch (DatabaseException e) {\r
95                     Logger.defaultLogError(e);\r
96                 }\r
97             } \r
98             return null;\r
99         }\r
100 \r
101     }\r
102 \r
103     protected DataContainer<IDiagram> sourceDiagram;\r
104 \r
105     public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {\r
106         super(queue, site);\r
107         assert(sourceDiagram != null);\r
108         this.sourceDiagram = sourceDiagram;\r
109     }\r
110 \r
111         @Override\r
112         protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
113                 ArrayList<Object> objects = new ArrayList<Object>();\r
114                 IDiagram diagram = sourceDiagram.get();\r
115                 if (diagram != null) {\r
116                         Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
117                         Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
118                         for (Object o : selection) {\r
119                                 Resource resource = getSelectionResource(o);\r
120                                 if (resource != null) {\r
121                                         objects.add( constructSelectionElement(runtime, resource) );\r
122                                 } else {\r
123                                         System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);\r
124                                 }\r
125                         }\r
126                         if (objects.isEmpty() && runtime != null && dr != null) {\r
127                                 objects.add( constructSelectionElement(runtime, dr) );\r
128                         }\r
129                 }\r
130                 return new StructuredSelection(objects);\r
131         }\r
132 \r
133         protected Resource getSelectionResource(Object o) {\r
134                 if (o instanceof IElement) {\r
135                         IElement e = (IElement) o;\r
136                         Object object = e.getHint(ElementHints.KEY_OBJECT);\r
137                         if (object instanceof Resource)\r
138                                 return (Resource) object;\r
139                         object = ElementUtils.adaptElement(e, Resource.class);\r
140                         if (object != null)\r
141                                 return (Resource) object;\r
142                 }\r
143                 return null;\r
144         }\r
145 \r
146         /**\r
147          * @param runtime\r
148          * @param object\r
149          * @return\r
150          */\r
151         protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {\r
152                 SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);\r
153                 context.setHint(SelectionHints.KEY_MAIN, object);\r
154                 if (runtime != null) {\r
155                         context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);\r
156                         try {\r
157                                 Resource model = RequestUtil.trySyncRequest(\r
158                                                 Simantics.getSession(),\r
159                                                 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
160                                                 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
161                                                 null,\r
162                                                 new ModelOfRuntime(runtime));\r
163                                 if (model != null)\r
164                                         context.setHint(SelectionHints.KEY_MODEL, model);\r
165                         } catch (DatabaseException | InterruptedException ex) {\r
166                                 ErrorLogger.defaultLogError(ex);\r
167                         }\r
168                 }\r
169                 return context;\r
170         }\r
171 \r
172         private static class ModelOfRuntime extends ResourceRead<Resource> {\r
173                 public ModelOfRuntime(Resource runtime) {\r
174                         super(runtime);\r
175                 }\r
176 \r
177                 @Override\r
178                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
179                         DiagramResource DIA = DiagramResource.getInstance(graph);\r
180                         String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);\r
181                         if (uri != null) {\r
182                                 Resource resource = graph.getPossibleResource(uri);\r
183                                 if(resource != null) return resource;\r
184                         }\r
185                         Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);\r
186                         if (diagram == null)\r
187                                 return null;\r
188                         return graph.syncRequest(new PossibleModel(diagram));\r
189                 }\r
190         }\r
191 \r
192 }