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