From 23d7d6b164e9a786999bbd8ca92bc8f66c15cf6d Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 20 Dec 2019 15:05:31 +0100 Subject: [PATCH] Saving settings and local database on application exit Fixes #55 --- src/main/java/envoy/client/ui/ChatWindow.java | 21 +------------------ src/main/java/envoy/client/ui/Startup.java | 14 +++++++++++++ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 8814195..e94c32b 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -8,9 +8,6 @@ import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; @@ -30,10 +27,9 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; -import envoy.client.util.EnvoyLog; import envoy.client.event.EventBus; import envoy.client.event.ThemeChangeEvent; - +import envoy.client.util.EnvoyLog; import envoy.schema.Message; import envoy.schema.User; @@ -80,21 +76,6 @@ public class ChatWindow extends JFrame { setLocationRelativeTo(null); setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("envoy_logo.png"))); - // Save chats when window closes - addWindowListener(new WindowAdapter() { - - @Override - public void windowClosing(WindowEvent evt) { - try { - localDB.save(); - Settings.getInstance().save(); - } catch (IOException e1) { - e1.printStackTrace(); - logger.log(Level.WARNING, "Unable to save the messages", e1); - } - } - }); - contentPane.setBorder(new EmptyBorder(space, space, space, space)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index b9f9633..d011bab 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -11,6 +11,7 @@ import javax.swing.JOptionPane; import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; +import envoy.client.Settings; import envoy.client.util.EnvoyLog; import envoy.exception.EnvoyException; import envoy.schema.User; @@ -126,5 +127,18 @@ public class Startup { e.printStackTrace(); } }); + + // Save Settings and LocalDB on shutdown + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + logger.info("Saving local database..."); + localDB.save(); + logger.info("Saving settings..."); + Settings.getInstance().save(); + } catch (IOException e1) { + e1.printStackTrace(); + logger.log(Level.WARNING, "Unable to save the messages", e1); + } + })); } } \ No newline at end of file