more printing experiments, loggin
authorBill Erickson <berick@esilibrary.com>
Mon, 24 Feb 2014 16:25:06 +0000 (11:25 -0500)
committerBill Erickson <berick@esilibrary.com>
Mon, 24 Feb 2014 16:25:06 +0000 (11:25 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
src/org/evergreen_ils/hatch/HatchServlet.java
src/org/evergreen_ils/hatch/PrintDriver.java

index da0d838..c6db310 100644 (file)
@@ -178,7 +178,7 @@ public class HatchServlet extends HttpServlet {
         switch(action) {
 
             case "print" : 
-                boolean ok = new PrintDriver().printWithDialog(value);
+                boolean ok = new PrintDriver().printWithDialog(key, value);
                 if (ok) {
                     response.setStatus(HttpServletResponse.SC_OK);
                 } else {
index f245c0a..9dc2d85 100644 (file)
@@ -1,4 +1,7 @@
 package org.evergreen_ils.hatch;
+
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
  
 import java.awt.*;
 import java.awt.event.*;
@@ -8,6 +11,7 @@ import java.awt.print.*;
 public class PrintDriver implements Printable {
 
     private String printText;
+    private static final Logger logger = Log.getLogger("PrintDriver");
 
     public int print(Graphics g, PageFormat pf, int page) 
         throws PrinterException {
@@ -30,7 +34,7 @@ public class PrintDriver implements Printable {
     /**
      * Spawns standard JAVA-driven print dialog and prints text
      */
-    public boolean printWithDialog(String text) {
+    public boolean printWithDialog(String key, String text) {
         printText = text;
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
@@ -38,17 +42,43 @@ public class PrintDriver implements Printable {
         try {
             job.print();
         } catch (PrinterException ex) {
-            // TODO
+            logger.warn("Error printing document for key " + key);
+            logger.warn(ex);
             return false;
         }
         return true;
     }
 
+    /**
+     * Print using defaults
+     *
+     * Sends the print job to the configured printer based on the key
+     * and user settings.
+     */
+    public boolean printWithoutDialog(String key, String text) {
+        printText = text;
+        PrinterJob job = PrinterJob.getPrinterJob();
+        job.setPrintable(this);
+
+        // TODO: load user settings, find the right printer, send the
+        // correct attributes, etc.
+
+        try {
+            job.print();
+        } catch (PrinterException ex) {
+            logger.warn("Error printing document for key " + key);
+            logger.warn(ex);
+            return false;
+        }
+        return true;
+    }
+
+
     // experiment
     // show our own print dialog before the real print action takes over
     // currently just shows Print and Cancel.
     // Not sure if there is a need for such a thing..
-    public void printWithCustomDialog(String msg) {
+    public void printWithCustomDialog(String key, String msg) {
         UIManager.put("swing.boldMetal", Boolean.FALSE);
         JFrame f = new JFrame("Hello World Printer");
 
@@ -56,11 +86,12 @@ public class PrintDriver implements Printable {
         f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 
         final String printMsg = msg;
+        final String printKey = key;
         JButton printButton = new JButton("Print '" + msg + "'");
         printButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
-                printWithDialog(printMsg);
+                printWithDialog(printKey, printMsg);
             }
         });