Example.java revision 0:37a05a11f281
1/* 2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24/** 25 * @test 26 * @bug 4700712 4707777 27 * @summary Should submit only 1 job in Windows and print only 1 page. 28 * @author jgodinez 29 * @run main/manual Example 30 */ 31import java.awt.*; 32import java.awt.event.*; 33import javax.swing.*; 34import java.io.*; 35import javax.print.*; 36import javax.print.attribute.*; 37import javax.print.event.*; 38 39public class Example 40{ 41 public static void main(String [] args) 42 { 43 if(args.length != 1) 44 { 45 System.err.println("Usage: Example num_sections"); 46 return; 47 } 48 49 try{ 50 int stream_sections = Integer.parseInt(args[0]); 51 DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 52 PrintService [] services = PrintServiceLookup.lookupPrintServices 53(flavor, null); 54 55 if(services.length > 0) 56 { 57 PrintRequestAttributeSet attbs = new 58HashPrintRequestAttributeSet(); 59 PrintService service = ServiceUI.printDialog(null, 100, 100, 60services, null, flavor, attbs); 61 62 if(service != null) 63 { 64 InputStream stream = createInputStream(stream_sections); 65 Doc doc = new SimpleDoc(stream, flavor, null); 66 DocPrintJob job = service.createPrintJob(); 67 job.addPrintJobListener(new PrintJobListener(){ 68 public void printJobCanceled(PrintJobEvent e) 69 { 70 finish("Canceled"); 71 } 72 73 public void printJobCompleted(PrintJobEvent e) 74 { 75 finish("Complete"); 76 } 77 78 public void printJobFailed(PrintJobEvent e) 79 { 80 finish("Failed"); 81 } 82 83 public void printDataTransferCompleted(PrintJobEvent 84pje) 85 { 86 System.out.println("data transfered"); 87 } 88 89 public void printJobNoMoreEvents(PrintJobEvent pje) 90 { 91 finish("Complete"); 92 } 93 public void printJobRequiresAttention(PrintJobEvent pje) 94{} 95 96 }); 97 System.out.println("Printing..."); 98 job.print(doc, attbs); 99 } 100 101 }else 102 { 103 System.out.println("no printers found"); 104 } 105 106 }catch(Exception e) 107 { 108 e.printStackTrace(); 109 } 110 } 111 112 private static void finish(String message) 113 { 114 System.out.println("Printing " + message); 115 System.out.flush(); 116 } 117 118 private static InputStream createInputStream(int num_sections) 119 { 120 byte [] bytes = "Returns the number of bytes that can be read (or skipped over)\nfrom this input stream without blocking by the next caller of\na method for this input stream. The next caller might be the same thread or\nanother thread. ".getBytes(); 121 122 return new TestInputStream(bytes, num_sections); 123 } 124 125 private static class TestInputStream extends ByteArrayInputStream 126 { 127 public TestInputStream(byte [] bytes, int sections) 128 { 129 super(bytes); 130 int avail = super.available(); 131 block_size = avail / sections; 132 } 133 134 public int available() 135 { 136 int true_avail = super.available(); 137 return true_avail == 0 ? 0 : block_size; 138 } 139 140 private int block_size; 141 } 142} 143