From: Marko Luukkainen Date: Fri, 5 Feb 2021 11:57:45 +0000 (+0200) Subject: Clear Context/CanvasContext on dispose() X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F44%2F4644%2F1;p=simantics%2Fplatform.git Clear Context/CanvasContext on dispose() Changed PageDesc DB listening to static class to clear references to main object after dispose. gitlab #674 Change-Id: Ib5659dee7495ba4310cbf3ac459231160c328c2f --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sg/DiagramSceneGraphProvider.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sg/DiagramSceneGraphProvider.java index e08984e7f..ef3197522 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sg/DiagramSceneGraphProvider.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sg/DiagramSceneGraphProvider.java @@ -288,36 +288,56 @@ public class DiagramSceneGraphProvider implements ICanvasSceneGraphProvider, IDi if (unlock) ctx.setLocked(false); } + + public static class DiagramDescListener implements Listener { + 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() { - @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(); diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/context/Context.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/context/Context.java index 7b5465992..e4878e4e7 100644 --- a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/context/Context.java +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/context/Context.java @@ -240,6 +240,7 @@ public class Context extends AbstractDisposable implements IContext { @Override protected void doDispose() { + clear(); } @Override