Moved system tray logic to StatusTrayIcon class
This commit is contained in:
		| @@ -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<Message> 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); | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										39
									
								
								src/main/java/envoy/client/ui/StatusTrayIcon.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/main/java/envoy/client/ui/StatusTrayIcon.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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: <strong>envoy-client</strong><br> | ||||
|  * File: <strong>StatusTrayIcon.java</strong><br> | ||||
|  * Created: <strong>3 Dec 2019</strong><br> | ||||
|  * | ||||
|  * @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); | ||||
| } | ||||
| Before Width: | Height: | Size: 8.0 KiB After Width: | Height: | Size: 8.0 KiB | 
		Reference in New Issue
	
	Block a user