GUI: fix memleaks 75/10075/4
Calixte DENIZET [Fri, 21 Dec 2012 13:45:56 +0000 (14:45 +0100)]
Change-Id: Ib101bed6297bdd318339c90c03bd60b18208468b

scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/tab/SwingScilabTab.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/window/SwingScilabWindow.java

index 356fba6..695ce78 100644 (file)
@@ -57,6 +57,7 @@ import javax.swing.KeyStroke;
 import javax.swing.SwingUtilities;
 
 import org.flexdock.docking.DockingConstants;
+import org.flexdock.docking.DockingManager;
 import org.flexdock.docking.DockingPort;
 import org.flexdock.docking.activation.ActiveDockableTracker;
 import org.flexdock.docking.event.DockingEvent;
@@ -76,10 +77,12 @@ import org.scilab.modules.gui.bridge.frame.SwingScilabFrame;
 import org.scilab.modules.gui.bridge.helpbrowser.SwingScilabHelpBrowser;
 import org.scilab.modules.gui.bridge.label.SwingScilabLabel;
 import org.scilab.modules.gui.bridge.listbox.SwingScilabListBox;
+import org.scilab.modules.gui.bridge.menubar.SwingScilabMenuBar;
 import org.scilab.modules.gui.bridge.popupmenu.SwingScilabPopupMenu;
 import org.scilab.modules.gui.bridge.pushbutton.SwingScilabPushButton;
 import org.scilab.modules.gui.bridge.radiobutton.SwingScilabRadioButton;
 import org.scilab.modules.gui.bridge.slider.SwingScilabSlider;
+import org.scilab.modules.gui.bridge.toolbar.SwingScilabToolBar;
 import org.scilab.modules.gui.bridge.tree.SwingScilabTree;
 import org.scilab.modules.gui.bridge.uidisplaytree.SwingScilabUiDisplayTree;
 import org.scilab.modules.gui.bridge.uiimage.SwingScilabUiImage;
@@ -1139,6 +1142,9 @@ public class SwingScilabTab extends View implements SwingViewObject, SimpleTab,
      */
     @Override
     public void setMenuBar(MenuBar newMenuBar) {
+        if (this.menuBar != null) {
+            ((SwingScilabMenuBar) this.menuBar.getAsSimpleMenuBar()).close();
+        }
         this.menuBar = newMenuBar;
     }
 
@@ -1160,6 +1166,9 @@ public class SwingScilabTab extends View implements SwingViewObject, SimpleTab,
      */
     @Override
     public void setToolBar(ToolBar newToolBar) {
+        if (this.toolBar != null) {
+            ((SwingScilabToolBar) this.toolBar.getAsSimpleToolBar()).close();
+        }
         this.toolBar = newToolBar;
     }
 
@@ -1325,10 +1334,11 @@ public class SwingScilabTab extends View implements SwingViewObject, SimpleTab,
         setInfoBar(null);
         setTitlebar(null);
         removeAll();
-        setActive(false);
+        //setActive(false);
 
         scrolling = null;
         contentPane = null;
+        DockingManager.unregisterDockable((Component) this);
 
         // without this children canvas are not released.
         Container dummyContainer = new Container();
