]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Clear Context/CanvasContext on dispose() 44/4644/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 5 Feb 2021 11:57:45 +0000 (13:57 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 5 Feb 2021 11:57:45 +0000 (13:57 +0200)
Changed PageDesc DB listening to static class to clear references to
main object after dispose.

gitlab #674

Change-Id: Ib5659dee7495ba4310cbf3ac459231160c328c2f

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sg/DiagramSceneGraphProvider.java
bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/context/Context.java

index e08984e7f2baa7490c3ec58a56266317519bb314..ef3197522ff158081466be7f3bfd9daea4418d18 100644 (file)
@@ -288,36 +288,56 @@ public class DiagramSceneGraphProvider implements ICanvasSceneGraphProvider, IDi
         if (unlock)
             ctx.setLocked(false);
     }
+    
+    public static class DiagramDescListener implements Listener<DiagramDesc> {
+       ICanvasContext ctx;
+       public DiagramDescListener(ICanvasContext ctx) {
+                       this.ctx = ctx;
+               }
+        @Override
+         public void execute(DiagramDesc result) {
+             if (result != null && ctx != null) {
+                 ThreadUtils.asyncExec(ctx.getThreadAccess(), () -> {
+                     if (ctx != null) {
+                         setDiagramDesc(ctx, result);
+                     }
+                 });
+             }
+         }
+
+         @Override
+         public void exception(Throwable t) {
+             ErrorLogger.defaultLogError(t);
+         }
+
+         @Override
+         public boolean isDisposed() {
+             return ctx == null;
+         }
+         
+         protected void setDiagramDesc(ICanvasContext ctx, DiagramDesc diagramDesc) {
+             IHintContext hints = ctx.getDefaultHintContext();
+             hints.setHint(Hints.KEY_PAGE_DESC, diagramDesc.getPageDesc());
+             hints.setHint(Hints.KEY_DISPLAY_PAGE, diagramDesc.isPageBordersVisible());
+             hints.setHint(Hints.KEY_DISPLAY_MARGINS, diagramDesc.isMarginsVisible());
+         }
+         
+         public void dispose() {
+                ctx = null;
+         }
+    }
 
+    DiagramDescListener diagramDescListener;
+    
     protected void loadPageSettings(ICanvasContext ctx, Resource diagramResource) {
         try {
             DiagramDesc diagramDesc = Simantics.getSession().syncRequest(DiagramRequests.getDiagramDesc(diagramResource));
             if (diagramDesc != null)
                 setDiagramDesc(ctx, diagramDesc);
 
+            diagramDescListener = new DiagramDescListener(ctx);
             // Create a listener to react to page setting changes.
-            Simantics.getSession().asyncRequest(DiagramRequests.getDiagramDesc(diagramResource), new Listener<DiagramDesc>() {
-                @Override
-                public void execute(DiagramDesc result) {
-                    if (result != null && ctx != null) {
-                        ThreadUtils.asyncExec(ctx.getThreadAccess(), () -> {
-                            if (ctx != null) {
-                                setDiagramDesc(ctx, result);
-                            }
-                        });
-                    }
-                }
-
-                @Override
-                public void exception(Throwable t) {
-                    ErrorLogger.defaultLogError(t);
-                }
-
-                @Override
-                public boolean isDisposed() {
-                    return DiagramSceneGraphProvider.this.ctx == null;
-                }
-            });
+            Simantics.getSession().asyncRequest(DiagramRequests.getDiagramDesc(diagramResource), diagramDescListener);
         } catch (DatabaseException e) {
             ErrorLogger.defaultLogError(e);
         }
@@ -359,7 +379,7 @@ public class DiagramSceneGraphProvider implements ICanvasSceneGraphProvider, IDi
      */
     private G2DSceneGraph initializeSceneGraph(G2DSceneGraph sg, String modelURI, String RVI, String view) {
         IThreadWorkQueue thread = AWTThread.getThreadAccess();
-        ctx = new CanvasContext(thread, sg); // By giving the scene graph instance as parameter, we can use external serializer
+        ICanvasContext ctx = new CanvasContext(thread, sg); // By giving the scene graph instance as parameter, we can use external serializer
         return initializeSceneGraph(ctx, sg, modelURI, RVI, view);
     }
 
@@ -370,7 +390,9 @@ public class DiagramSceneGraphProvider implements ICanvasSceneGraphProvider, IDi
      * @return
      */
     private G2DSceneGraph initializeSceneGraph(ICanvasContext context, G2DSceneGraph sg, String modelURI, String RVI, String view) {
-        this.ctx = (CanvasContext) context;
+        if (this.ctx != null)
+               throw new RuntimeException("Cannot initialize scenegraph twice");
+       this.ctx = (CanvasContext) context;
         this.view = view;
         if(view != null) {
             //System.out.println("using layer '"+view+"'");
@@ -436,6 +458,10 @@ public class DiagramSceneGraphProvider implements ICanvasSceneGraphProvider, IDi
 
     @Override
     public void dispose() {
+       if (diagramDescListener != null) {
+               diagramDescListener.dispose();
+               diagramDescListener = null;
+       }
         if(ctx != null) {
             if (ownsContext)
                 ctx.dispose();
index 7b5465992cbcf4957f19664fea68b79c0cab1e71..e4878e4e71c4532af9a69cf99e7cebb3beb0c02f 100644 (file)
@@ -240,6 +240,7 @@ public class Context<E> extends AbstractDisposable implements IContext<E> {
        
        @Override
        protected void doDispose() {
+               clear();
        }
 
        @Override