From fbe2d0d0b0e082d04673cefe042532ec0a2e883b Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 3 Dec 2019 21:48:16 +0100 Subject: [PATCH] Moved system tray logic to StatusTrayIcon class --- src/main/java/envoy/client/ui/ChatWindow.java | 37 +++-------------- .../java/envoy/client/ui/StatusTrayIcon.java | 39 ++++++++++++++++++ .../{Envoy Logo.png => envoy_logo.png} | Bin 3 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 src/main/java/envoy/client/ui/StatusTrayIcon.java rename src/main/resources/{Envoy Logo.png => envoy_logo.png} (100%) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 0eeccc0..7a5c67c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -1,17 +1,11 @@ package envoy.client.ui; -import java.awt.AWTException; import java.awt.Color; import java.awt.ComponentOrientation; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; -import java.awt.Image; import java.awt.Insets; -import java.awt.SystemTray; -import java.awt.Toolkit; -import java.awt.TrayIcon; -import java.awt.TrayIcon.MessageType; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; @@ -128,8 +122,8 @@ public class ChatWindow extends JFrame { @Override public void keyReleased(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_ENTER && ((SettingsScreen.enterToSend && e.getModifiersEx() == 0) - || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { + if (e.getKeyCode() == KeyEvent.VK_ENTER + && ((SettingsScreen.enterToSend && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); } @@ -220,11 +214,7 @@ public class ChatWindow extends JFrame { final User user = selectedUserList.getSelectedValue(); client.setRecipient(user); - currentChat = localDB.getChats() - .stream() - .filter(chat -> chat.getRecipient().getID() == user.getID()) - .findFirst() - .get(); + currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get(); // Set all unread messages in the chat to read readCurrentChat(); @@ -263,10 +253,7 @@ public class ChatWindow extends JFrame { private void postMessage(JList messageList) { if (!client.hasRecipient()) { - JOptionPane.showMessageDialog(this, - "Please select a recipient!", - "Cannot send message", - JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); } if (!messageEnterTextArea.getText().isEmpty()) try { @@ -322,8 +309,7 @@ public class ChatWindow extends JFrame { new Thread(() -> { // Synchronize - localDB.applySync( - client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID()))); + localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID()))); // Process unread messages localDB.addUnreadMessagesToLocalDB(); @@ -333,8 +319,7 @@ public class ChatWindow extends JFrame { readCurrentChat(); // Update UI - SwingUtilities - .invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); + SwingUtilities.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); }).start(); }).start(); } @@ -350,14 +335,4 @@ public class ChatWindow extends JFrame { * Marks messages in the current chat as {@code READ}. */ private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } } - - private void displayNotification(String message) throws AWTException { - SystemTray tray = SystemTray.getSystemTray(); - Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("Envoy Logo.png")); - TrayIcon trayIcon = new TrayIcon(image, "Envoy Client"); - trayIcon.setImageAutoSize(true); - trayIcon.setToolTip("You are notified if you have unread messages."); - tray.add(trayIcon); - trayIcon.displayMessage("Envoy Client", message, MessageType.INFO); - } } diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java new file mode 100644 index 0000000..54bc3a3 --- /dev/null +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -0,0 +1,39 @@ +package envoy.client.ui; + +import java.awt.AWTException; +import java.awt.Image; +import java.awt.SystemTray; +import java.awt.Toolkit; +import java.awt.TrayIcon; + +import envoy.exception.EnvoyException; + +/** + * Project: envoy-client
+ * File: StatusTrayIcon.java
+ * Created: 3 Dec 2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy v0.2-alpha + */ +public class StatusTrayIcon { + + private TrayIcon trayIcon; + + public StatusTrayIcon() throws EnvoyException { + if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported."); + + Image img = Toolkit.getDefaultToolkit().createImage(getClass().getResource("envoy_logo.png")); + TrayIcon trayIcon = new TrayIcon(img, "Envoy Client"); + trayIcon.setImageAutoSize(true); + trayIcon.setToolTip("You are notified if you have unread messages."); + try { + SystemTray.getSystemTray().add(trayIcon); + } catch (AWTException e) { + throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e); + } + } + + // TODO: Add event listener + // trayIcon.displayMessage("Envoy Client", message, MessageType.INFO); +} diff --git a/src/main/resources/Envoy Logo.png b/src/main/resources/envoy_logo.png similarity index 100% rename from src/main/resources/Envoy Logo.png rename to src/main/resources/envoy_logo.png