]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sg/DiagramSceneGraphProvider.java
Support selection of experiment run for scene graph provider
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sg / DiagramSceneGraphProvider.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.sg;
13
14 import java.awt.Color;
15 import java.awt.event.AWTEventListener;
16
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.simantics.Simantics;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Session;
23 import org.simantics.db.common.ResourceArray;
24 import org.simantics.db.common.primitiverequest.PossibleAdapter;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
27 import org.simantics.db.exception.NoSingleResultException;
28 import org.simantics.db.exception.ServiceException;
29 import org.simantics.db.layer0.variable.Variable;
30 import org.simantics.db.management.ISessionContext;
31 import org.simantics.db.request.Read;
32 import org.simantics.diagram.adapter.DefaultConnectionClassFactory;
33 import org.simantics.diagram.adapter.FlagClassFactory;
34 import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;
35 import org.simantics.diagram.handler.CopyPasteHandler;
36 import org.simantics.diagram.handler.CopyPasteStrategy;
37 import org.simantics.diagram.handler.DefaultCopyPasteStrategy;
38 import org.simantics.diagram.handler.DeleteHandler;
39 import org.simantics.diagram.handler.SimpleElementTransformHandler;
40 import org.simantics.diagram.query.DiagramRequests;
41 import org.simantics.diagram.runtime.RuntimeDiagramManager;
42 import org.simantics.diagram.stubs.DiagramResource;
43 import org.simantics.diagram.synchronization.CopyAdvisor;
44 import org.simantics.diagram.synchronization.SynchronizationHints;
45 import org.simantics.diagram.ui.DiagramModelHints;
46 import org.simantics.g2d.canvas.Hints;
47 import org.simantics.g2d.canvas.ICanvasContext;
48 import org.simantics.g2d.canvas.impl.CanvasContext;
49 import org.simantics.g2d.diagram.DiagramHints;
50 import org.simantics.g2d.diagram.IDiagram;
51 import org.simantics.g2d.diagram.participant.DiagramParticipant;
52 import org.simantics.g2d.diagram.participant.ElementInteractor;
53 import org.simantics.g2d.diagram.participant.ElementPainter;
54 import org.simantics.g2d.diagram.participant.Selection;
55 import org.simantics.g2d.diagram.participant.ZOrderHandler;
56 import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;
57 import org.simantics.g2d.element.ElementClassProviders;
58 import org.simantics.g2d.element.ElementClasses;
59 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
60 import org.simantics.g2d.multileveldiagram.TransitionFunction;
61 import org.simantics.g2d.multileveldiagram.ZoomTransitionParticipant;
62 import org.simantics.g2d.page.DiagramDesc;
63 import org.simantics.g2d.participant.BackgroundPainter;
64 import org.simantics.g2d.participant.CanvasBoundsParticipant;
65 import org.simantics.g2d.participant.CanvasGrab;
66 import org.simantics.g2d.participant.GridPainter;
67 import org.simantics.g2d.participant.KeyToCommand;
68 import org.simantics.g2d.participant.KeyUtil;
69 import org.simantics.g2d.participant.MouseUtil;
70 import org.simantics.g2d.participant.MultitouchPanZoomRotateInteractor;
71 import org.simantics.g2d.participant.Notifications;
72 import org.simantics.g2d.participant.PageBorderParticipant;
73 import org.simantics.g2d.participant.PanZoomRotateHandler;
74 import org.simantics.g2d.participant.PointerPainter;
75 import org.simantics.g2d.participant.RulerPainter;
76 import org.simantics.g2d.participant.SymbolUtil;
77 import org.simantics.g2d.participant.TimeParticipant;
78 import org.simantics.g2d.participant.TransformUtil;
79 import org.simantics.g2d.participant.ZoomToAreaHandler;
80 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
81 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider2;
82 import org.simantics.modeling.ModelingResources;
83 import org.simantics.modeling.mapping.ComponentCopyAdvisor;
84 import org.simantics.modeling.mapping.ElementCopyAdvisor;
85 import org.simantics.modeling.mapping.MappedElementCopyAdvisor;
86 import org.simantics.modeling.mapping.ModelingSynchronizationHints;
87 import org.simantics.modeling.ui.diagramEditor.PopulateElementDropParticipant;
88 import org.simantics.modeling.ui.diagramEditor.PopulateElementMonitorDropParticipant;
89 import org.simantics.modeling.ui.diagramEditor.handlers.HeadlessStructuralBrowsingHandler;
90 import org.simantics.modeling.ui.diagramEditor.handlers.HeadlessStructuralBrowsingHandler.IDiagramUpdateSupport;
91 import org.simantics.scenegraph.g2d.G2DSceneGraph;
92 import org.simantics.scenegraph.g2d.events.adapter.AWTRemoteEventAdapter;
93 import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;
94 import org.simantics.structural.stubs.StructuralResource2;
95 import org.simantics.structural2.modelingRules.IModelingRules;
96 import org.simantics.ui.SimanticsUI;
97 import org.simantics.utils.datastructures.hints.HintContext;
98 import org.simantics.utils.datastructures.hints.IHintContext;
99 import org.simantics.utils.page.PageDesc;
100 import org.simantics.utils.page.PageOrientation;
101 import org.simantics.utils.threads.AWTThread;
102 import org.simantics.utils.threads.IThreadWorkQueue;
103 import org.simantics.utils.ui.ErrorLogger;
104
105
106 /**
107  * ISceneGraphProvider implementation for diagrams.
108  * TODO: decide if this should be stateless or stateful class
109  * 
110  * @author J-P Laine
111  *
112  */
113 public class DiagramSceneGraphProvider implements ICanvasSceneGraphProvider2, IDiagramUpdateSupport {
114
115         protected boolean isSymbol = false;
116     protected Resource resource;
117     protected ResourceArray structuralPath = null;
118     protected IDiagram diagram = null;
119     protected CanvasContext ctx = null;
120     protected boolean ownsContext = false;
121     protected GraphToDiagramSynchronizer synchronizer = null;
122     protected String view = null;
123     protected Boolean navigation = null;
124
125     protected AWTRemoteEventAdapter listener = null;
126
127     protected final IHintContext initialHints = new HintContext();
128
129     public DiagramSceneGraphProvider(ReadGraph g, final Resource diagramOrComposite) {
130         this.structuralPath = new ResourceArray();
131         try {
132
133             StructuralResource2 sr = StructuralResource2.getInstance(g);
134                 DiagramResource DIA = DiagramResource.getInstance(g);
135                 
136                 Resource diagram = diagramOrComposite;
137                 if(!g.isInstanceOf(diagram, DIA.Composite)) {
138                         
139                 ModelingResources mr = ModelingResources.getInstance(g);
140                         diagram = g.getPossibleObject(diagramOrComposite, mr.CompositeToDiagram);
141                         if(diagram == null) {
142                     // looks like we have a component without direct relation to composite.. try to solve this
143                     // FIXME: use adapter to get composite directly from resource
144                     final Resource type = g.getSingleType(diagramOrComposite, sr.Component);
145                     if (type == null)
146                         return;
147
148                     final Resource definedBy = g.getPossibleObject(type, sr.IsDefinedBy);
149                     if (definedBy == null)
150                         return;
151
152                     this.structuralPath = new ResourceArray(diagramOrComposite);
153                     diagram =  g.getPossibleObject(definedBy, mr.CompositeToDiagram);
154                         }
155                         
156                 } else {
157                         
158                         Resource possibleSymbol = g.getPossibleObject(diagram, sr.Defines);
159                         if(possibleSymbol != null && g.isInstanceOf(possibleSymbol, DIA.ElementClass)) isSymbol = true;
160                         
161                 }
162                 
163                         this.resource = diagram;
164                 
165         } catch (ManyObjectsForFunctionalRelationException e) {
166             // TODO Auto-generated catch block
167             e.printStackTrace();
168         } catch (ServiceException e) {
169             // TODO Auto-generated catch block
170             e.printStackTrace();
171         } catch (NoSingleResultException e) {
172             // TODO Auto-generated catch block
173             e.printStackTrace();
174         }
175     }
176
177     public GraphToDiagramSynchronizer getGraphToDiagramSynchronizer() {
178         return synchronizer;
179     }
180
181     protected CopyPasteStrategy getCopyPasteStrategy() {
182                 try {
183                         CopyPasteStrategy cpStrategy = Simantics.getSession().syncRequest(new PossibleAdapter<CopyPasteStrategy>(resource, CopyPasteStrategy.class));
184                         if(cpStrategy != null) return cpStrategy;
185                 } catch (DatabaseException e) {
186                 }
187                 return new DefaultCopyPasteStrategy();
188     }
189     
190     protected CopyAdvisor getCopyAdvisor() {
191                 try {
192                         CopyAdvisor advisor = Simantics.getSession().syncRequest(new PossibleAdapter<CopyAdvisor>(resource, CopyAdvisor.class));
193                         if(advisor != null) return advisor;
194                 } catch (DatabaseException e) {
195                 }
196                 return new MappedElementCopyAdvisor(new ElementCopyAdvisor(), new ComponentCopyAdvisor());
197     }
198     
199     private void initContext(CanvasContext ctx) {
200         boolean unlock = false;
201         if (!ctx.isLocked()) {
202             ctx.setLocked(true);
203             unlock = true;
204         }
205
206         IHintContext h = ctx.getDefaultHintContext();
207
208         // Support & Util Participants
209         ctx.add( new TransformUtil() );
210         ctx.add( new MouseUtil() );
211         ctx.add( new KeyUtil() );
212         ctx.add( new CanvasGrab() );
213         ctx.add( new SymbolUtil() );
214         ctx.add( new TimeParticipant() );
215         ctx.add( new CanvasBoundsParticipant() );
216         ctx.add( new Notifications() );
217
218         // SGFocusParticipant does not work for remote viewer at the moment.
219         //ctx.add( new SGFocusParticipant() );
220
221         h.setHint(PointerPainter.KEY_PAINT_POINTER, true);
222
223         ctx.add( new PanZoomRotateHandler(navigation == null || navigation == true) );
224         ctx.add( new MultitouchPanZoomRotateInteractor() );
225         ctx.add( new ZoomToAreaHandler() );
226         ctx.add( new SimpleElementTransformHandler() );
227         
228         // Key bindings
229         if(navigation == null || navigation == true)
230             ctx.add( new KeyToCommand( CommandKeyBinding.DEFAULT_BINDINGS ) );
231
232         // Grid & Ruler & Background
233         h.setHint(RulerPainter.KEY_RULER_ENABLED, false);
234         h.setHint(GridPainter.KEY_GRID_ENABLED, false);
235
236         ctx.add( new GridPainter() );
237         ctx.add( new RulerPainter() );
238         ctx.add( new BackgroundPainter() );
239         
240         h.setHint(Hints.KEY_DISPLAY_PAGE, Boolean.FALSE);
241         h.setHint(Hints.KEY_DISPLAY_MARGINS, Boolean.TRUE);
242         ctx.add( new PageBorderParticipant() );
243
244         h.setHint(Hints.KEY_GRID_COLOR, new Color(0.95f, 0.95f, 0.95f));
245         h.setHint(Hints.KEY_BACKGROUND_COLOR, Color.WHITE);
246         h.setHint(RulerPainter.KEY_RULER_BACKGROUND_COLOR, new Color(0.9f, 0.9f, 0.9f, 0.75f));
247         h.setHint(RulerPainter.KEY_RULER_TEXT_COLOR, Color.BLACK);
248
249         ////// Diagram Participants //////
250         ctx.add( new PointerInteractor(false, false, false, false, true, true, synchronizer.getElementClassProvider(), null) );
251         ctx.add( new ElementInteractor() );
252         ctx.add( new Selection() );
253         ctx.add( new DiagramParticipant() );
254         ctx.add( new ElementPainter(true) );
255
256         //ctx.add( new ElementHeartbeater() );
257         ctx.add( new ZOrderHandler() );
258         ctx.add( new ZoomTransitionParticipant(TransitionFunction.SIGMOID) );
259
260         /////// D'n'D ///////
261         ctx.add(new PopulateElementDropParticipant(synchronizer));
262         ctx.add(new PopulateElementMonitorDropParticipant(synchronizer, 0.5, 0.5));
263
264         h.setHint(Hints.KEY_TOOL, Hints.POINTERTOOL);
265
266         h.setHint(PanZoomRotateHandler.KEY_ZOOM_IN_LIMIT, 1000.0);
267         h.setHint(PanZoomRotateHandler.KEY_ZOOM_OUT_LIMIT, 10.0);
268
269         // Add visual browsing capabilities for structural models
270         ISessionContext sessionContext = Simantics.getSessionContext();
271         ctx.add(new HeadlessStructuralBrowsingHandler(this, sessionContext, structuralPath));
272
273         // Page settings
274         PageDesc pageDesc = PageDesc.DEFAULT.withOrientation(PageOrientation.Landscape);
275         // TODO: load page desc from graph
276         h.setHint(Hints.KEY_PAGE_DESC, pageDesc);
277
278         ctx.add(new CopyPasteHandler(getCopyPasteStrategy()));
279         ctx.add(new DeleteHandler(null));
280
281         if (resource != null)
282             loadPageSettings(ctx, resource);
283
284         ctx.assertParticipantDependencies();
285         if (unlock)
286             ctx.setLocked(false);
287     }
288
289     protected void loadPageSettings(ICanvasContext ctx, Resource diagramResource) {
290         try {
291             DiagramDesc diagramDesc = Simantics.getSession().syncRequest(DiagramRequests.getDiagramDesc(diagramResource));
292             if (diagramDesc != null)
293                 setDiagramDesc(ctx, diagramDesc);
294         } catch (DatabaseException e) {
295             ErrorLogger.defaultLogError(e);
296         }
297     }
298
299     protected void setDiagramDesc(ICanvasContext ctx, DiagramDesc diagramDesc) {
300         IHintContext hints = ctx.getDefaultHintContext();
301         hints.setHint(Hints.KEY_PAGE_DESC, diagramDesc.getPageDesc());
302         //hints.setHint(Hints.KEY_DISPLAY_PAGE, diagramDesc.isPageBordersVisible());
303         hints.setHint(Hints.KEY_DISPLAY_MARGINS, diagramDesc.isMarginsVisible());
304     }
305
306     @Override
307     public G2DSceneGraph initializeSceneGraph(G2DSceneGraph sg) {
308         return initializeSceneGraph(sg, null);
309     }
310
311     @Override
312     public G2DSceneGraph initializeSceneGraph(G2DSceneGraph sg, String view) {
313         return initializeSceneGraph(sg, "", "", view);
314     }
315
316     @Override
317     public G2DSceneGraph initializeSceneGraph(G2DSceneGraph sg, String modelURI, String RVI) {
318         return initializeSceneGraph(sg, modelURI, RVI, "");
319     }
320
321     @Override
322     public G2DSceneGraph initializeSceneGraph(ICanvasContext context, String modelURI, String RVI) {
323         G2DSceneGraph sg = context.getSceneGraph();
324         return initializeSceneGraph(context, sg, modelURI, RVI, "", null);
325     }
326     
327     
328     @Override
329     public G2DSceneGraph initializeSceneGraph(ICanvasContext context, String modelURI, String RVI,
330             Variable experiment) {
331         G2DSceneGraph sg = context.getSceneGraph();
332         return initializeSceneGraph(context, sg, modelURI, RVI, "", experiment);
333     }
334
335
336     /**
337      * @param sg
338      * @param view
339      * @param initialHints
340      * @return
341      */
342     private G2DSceneGraph initializeSceneGraph(G2DSceneGraph sg, String modelURI, String RVI, String view) {
343         IThreadWorkQueue thread = AWTThread.getThreadAccess();
344         ctx = new CanvasContext(thread, sg); // By giving the scene graph instance as parameter, we can use external serializer
345         return initializeSceneGraph(ctx, sg, modelURI, RVI, view, null);
346     }
347     
348     protected GraphToDiagramSynchronizer createSynchronizer(ReadGraph graph) throws DatabaseException {
349         DiagramResource DIA = DiagramResource.getInstance(graph);
350         return new GraphToDiagramSynchronizer(graph, ctx,
351                 ElementClassProviders.mappedProvider(
352                         ElementClasses.CONNECTION, DefaultConnectionClassFactory.CLASS.newClassWith(new StaticObjectAdapter(DIA.RouteGraphConnection)),
353                         ElementClasses.FLAG, FlagClassFactory.createFlagClass(DIA.Flag, DIA.Flag_Terminal)
354                 )
355         );
356     }
357     
358     protected RuntimeDiagramManager createRuntimeDiagramManager(Session session, Resource resource, String modelURI, String RVI, Variable experiment) throws DatabaseException {
359         return RuntimeDiagramManager.create(session, resource, modelURI, RVI);
360     }
361
362     /**
363      * @param sg
364      * @param view
365      * @param initialHints
366      * @return
367      */
368     private G2DSceneGraph initializeSceneGraph(ICanvasContext context, G2DSceneGraph sg, String modelURI, String RVI, String view, Variable run) {
369         this.ctx = (CanvasContext) context;
370         this.view = view;
371         if(view != null) {
372             //System.out.println("using layer '"+view+"'");
373             initialHints.setHint(DiagramHints.KEY_FIXED_LAYERS, new String[] { view });
374         }
375
376         try {
377
378             IModelingRules modelingRules = Simantics.getSession().syncRequest(DiagramRequests.getModelingRules(resource, null));
379             if (modelingRules != null) {
380                 initialHints.setHint(DiagramModelHints.KEY_MODELING_RULES, modelingRules);
381             }
382             
383             initialHints.setHint(SynchronizationHints.COPY_ADVISOR, getCopyAdvisor());
384             
385             final RuntimeDiagramManager runtimeDiagramManager = createRuntimeDiagramManager(Simantics.getSession(), resource, modelURI, RVI, run);
386             
387             synchronizer = Simantics.getSession().syncRequest(new Read<GraphToDiagramSynchronizer>() {
388                 @Override
389                 public GraphToDiagramSynchronizer perform(ReadGraph graph) throws DatabaseException {
390                     DiagramResource dr = DiagramResource.getInstance(graph);
391                     Boolean val = graph.getPossibleRelatedValue(resource, dr.NavigationEnabled, Bindings.BOOLEAN);
392                     if(val != null && navigation == null) { // Set only if navigation has not been set manually
393                         navigation = val;
394                     }
395                     GraphToDiagramSynchronizer sync = createSynchronizer(graph);
396                     sync.set(ModelingSynchronizationHints.MODELING_RESOURCE, ModelingResources.getInstance(graph));
397                     diagram = sync.loadDiagram(new NullProgressMonitor(), graph, null, resource, runtimeDiagramManager.getRuntimeDiagram(), structuralPath, initialHints); // FIXME
398                     return sync;
399                 }
400             });
401             
402         } catch (DatabaseException e) {
403             // TODO Auto-generated catch block
404             e.printStackTrace();
405         }
406         ctx.getDefaultHintContext().setHint(DiagramHints.KEY_DIAGRAM, diagram);
407
408         initContext(ctx);
409
410         return ctx.getSceneGraph();
411     }
412
413     @Override
414     public void dispose() {
415         if(ctx != null) {
416             if (ownsContext)
417                 ctx.dispose();
418             ctx = null;
419         }
420         if (synchronizer != null) {
421             synchronizer.dispose();
422             synchronizer = null;
423         }
424         if (diagram != null) {
425             diagram.dispose();
426             diagram = null;
427         }
428     }
429
430     /**
431      * Hack for returning canvasContext for some special cases
432      * this method brakes the interface, but this is not intended to be used in production context
433      * @return
434      */
435     @Override
436     public ICanvasContext getCanvasContext() {
437         return ctx;
438     }
439
440     @Override
441     public AWTEventListener getEventListener() {
442         if(ctx == null) return null;
443         if(listener == null) {
444             listener = new AWTRemoteEventAdapter(ctx, ctx.getEventQueue());
445         }
446         return listener;
447     }
448
449     @Override
450     public void updateDiagram(final ResourceArray structuralPath) {
451         final IHintContext hints = new HintContext();
452         if(view != null) {
453             System.out.println("using layer '"+view+"'");
454             hints.setHint(DiagramHints.KEY_FIXED_LAYERS, new String[] { view });
455         }
456
457         try {
458             // FIXME: I have no idea if this works or not..
459             diagram = Simantics.getSession().syncRequest(new Read<IDiagram>() {
460                 @Override
461                 public IDiagram perform(ReadGraph graph) throws DatabaseException {
462                     IDiagram d = synchronizer.loadDiagram(new NullProgressMonitor(), graph, null, structuralPath.resources[0], null, structuralPath.removeFromBeginning(0), hints);
463                     return d;
464                 }
465             });
466             ctx.getDefaultHintContext().setHint(DiagramHints.KEY_DIAGRAM, diagram);
467         } catch (DatabaseException e) {
468             // TODO Auto-generated catch block
469             e.printStackTrace();
470         }
471     }
472
473     public void setExperiment(ReadGraph g, String identifier) {
474         if(diagram == null) return;
475         diagram.setHint(DiagramModelHints.KEY_ACTIVE_EXPERIMENT, identifier);
476         initialHints.setHint(DiagramModelHints.KEY_ACTIVE_EXPERIMENT, identifier);
477
478 //              try {
479 //                      DiagramExperiment.setActiveExperiment(g, diagram, (String)identifier);
480 //              } catch (DatabaseException e) {
481 //                      // TODO Auto-generated catch block
482 //                      e.printStackTrace();
483 //              }
484
485     }
486
487     /**
488      * Supported keys:
489      *     DiagramModelHints.KEY_SESSION_ID
490      *     DiagramHints.KEY_NAVIGATION_ENABLED
491      */
492     @Override
493     public void setHint(Object key, Object value) {
494         if(DiagramModelHints.KEY_SESSION_ID.equals(key)) {
495             if(diagram != null)
496                 diagram.setHint(DiagramModelHints.KEY_SESSION_ID, value);
497             initialHints.setHint(DiagramModelHints.KEY_SESSION_ID, value);
498         } else if(DiagramModelHints.KEY_ACTIVE_EXPERIMENT.equals(key)) {
499             if(diagram != null)
500                 diagram.setHint(DiagramModelHints.KEY_ACTIVE_EXPERIMENT, value);
501             initialHints.setHint(DiagramModelHints.KEY_ACTIVE_EXPERIMENT, value);
502         } else if(DiagramHints.KEY_NAVIGATION_ENABLED.equals(key)) {
503             navigation = (Boolean)value;
504         }
505     }
506 }