Browse Source

Moved to the SystemTray library

frans 3 years ago
parent
commit
e2acb1dbc6
3 changed files with 26 additions and 30 deletions
  1. 2 2
      README.md
  2. 6 0
      pom.xml
  3. 18 28
      src/main/java/eu/tankernn/grid/GridControl.java

+ 2 - 2
README.md

@@ -14,13 +14,13 @@ A Java-based, open-source alternative to the default control software for the NZ
 ## Credit
 - This project was originally a fork of [RoelGo/CamSucks](https://github.com/RoelGo/CamSucks)
 - The reverse engineering of the GRID+ communication was done by rizvanrp, their site is no longer available but here is a screenshot of their article on the GRID+. http://research.domaintools.com/research/screenshot-history/rizvanrp.com/#0
-- Some of the serial command codes were taken from [akej74/grid-control](https://github.com/akej74/grid-control)
+- Some of the serial command codes and GUI ideas were taken from [akej74/grid-control](https://github.com/akej74/grid-control).
 - The sensor data on windows systems is read with the help of the jWMI class made by Henry Ranch @ http://henryranch.net
 - On windows systems, this class communicates with an external program called openhardwaremonitor @ http://openhardwaremonitor.org/
+- This project uses [SystemTray](https://github.com/dorkbox/SystemTray).
 
 ##TODO
 - Make it possible to set weights for different temperature readings.
-- Move to [SystemTray](https://github.com/dorkbox/SystemTray)
 - Unit tests
 - GUI Improvements
   - More menu items (start minimized, start on boot, etc.)

+ 6 - 0
pom.xml

@@ -25,6 +25,12 @@
 			<artifactId>commons-lang3</artifactId>
 			<version>3.0</version>
 		</dependency>
+		<dependency>
+			<groupId>com.dorkbox</groupId>
+			<artifactId>SystemTray</artifactId>
+			<version>3.1</version>
+		</dependency>
+
 	</dependencies>
 
 	<build>

+ 18 - 28
src/main/java/eu/tankernn/grid/GridControl.java

@@ -1,11 +1,6 @@
 package eu.tankernn.grid;
 
-import java.awt.AWTException;
 import java.awt.Image;
-import java.awt.MenuItem;
-import java.awt.PopupMenu;
-import java.awt.SystemTray;
-import java.awt.TrayIcon;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.FileWriter;
@@ -16,12 +11,15 @@ import java.util.Arrays;
 
 import javax.imageio.ImageIO;
 import javax.swing.JFrame;
+import javax.swing.JSeparator;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 
+import dorkbox.systemTray.MenuItem;
+import dorkbox.systemTray.SystemTray;
 import eu.tankernn.grid.frame.GridControlPanel;
 import eu.tankernn.grid.model.ComputerModel;
 
@@ -37,7 +35,6 @@ public class GridControl implements Runnable {
 	private ComputerModel model = new ComputerModel();
 
 	private GridControlPanel frame;
-	private TrayIcon trayIcon;
 
 	public GridControl(boolean gui) {
 		readSettings();
@@ -63,32 +60,26 @@ public class GridControl implements Runnable {
 	}
 
 	private void addTrayIcon(Image image) {
-		// Check the SystemTray is supported
-		if (!SystemTray.isSupported()) {
-			System.out.println("SystemTray is not supported");
-			return;
+		SystemTray systemTray = SystemTray.get();
+		if (systemTray == null) {
+			throw new RuntimeException("Unable to load SystemTray!");
 		}
-		final PopupMenu popup = new PopupMenu();
 
-		// Create a pop-up menu components
-		MenuItem openItem = new MenuItem("Open");
-		openItem.addActionListener(a -> frame.setVisible(true));
-		MenuItem exitItem = new MenuItem("Exit");
-		exitItem.addActionListener(a -> this.exit());
+		systemTray.setImage(image);
 
-		// Add components to pop-up menu
-		popup.add(openItem);
-		popup.addSeparator();
-		popup.add(exitItem);
+		systemTray.setStatus("Not Running");
 
-		trayIcon = new TrayIcon(image, "JavaGridControl", popup);
-		trayIcon.setImageAutoSize(true);
+		systemTray.getMenu().add(new MenuItem("Show", a -> {
+			frame.setVisible(true);
+		}));
+		
+		systemTray.getMenu().add(new JSeparator());
+
+		systemTray.getMenu().add(new MenuItem("Quit", a -> {
+			systemTray.shutdown();
+			exit();
+		})).setShortcut('q'); // case does not matter
 
-		try {
-			SystemTray.getSystemTray().add(trayIcon);
-		} catch (AWTException e) {
-			System.out.println("TrayIcon could not be added.");
-		}
 	}
 
 	public void readSettings() {
@@ -178,7 +169,6 @@ public class GridControl implements Runnable {
 		model.getGrid().disconnect();
 		saveSettings();
 		frame.dispose();
-		SystemTray.getSystemTray().remove(trayIcon);
 		for (Thread t : Thread.getAllStackTraces().keySet())
 			if (t.isAlive())
 				System.out.println(t);