Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / examples / SimpleDocExample.java
diff --git a/jspi/src/main/java/de/lohndirekt/print/examples/SimpleDocExample.java b/jspi/src/main/java/de/lohndirekt/print/examples/SimpleDocExample.java
new file mode 100644 (file)
index 0000000..0e37c32
--- /dev/null
@@ -0,0 +1,165 @@
+/**\r
+ * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ * \r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *  \r
+ */\r
+package de.lohndirekt.print.examples;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.URISyntaxException;\r
+import java.util.Locale;\r
+\r
+import javax.print.CancelablePrintJob;\r
+import javax.print.Doc;\r
+import javax.print.DocFlavor;\r
+import javax.print.PrintException;\r
+import javax.print.PrintService;\r
+import javax.print.SimpleDoc;\r
+import javax.print.attribute.HashDocAttributeSet;\r
+import javax.print.attribute.HashPrintRequestAttributeSet;\r
+import javax.print.attribute.PrintRequestAttributeSet;\r
+import javax.print.attribute.standard.Copies;\r
+import javax.print.attribute.standard.RequestingUserName;\r
+import javax.print.attribute.standard.Sides;\r
+\r
+import de.lohndirekt.print.IppPrintServiceLookup;\r
+import de.lohndirekt.print.attribute.auth.RequestingUserPassword;\r
+\r
+/**\r
+ * @author sefftinge\r
+ * \r
+ *  \r
+ */\r
+public class SimpleDocExample {\r
+\r
+    /**\r
+     * @param args\r
+     * @throws URISyntaxException\r
+     * @throws PrintException\r
+     * @throws IOException\r
+     */\r
+    public static void main(String[] args) throws URISyntaxException,\r
+            PrintException, IOException {\r
+\r
+        // setting cups properties\r
+        System.getProperties().setProperty(IppPrintServiceLookup.URI_KEY,\r
+                Messages.getString("cups.uri")); //$NON-NLS-1$\r
+        System.getProperties().setProperty(IppPrintServiceLookup.USERNAME_KEY,\r
+                Messages.getString("cups.username")); //$NON-NLS-1$\r
+        System.getProperties().setProperty(IppPrintServiceLookup.PASSWORD_KEY,\r
+                Messages.getString("cups.password")); //$NON-NLS-1$\r
+\r
+        // get the PrintServices\r
+        PrintService[] services = new IppPrintServiceLookup().getPrintServices();\r
+\r
+        // get the first Printer\r
+        if (services.length > 0) {\r
+            PrintService service = null;\r
+            System.out.println("Please choose a print service:");\r
+            for (int i = 0; i < services.length; i++) {\r
+                PrintService service2 = services[i];\r
+                System.out.println("[" + i + "] : " + service2.getName());\r
+            }\r
+            // let the user choose a service\r
+            while (true) {\r
+                int serviceToUse = Integer.valueOf(readStdIn()).intValue();\r
+                if (serviceToUse < 0 || serviceToUse >= services.length) {\r
+                    System.out.println("Bitte eine Zahl zwischen 0 und "\r
+                            + (services.length - 1) + " eingeben.");\r
+                } else {\r
+                    service = services[serviceToUse];\r
+                    break;\r
+                }\r
+            }\r
+\r
+            // ask for username\r
+            System.out.print("Username : ");\r
+            String username = readStdIn().trim();\r
+            String password = null;\r
+            if (username != null && username.trim().length() > 0) {\r
+                System.out.print("Password : ");\r
+                password = readStdIn().trim();\r
+            }\r
+\r
+            System.out.println("Printing on: " + service.getName());\r
+            // create a job\r
+            CancelablePrintJob job = (CancelablePrintJob) service\r
+                    .createPrintJob();\r
+\r
+            // set the job attributes\r
+            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();\r
+\r
+            if (username != null && username.trim().length() > 0) {\r
+                RequestingUserName usernameAttr = new RequestingUserName(\r
+                        username, Locale.GERMANY);\r
+                RequestingUserPassword userpassAttr = new RequestingUserPassword(\r
+                        password, Locale.GERMANY);\r
+                attributes.add(usernameAttr);\r
+                attributes.add(userpassAttr);\r
+            }\r
+            // we just want one copy\r
+            Copies copies = new Copies(1);\r
+            attributes.add(copies);\r
+            // we want duplex printing\r
+            Sides sides = Sides.DUPLEX;\r
+            attributes.add(sides);\r
+            // print it on the main media tray\r
+                       // Media media = MediaTray.MAIN;\r
+            // If you have special Mediatrays (like most printers)\r
+            // you can use the class LdMediaTray and give a String to the\r
+            // constructor like\r
+            // new LdMediaTray("Optional2");\r
+                       // attributes.add(media);\r
+\r
+            // Now create a document\r
+            File testFile = new File(Messages.getString("pdfdocument.1"));\r
+            InputStream stream = new FileInputStream(testFile);\r
+            Doc doc = new SimpleDoc(stream, DocFlavor.INPUT_STREAM.PDF,\r
+                    new HashDocAttributeSet());\r
+\r
+            // finally the print action\r
+            try {\r
+                // we are setting the doc and the job attributes\r
+                job.print(doc, attributes);\r
+                System.out.println("printing successfull...");\r
+            } catch (PrintException e) {\r
+                e.printStackTrace();\r
+            }\r
+\r
+        } else {\r
+            System.out.println("no Services found!");\r
+        }\r
+    }\r
+\r
+    /**\r
+     * @return\r
+     */\r
+    private static String readStdIn() {\r
+        StringBuffer sb = new StringBuffer();\r
+        int ch;\r
+        try {\r
+            while ((ch = System.in.read()) != 10) {\r
+                sb.append((char) ch);\r
+            }\r
+        } catch (IOException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return sb.toString();\r
+    }\r
+}
\ No newline at end of file