/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g3d.vtk.utils; import java.util.ArrayList; import java.util.List; import org.simantics.g3d.vtk.Activator; import org.simantics.g3d.vtk.preferences.CloseMethod; import org.simantics.g3d.vtk.preferences.PreferenceConstants; import org.simantics.utils.threads.AWTThread; import vtk.vtkCamera; import vtk.vtkObject; import vtk.vtkPanel; import vtk.vtkReferenceInformation; import vtk.vtkRenderWindow; import vtk.vtkRenderer; public class vtkPanelUtil { private static List activePanels = new ArrayList(); private static List waitingToDeletePanels = new ArrayList(); public static void registerPanel(vtkPanel panel) { activePanels.add(panel); } public static void unregisterPanel(vtkPanel panel) { assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); if (!activePanels.remove(panel)) return; CloseMethod method = getCloseMethod(); if (method == CloseMethod.ON_LAST_CLOSE) { waitingToDeletePanels.add(panel); if (activePanels.size() == 0) { cleanup(); } } else if (method == CloseMethod.ON_CLOSE) { dPanel(panel); vtkGC(); } else if (method == CloseMethod.NO_CLOSE) { waitingToDeletePanels.add(panel); } } public static CloseMethod getCloseMethod() { return CloseMethod.valueOf(Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.CLOSE_METHOD)); } private static void cleanup() { for (vtkPanel p : waitingToDeletePanels) { dPanel(p); } waitingToDeletePanels.clear(); vtkGC(); } private static void vtkGC() { vtkReferenceInformation info = vtkObject.JAVA_OBJECT_MANAGER.gc(true); System.out.println("Referenced objects when closing editor: " + info.getTotalNumberOfObjects() + "\n"); System.out.println(info.listRemovedReferenceToString()); System.out.println(info.listKeptReferenceToString()); } private static void dPanel(vtkPanel panel) { panel.lock(); vtkCamera camera = panel.GetRenderer().GetActiveCamera(); vtkRenderer ren = panel.GetRenderer(); vtkRenderWindow win = panel.GetRenderWindow(); win.SetForceMakeCurrent(); panel.Delete(); panel = null; camera.Delete(); ren.Delete(); win.Delete(); } }