Browse Source

[ADD] tray features

Gogs 7 years ago
parent
commit
36b6ba1b51

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@
 .git
 .project
 .settings
+/target/

+ 10 - 4
pom.xml

@@ -39,11 +39,17 @@
 	    <artifactId>commons-io</artifactId>
 	    <version>2.5</version>
 	</dependency>
-	<!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
+	   <!-- https://mvnrepository.com/artifact/com.dorkbox/SystemTray -->
 	<dependency>
-	    <groupId>org.codehaus.jettison</groupId>
-	    <artifactId>jettison</artifactId>
-	    <version>1.3.8</version>
+	    <groupId>com.dorkbox</groupId>
+	    <artifactId>SystemTray</artifactId>
+	    <version>3.11</version>
+	</dependency>
+	<!-- https://mvnrepository.com/artifact/org.json/json -->
+	<dependency>
+	    <groupId>org.json</groupId>
+	    <artifactId>json</artifactId>
+	    <version>20171018</version>
 	</dependency>
   </dependencies>
 </project>

+ 19 - 10
src/main/java/org/robert/printer/App.java

@@ -1,5 +1,8 @@
 package org.robert.printer;
 
+import javax.swing.SwingUtilities;
+
+import org.robert.printer.ui.Tray;
 import org.robert.printer.ws.PrintersSocketServer;
 
 /**
@@ -7,21 +10,27 @@ import org.robert.printer.ws.PrintersSocketServer;
  *
  */
 public class App {
+
+	private static Tray tray;
+
 	/**
 	 *
 	 * @param args
 	 */
 	public static void main(String[] args) {
-		new Runnable() {
-			public void run() {
-				PrintersSocketServer server = new PrintersSocketServer();
-
-				try {
-					server.goUp();
-				} catch (Exception e) {
-					e.printStackTrace();
+		try {
+			SwingUtilities.invokeAndWait(new Runnable() {
+				@Override
+				public void run() {
+					tray = new Tray();
+					tray.start();
 				}
-			}
-		};
+			});
+
+			tray.setupServer(new PrintersSocketServer());
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 	}
 }

+ 2 - 3
src/main/java/org/robert/printer/common/PrintersDiscovery.java

@@ -3,8 +3,7 @@ package org.robert.printer.common;
 import javax.print.PrintService;
 import javax.print.PrintServiceLookup;
 
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;
+import org.json.JSONObject;
 
 public abstract class PrintersDiscovery {
 
@@ -13,7 +12,7 @@ public abstract class PrintersDiscovery {
 	 * @return
 	 * @throws JSONException
 	 */
-	public static JSONObject getPrinterNames() throws JSONException {
+	public static JSONObject getPrinterNames() {
 		JSONObject json = new JSONObject();
 		PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
 

+ 96 - 0
src/main/java/org/robert/printer/ui/Tray.java

@@ -0,0 +1,96 @@
+package org.robert.printer.ui;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import org.robert.printer.ws.PrintersSocketServer;
+
+import dorkbox.systemTray.MenuItem;
+import dorkbox.systemTray.SystemTray;
+
+public class Tray {
+
+	private SystemTray systemTray;
+	private PrintersSocketServer server;
+
+	/**
+	 *
+	 */
+	public void start() {
+		this.systemTray = SystemTray.get();
+
+		if (this.systemTray == null) {
+			return;
+		}
+
+		this.systemTray.setImage(Tray.class.getResource("./resources/printer_ok.png"));
+
+		MenuItem startServerMenu = new MenuItem("Arrancar servidor", new ActionListener() {
+			public void actionPerformed(ActionEvent e) {
+				startServerActionListener(e);
+			}
+		});
+
+		MenuItem stopServerMenu = new MenuItem("Parar servidor", new ActionListener() {
+			public void actionPerformed(ActionEvent e) {
+				stopServerActionListener(e);
+			}
+		});
+
+		MenuItem quitMenu = new MenuItem("Salir", new ActionListener() {
+			public void actionPerformed(ActionEvent e) {
+				quitActionListener(e);
+			}
+		});
+
+		this.systemTray.getMenu().add(startServerMenu);
+		this.systemTray.getMenu().add(stopServerMenu);
+		this.systemTray.getMenu().add(quitMenu);
+	}
+
+	/**
+	 *
+	 * @param e
+	 */
+	protected void startServerActionListener(ActionEvent e) {
+		try {
+			this.server.goUp();
+		} catch (Exception e1) {
+			e1.printStackTrace();
+		}
+	}
+
+	/**
+	 *
+	 * @param e
+	 */
+	protected void stopServerActionListener(ActionEvent e) {
+		try {
+			this.server.goDown();
+		} catch (Exception e1) {
+			e1.printStackTrace();
+		}
+	}
+
+	/**
+	 *
+	 * @param e
+	 */
+	protected void quitActionListener(ActionEvent e) {
+		this.systemTray.shutdown();
+	}
+
+	/**
+	 *
+	 * @param server
+	 * @throws Exception
+	 */
+	public void setupServer(PrintersSocketServer server) throws Exception {
+		if (server == null) {
+			return;
+		}
+
+		this.server = server;
+		this.server.goUp();
+	}
+}

BIN
src/main/java/org/robert/printer/ui/resources/printer_ok.png


+ 4 - 5
src/main/java/org/robert/printer/ws/PrintersSocketHandler.java

@@ -5,14 +5,14 @@ import java.io.Reader;
 import java.util.HashMap;
 
 import org.apache.commons.io.IOUtils;
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;
+
 import org.eclipse.jetty.websocket.api.Session;
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
 import org.eclipse.jetty.websocket.api.annotations.WebSocket;
+import org.json.JSONObject;
 import org.robert.printer.common.PrintersDiscovery;
 
 @WebSocket
@@ -60,7 +60,7 @@ public class PrintersSocketHandler {
 	}
 
 	@OnWebSocketMessage
-	public void onMessage(Session session, Reader reader) throws IOException, JSONException {
+	public void onMessage(Session session, Reader reader) throws IOException {
 		String message = IOUtils.toString(reader);
 
 		if (message == null || message.isEmpty()) {
@@ -79,10 +79,9 @@ public class PrintersSocketHandler {
 	 * @throws JSONException
 	 *
 	 */
-	private void processMessage(Session session, JSONObject json, SocketConnection connection) throws JSONException {
+	private void processMessage(Session session, JSONObject json, SocketConnection connection) {
 		String action = json.optString("action");
 
-
 		if (action.equals("list")) {
 			JSONObject printerNames = PrintersDiscovery.getPrinterNames();
 			this.send(session, printerNames);

+ 2 - 1
src/main/java/org/robert/printer/ws/PrintersSocketServer.java

@@ -67,7 +67,7 @@ public class PrintersSocketServer {
 
 		ServerConnector connector = new ServerConnector(this.server, httpConnectionFactory);
 		connector.setHost(this.getHost());
-		connector.setPort(this.getPort());
+		connector.setPort(this.getPort() + 1);
 
 		this.server.addConnector(connector);
 
@@ -92,6 +92,7 @@ public class PrintersSocketServer {
 	 *
 	 */
 	public void goDown() throws Exception {
+		this.getServer().getServer().stop();
 		this.getServer().getHandler().destroy();
 
 		for (Connector connector : this.getServer().getConnectors()) {

BIN
target/classes/org/robert/printer/App$1.class


BIN
target/classes/org/robert/printer/App.class


BIN
target/classes/org/robert/printer/common/PrintersDiscovery.class


BIN
target/classes/org/robert/printer/common/PrintingActions.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketHandler.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketServer$1.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketServer.class


BIN
target/classes/org/robert/printer/ws/SocketConnection.class


BIN
target/test-classes/org/robert/printer/AppTest.class