@@ -1433,9 +1443,9 @@ public class SwingScilabTab extends View implements SwingViewObject, SimpleTab,
         for (int kChild = 0; kChild < children.length; kChild++) {
             int childType = (Integer) GraphicController.getController().getProperty(children[kChild], __GO_TYPE__);
             if (childType == __GO_UIMENU__
-                    || childType == __GO_UIPARENTMENU__
-                    || childType == __GO_UICHILDMENU__
-                    || childType == __GO_UICHECKEDMENU__) {
+                || childType == __GO_UIPARENTMENU__
+                || childType == __GO_UICHILDMENU__
+                || childType == __GO_UICHECKEDMENU__) {
                 String cb = (String) GraphicController.getController().getProperty(children[kChild], __GO_CALLBACK__);
                 SwingView.getFromId(children[kChild]).update(__GO_CALLBACK__, replaceFigureID(cb, parentFigureId));
                 String[] menuChildren = (String[]) GraphicController.getController().getProperty(children[kChild], __GO_CHILDREN__);
index 7a39d70..dcc66a7 100644 (file)
@@ -24,6 +24,7 @@ import org.flexdock.docking.defaults.DefaultDockingPort;
 import org.flexdock.docking.defaults.DefaultDockingStrategy;
 import org.flexdock.docking.drag.effects.EffectsManager;
 import org.flexdock.docking.drag.preview.GhostPreview;
+import org.flexdock.docking.event.hierarchy.DockingPortTracker;
 import org.scilab.modules.action_binding.InterpreterManagement;
 import org.scilab.modules.commons.gui.ScilabKeyStroke;
 import org.scilab.modules.gui.bridge.menubar.SwingScilabMenuBar;
@@ -108,11 +109,11 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
 
         // By default ctrl+w close the window
         ActionListener listener = new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                processWindowEvent(new WindowEvent(SwingScilabWindow.this, WindowEvent.WINDOW_CLOSING));
-            }
-        };
+                @Override
+                public void actionPerformed(ActionEvent e) {
+                    processWindowEvent(new WindowEvent(SwingScilabWindow.this, WindowEvent.WINDOW_CLOSING));
+                }
+            };
         getRootPane().registerKeyboardAction(listener, ScilabKeyStroke.getKeyStroke("OSSCKEY W"), JComponent.WHEN_IN_FOCUSED_WINDOW);
 
         // TODO : Only for testing : Must be removed
@@ -149,11 +150,11 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
         setLocationByPlatform(true);
 
         addWindowListener(new WindowAdapter() {
-            @Override
-            public void windowClosing(WindowEvent e) {
-                ClosingOperationsManager.startClosingOperation(SwingScilabWindow.this);
-            }
-        });
+                @Override
+                public void windowClosing(WindowEvent e) {
+                    ClosingOperationsManager.startClosingOperation(SwingScilabWindow.this);
+                }
+            });
 
         if (MAC_OS_X) {
             registerForMacOSXEvents();
@@ -267,11 +268,11 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
             /* javasci bug: See bug 9544 why we are doing this check */
             try {
                 SwingUtilities.invokeAndWait(new Runnable() {
-                    @Override
-                    public void run() {
-                        raiseToFront();
-                    }
-                });
+                        @Override
+                        public void run() {
+                            raiseToFront();
+                        }
+                    });
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } catch (InvocationTargetException e) {
@@ -399,7 +400,8 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
             tab.close();
             DockingManager.close(tab);
         }
-        if (getDockingPort().getDockables().isEmpty()) {
+
+        if (getDockingPort() == null || getDockingPort().getDockables().isEmpty()) {
             // remove xxxBars
             if (toolBar != null) {
                 ((SwingScilabToolBar) toolBar).close();
@@ -411,12 +413,6 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
             // clean all
             this.removeAll();
             close();
-
-            // disable docking port
-            ActiveDockableTracker.getTracker(this).setActive(null);
-            sciDockingPort.removeDockingListener(sciDockingListener);
-            sciDockingPort = null;
-            sciDockingListener = null;
         } else {
             /* Make sur a Tab is active */
             Set<SwingScilabTab> docks = sciDockingPort.getDockables();
@@ -525,7 +521,7 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
      * @param id the id of the corresponding window object
      */
     @Override
-    public void setElementId(int id) {
+    public void setElementId(int id) {System.out.println("Window="+id);
         this.elementId = id;
         //sciDockingListener.setAssociatedWindowId(id);
     }
@@ -538,6 +534,14 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
     public void close() {
         try {
             dispose();
+            // disable docking port
+            ActiveDockableTracker.getTracker(this).setActive(null);
+            if (sciDockingPort != null) {
+                sciDockingPort.removeDockingListener(sciDockingListener);
+                sciDockingPort = null;
+                sciDockingListener = null;
+            }
+            DockingPortTracker.remove(this);
         } catch (IllegalStateException e) {
             enableInputMethods(false);
         }