added 'printers' action to list all printers w/ properties
authorBill Erickson <berick@esilibrary.com>
Wed, 26 Feb 2014 14:16:44 +0000 (09:16 -0500)
committerBill Erickson <berick@esilibrary.com>
Wed, 26 Feb 2014 14:16:44 +0000 (09:16 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
src/org/evergreen_ils/hatch/HatchServlet.java
src/org/evergreen_ils/hatch/PrintDriver.java

index c6db310..2fa4f40 100644 (file)
@@ -11,6 +11,7 @@ import org.eclipse.jetty.util.ajax.JSON;
 import org.eclipse.jetty.util.log.Log;
 import org.eclipse.jetty.util.log.Logger;
 import java.util.Arrays;
+import java.util.List;
 
 public class HatchServlet extends HttpServlet {
 
@@ -127,6 +128,14 @@ public class HatchServlet extends HttpServlet {
             return;
         }
 
+        if (action.equals("printers")) {
+            List printers = new PrintDriver().getPrinters();
+            response.setStatus(HttpServletResponse.SC_OK);
+            response.getWriter().println(JSON.toString(printers));
+            return;
+        }
+
+
         // all remaining requests require a key
         if (key == null || key.equals("")) {
             String err = JSON.toString("No key specified in request");
index 316814b..5247e38 100644 (file)
@@ -21,6 +21,11 @@ import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.text.AttributedString;
 
+import java.util.Map;
+import java.util.List;
+import java.util.HashMap;
+import java.util.LinkedList;
+
 public class PrintDriver implements Printable {
 
     private String printText;
@@ -148,6 +153,33 @@ public class PrintDriver implements Printable {
         }
     }
 
+    public List<HashMap> getPrinters() {
+
+        List<HashMap> printers = new LinkedList<HashMap>();
+        PrintService[] printServices = 
+            PrintServiceLookup.lookupPrintServices(null, null);
+
+        String defaultPrinter = "";
+        PrintService def = PrintServiceLookup.lookupDefaultPrintService();
+        if (def != null) defaultPrinter = def.getName();
+
+        for (PrintService service : printServices) {
+            HashMap<String, Object> printer = new HashMap<String, Object>();
+            printers.add(printer);
+
+            if (service.getName().equals(defaultPrinter)) 
+                printer.put("is-default", new Boolean(true));
+
+            AttributeSet attributes = service.getAttributes();
+            for (Attribute a : attributes.toArray()) {
+                String name = a.getName();
+                String value = attributes.get(a.getClass()).toString();
+                printer.put(name, value);
+            }
+        }
+
+        return printers;
+    }
 
     // experiment
     // show our own print dialog before the real print action takes over