]> gerrit.simantics Code Review - simantics/platform.git/blob
c3070d9cceb9f60ccd3fde3aef1e7407a14b9957
[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 \r
66                             String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);\r
67                             if (uri == null)\r
68                                 return null;\r
69 \r
70                             Variable var = Variables.getPossibleVariable(graph, uri);\r
71                             if (var == null)\r
72                                 return null;\r
73 \r
74                             Resource config = graph.getPossibleObject(resource2, ModelingResources.getInstance(graph).ElementToComponent);\r
75                             if (config == null)\r
76                                 return null;\r
77 \r
78                             return var.browsePossible(graph, config);\r
79 \r
80                         }\r
81                     });\r
82                 } catch (DatabaseException e) {\r
83                     Logger.defaultLogError(e);\r
84                 }\r
85             } \r
86             return null;\r
87         }\r
88 \r
89     }\r
90 \r
91     protected DataContainer<IDiagram> sourceDiagram;\r
92 \r
93     public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {\r
94         super(queue, site);\r
95         assert(sourceDiagram != null);\r
96         this.sourceDiagram = sourceDiagram;\r
97     }\r
98 \r
99         @Override\r
100         protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
101                 ArrayList<Object> objects = new ArrayList<Object>();\r
102                 IDiagram diagram = sourceDiagram.get();\r
103                 if (diagram != null) {\r
104                         Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
105                         Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
106                         for (Object o : selection) {\r
107                                 Resource resource = getSelectionResource(o);\r
108                                 if (resource != null) {\r
109                                         objects.add( constructSelectionElement(runtime, resource) );\r
110                                 } else {\r
111                                         System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);\r
112                                 }\r
113                         }\r
114                         if (objects.isEmpty() && runtime != null && dr != null) {\r
115                                 objects.add( constructSelectionElement(runtime, dr) );\r
116                         }\r
117                 }\r
118                 return new StructuredSelection(objects);\r
119         }\r
120 \r
121         protected Resource getSelectionResource(Object o) {\r
122                 if (o instanceof IElement) {\r
123                         IElement e = (IElement) o;\r
124                         Object object = e.getHint(ElementHints.KEY_OBJECT);\r
125                         if (object instanceof Resource)\r
126                                 return (Resource) object;\r
127                         object = ElementUtils.adaptElement(e, Resource.class);\r
128                         if (object != null)\r
129                                 return (Resource) object;\r
130                 }\r
131                 return null;\r
132         }\r
133 \r
134         /**\r
135          * @param runtime\r
136          * @param object\r
137          * @return\r
138          */\r
139         protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {\r
140                 SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);\r
141                 context.setHint(SelectionHints.KEY_MAIN, object);\r
142                 if (runtime != null) {\r
143                         context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);\r
144                         try {\r
145                                 Resource model = RequestUtil.trySyncRequest(\r
146                                                 Simantics.getSession(),\r
147                                                 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
148                                                 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
149                                                 null,\r
150                                                 new ModelOfRuntime(runtime));\r
151                                 if (model != null)\r
152                                         context.setHint(SelectionHints.KEY_MODEL, model);\r
153                         } catch (DatabaseException | InterruptedException ex) {\r
154                                 ErrorLogger.defaultLogError(ex);\r
155                         }\r
156                 }\r
157                 return context;\r
158         }\r
159 \r
160         private static class ModelOfRuntime extends ResourceRead<Resource> {\r
161                 public ModelOfRuntime(Resource runtime) {\r
162                         super(runtime);\r
163                 }\r
164 \r
165                 @Override\r
166                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
167                         DiagramResource DIA = DiagramResource.getInstance(graph);\r
168                         String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);\r
169                         if (uri != null) {\r
170                                 Resource resource = graph.getPossibleResource(uri);\r
171                                 if(resource != null) return resource;\r
172                         }\r
173                         Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);\r
174                         if (diagram == null)\r
175                                 return null;\r
176                         return graph.syncRequest(new PossibleModel(diagram));\r
177                 }\r
178         }\r
179 \r
180 }