Try to improve shutdown hook

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1980 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2012-07-17 22:00:46 +00:00
parent 5c337081b8
commit e680c3e1ad
6 changed files with 64 additions and 43 deletions

View File

@ -173,6 +173,7 @@ public final class DavGateway {
// clear session cache
ExchangeSessionFactory.reset();
DavGatewayTray.info(new BundleMessage("LOG_GATEWAY_STOP"));
DavGatewayTray.dispose();
}
/**

View File

@ -37,6 +37,7 @@ import java.awt.event.ActionListener;
/**
* Tray icon handler based on java 1.6
*/
@SuppressWarnings("Since15")
public class AwtGatewayTray implements DavGatewayTrayInterface {
protected static final String TRAY_ACTIVE_PNG = "tray2.png";
protected static final String TRAY_PNG = "tray.png";
@ -163,6 +164,7 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
settingsFrame.reload();
settingsFrame.setVisible(true);
settingsFrame.toFront();
settingsFrame.repaint();
settingsFrame.requestFocus();
}
});
@ -179,6 +181,17 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
});
}
public void dispose() {
SystemTray.getSystemTray().remove(trayIcon);
// dispose frames
settingsFrame.dispose();
aboutFrame.dispose();
if (logBrokerMonitor != null) {
logBrokerMonitor.dispose();
}
}
protected void createAndShowGUI() {
System.setProperty("swing.defaultlaf", UIManager.getSystemLookAndFeelClassName());
@ -241,14 +254,6 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
public void actionPerformed(ActionEvent e) {
try {
DavGateway.stop();
SystemTray.getSystemTray().remove(trayIcon);
// dispose frames
settingsFrame.dispose();
aboutFrame.dispose();
if (logBrokerMonitor != null) {
logBrokerMonitor.dispose();
}
} catch (Exception exc) {
DavGatewayTray.error(exc);
}
@ -278,6 +283,7 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
if (Settings.isFirstStart()) {
settingsFrame.setVisible(true);
settingsFrame.toFront();
settingsFrame.repaint();
settingsFrame.requestFocus();
}
}

View File

@ -290,4 +290,7 @@ public final class DavGatewayTray {
return result;
}
public static void dispose() {
davGatewayTray.dispose();
}
}

View File

@ -68,4 +68,8 @@ public interface DavGatewayTrayInterface {
*/
void init();
/**
* destroy frames
*/
void dispose();
}

View File

@ -196,6 +196,15 @@ public class FrameGatewayTray implements DavGatewayTrayInterface {
});
}
public void dispose() {
// dispose frames
settingsFrame.dispose();
aboutFrame.dispose();
if (logBrokerMonitor != null) {
logBrokerMonitor.dispose();
}
}
protected void buildMenu() {
// create a popup menu
JMenu menu = new JMenu(BundleMessage.format("UI_DAVMAIL_GATEWAY"));
@ -239,12 +248,6 @@ public class FrameGatewayTray implements DavGatewayTrayInterface {
public void actionPerformed(ActionEvent e) {
try {
DavGateway.stop();
// dispose frames
settingsFrame.dispose();
aboutFrame.dispose();
if (logBrokerMonitor != null) {
logBrokerMonitor.dispose();
}
} catch (Exception exc) {
DavGatewayTray.error(exc);
}

View File

@ -312,7 +312,6 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
exitItem.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
DavGateway.stop();
shell.dispose();
}
});
@ -339,6 +338,32 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
}
}
dispose();
}
} catch (Exception exc) {
DavGatewayTray.error(exc);
}
// make sure we do exit
System.exit(0);
}
}.start();
while (true) {
// wait for SWT init
try {
synchronized (mainThread) {
if (isReady) {
break;
}
mainThread.wait(1000);
}
} catch (InterruptedException e) {
DavGatewayTray.error(new BundleMessage("LOG_ERROR_WAITING_FOR_SWT_INIT"), e);
}
}
}
public void dispose() {
shell.dispose();
if (trayItem != null) {
trayItem.dispose();
trayItem = null;
@ -368,26 +393,5 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
logBrokerMonitor.dispose();
}
}
} catch (Exception exc) {
DavGatewayTray.error(exc);
}
// make sure we do exit
System.exit(0);
}
}.start();
while (true) {
// wait for SWT init
try {
synchronized (mainThread) {
if (isReady) {
break;
}
mainThread.wait(1000);
}
} catch (InterruptedException e) {
DavGatewayTray.error(new BundleMessage("LOG_ERROR_WAITING_FOR_SWT_INIT"), e);
}
}
}
}