Merge pull request #73 from informatik-ag-ngl/b/fast_startup

Loading ChatWindow in parallel to Client and LocalDB
This commit is contained in:
Kai S. K. Engelbart 2019-12-21 21:07:33 +01:00 committed by GitHub
commit 73167370b6
2 changed files with 45 additions and 18 deletions

View File

@ -54,14 +54,9 @@ public class ChatWindow extends JFrame {
* Initializes a {@link JFrame} with UI elements used to send and read messages * Initializes a {@link JFrame} with UI elements used to send and read messages
* to different users. * to different users.
* *
* @param client the {@link Client} used to send and receive messages
* @param localDB the {@link LocalDB} used to manage stored messages and users
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public ChatWindow(Client client, LocalDB localDB) { public ChatWindow() {
this.client = client;
this.localDB = localDB;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800); setBounds(100, 100, 600, 800);
setTitle("Envoy"); setTitle("Envoy");
@ -164,7 +159,7 @@ public class ChatWindow extends JFrame {
userList.setCellRenderer(new UserListRenderer()); userList.setCellRenderer(new UserListRenderer());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userList.addListSelectionListener((listSelectionEvent) -> { userList.addListSelectionListener((listSelectionEvent) -> {
if (!listSelectionEvent.getValueIsAdjusting()) { if (client != null && localDB != null && !listSelectionEvent.getValueIsAdjusting()) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource(); final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
final User user = selectedUserList.getSelectedValue(); final User user = selectedUserList.getSelectedValue();
@ -193,16 +188,12 @@ public class ChatWindow extends JFrame {
gbc_userList.anchor = GridBagConstraints.PAGE_START; gbc_userList.anchor = GridBagConstraints.PAGE_START;
gbc_userList.insets = new Insets(space, space, space, space); gbc_userList.insets = new Insets(space, space, space, space);
changeChatWindowColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
contentPane.add(userList, gbc_userList); contentPane.add(userList, gbc_userList);
contentPane.revalidate(); contentPane.revalidate();
EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> changeChatWindowColors((Theme) evt.get())); EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme((Theme) evt.get()));
loadUsersAndChats();
if (client.isOnline()) startSyncThread(Config.getInstance().getSyncTimeout());
contentPane.revalidate(); contentPane.revalidate();
} }
@ -211,9 +202,9 @@ public class ChatWindow extends JFrame {
* Used to immediately reload the ChatWindow when settings were changed. * Used to immediately reload the ChatWindow when settings were changed.
* *
* @param theme the theme to change colors into * @param theme the theme to change colors into
* @since Envoy v0.1-alpha * @since Envoy v0.2-alpha
*/ */
private void changeChatWindowColors(Theme theme) { private void applyTheme(Theme theme) {
// contentPane // contentPane
contentPane.setBackground(theme.getBackgroundColor()); contentPane.setBackground(theme.getBackgroundColor());
contentPane.setForeground(theme.getUserNameColor()); contentPane.setForeground(theme.getUserNameColor());
@ -334,4 +325,29 @@ public class ChatWindow extends JFrame {
* Marks messages in the current chat as {@code READ}. * Marks messages in the current chat as {@code READ}.
*/ */
private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } } private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } }
/**
* Sets the {@link Client} used by this {@link ChatWindow}. If the client is
* online, the sync thread is started.
*
* @param client the {@link Client} used to send and receive messages
* @since Envoy v0.2-alpha
*/
public void setClient(Client client) {
this.client = client;
if (client.isOnline() && localDB != null) startSyncThread(Config.getInstance().getSyncTimeout());
}
/**
* Sets the {@link LocalDB} used by this {@link ChatWindow}. After invoking this
* method, users and chats will be loaded from the database into the GUI.
*
* @param localDB the {@link LocalDB} used to manage stored messages and users
* @since Envoy v0.2-alpha
*/
public void setLocalDB(LocalDB localDB) {
this.localDB = localDB;
loadUsersAndChats();
if (client != null && client.isOnline()) startSyncThread(Config.getInstance().getSyncTimeout());
}
} }

View File

@ -8,6 +8,7 @@ import java.util.logging.Logger;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import envoy.client.*; import envoy.client.*;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
@ -15,7 +16,7 @@ import envoy.exception.EnvoyException;
import envoy.schema.User; import envoy.schema.User;
/** /**
* Starts the Envoy client and prompts the user to enter their name. * Starts the Envoy client and prompts the user to enter their name.<br>
* <br> * <br>
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>Startup.java</strong><br> * File: <strong>Startup.java</strong><br>
@ -28,6 +29,8 @@ import envoy.schema.User;
*/ */
public class Startup { public class Startup {
private static ChatWindow chatWindow;
private static final Logger logger = EnvoyLog.getLogger(Startup.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(Startup.class.getSimpleName());
/** /**
@ -44,6 +47,8 @@ public class Startup {
public static void main(String[] args) { public static void main(String[] args) {
Config config = Config.getInstance(); Config config = Config.getInstance();
SwingUtilities.invokeLater(() -> chatWindow = new ChatWindow());
try { try {
// Load the configuration from client.properties first // Load the configuration from client.properties first
config.load(); config.load();
@ -82,6 +87,8 @@ public class Startup {
return; return;
} }
SwingUtilities.invokeLater(() -> chatWindow.setVisible(true));
// Acquire the client user (with ID) either from the server or from the local // Acquire the client user (with ID) either from the server or from the local
// database, which triggers offline mode // database, which triggers offline mode
Client client = new Client(config); Client client = new Client(config);
@ -96,6 +103,10 @@ public class Startup {
User clientUser = localDB.getUsers().get(userName); User clientUser = localDB.getUsers().get(userName);
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser); client.setSender(clientUser);
JOptionPane.showMessageDialog(null,
"A connection to the server could not be established. Starting in offline mode.",
"Connection error",
JOptionPane.WARNING_MESSAGE);
} catch (Exception e2) { } catch (Exception e2) {
JOptionPane.showMessageDialog(null, e2.toString(), "Client error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, e2.toString(), "Client error", JOptionPane.ERROR_MESSAGE);
System.exit(1); System.exit(1);
@ -125,8 +136,8 @@ public class Startup {
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {
try { try {
ChatWindow chatWindow = new ChatWindow(client, localDB); chatWindow.setClient(client);
chatWindow.setVisible(true); chatWindow.setLocalDB(localDB);
try { try {
new StatusTrayIcon(chatWindow).show(); new StatusTrayIcon(chatWindow).show();