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