Saving settings and local database on application exit

Fixes #55
This commit is contained in:
Kai S. K. Engelbart 2019-12-20 15:05:31 +01:00
parent 16354ef392
commit e8f989902a
2 changed files with 15 additions and 20 deletions

View File

@ -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();

View File

@ -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);
}
}));
}
}