From a8a0ac392359fd45660285d627471a3615c21f46 Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 10 Mar 2014 14:55:34 +0000 Subject: [PATCH] Refactor SWT tray dispose management on exit git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2242 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/ui/tray/SwtGatewayTray.java | 55 +++++++++----------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/java/davmail/ui/tray/SwtGatewayTray.java b/src/java/davmail/ui/tray/SwtGatewayTray.java index ac3899ed..15765506 100644 --- a/src/java/davmail/ui/tray/SwtGatewayTray.java +++ b/src/java/davmail/ui/tray/SwtGatewayTray.java @@ -29,6 +29,8 @@ import org.apache.log4j.lf5.LF5Appender; import org.apache.log4j.lf5.LogLevel; import org.apache.log4j.lf5.viewer.LogBrokerMonitor; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.internal.gtk.OS; import org.eclipse.swt.widgets.*; @@ -211,6 +213,19 @@ public class SwtGatewayTray implements DavGatewayTrayInterface { inactiveImage = loadSwtImage(AwtGatewayTray.TRAY_INACTIVE_PNG); trayItem.setImage(image); + trayItem.addDisposeListener(new DisposeListener() { + public void widgetDisposed(DisposeEvent e) { + if (image != null && !image.isDisposed()) { + image.dispose(); + } + if (image2 != null && !image2.isDisposed()) { + image2.dispose(); + } + if (inactiveImage != null && !inactiveImage.isDisposed()) { + inactiveImage.dispose(); + } + } + }); // create a popup menu final Menu popup = new Menu(shell, SWT.POP_UP); @@ -340,8 +355,16 @@ public class SwtGatewayTray implements DavGatewayTrayInterface { display.sleep(); } } - - dispose(); + } + // dispose AWT frames + if (settingsFrame != null) { + settingsFrame.dispose(); + } + if (aboutFrame != null) { + aboutFrame.dispose(); + } + if (logBrokerMonitor != null) { + logBrokerMonitor.dispose(); } } catch (Exception exc) { DavGatewayTray.error(exc); @@ -367,34 +390,6 @@ public class SwtGatewayTray implements DavGatewayTrayInterface { public void dispose() { shell.dispose(); - if (trayItem != null) { - trayItem.dispose(); - trayItem = null; - } - - if (image != null) { - image.dispose(); - } - if (image2 != null) { - image2.dispose(); - } - try { - if (!display.isDisposed()) { - display.dispose(); - } - } catch (Exception e) { - // already disposed - } - // dispose AWT frames - if (settingsFrame != null) { - settingsFrame.dispose(); - } - if (aboutFrame != null) { - aboutFrame.dispose(); - } - if (logBrokerMonitor != null) { - logBrokerMonitor.dispose(); - } } }