0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4700712 4707777
0N/A * @summary Should submit only 1 job in Windows and print only 1 page.
0N/A * @author jgodinez
0N/A * @run main/manual Example
0N/A */
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport javax.swing.*;
0N/Aimport java.io.*;
0N/Aimport javax.print.*;
0N/Aimport javax.print.attribute.*;
0N/Aimport javax.print.event.*;
0N/A
0N/Apublic class Example
0N/A{
0N/A public static void main(String [] args)
0N/A {
0N/A if(args.length != 1)
0N/A {
0N/A System.err.println("Usage: Example num_sections");
0N/A return;
0N/A }
0N/A
0N/A try{
0N/A int stream_sections = Integer.parseInt(args[0]);
0N/A DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
0N/A PrintService [] services = PrintServiceLookup.lookupPrintServices
0N/A(flavor, null);
0N/A
0N/A if(services.length > 0)
0N/A {
0N/A PrintRequestAttributeSet attbs = new
0N/AHashPrintRequestAttributeSet();
0N/A PrintService service = ServiceUI.printDialog(null, 100, 100,
0N/Aservices, null, flavor, attbs);
0N/A
0N/A if(service != null)
0N/A {
0N/A InputStream stream = createInputStream(stream_sections);
0N/A Doc doc = new SimpleDoc(stream, flavor, null);
0N/A DocPrintJob job = service.createPrintJob();
0N/A job.addPrintJobListener(new PrintJobListener(){
0N/A public void printJobCanceled(PrintJobEvent e)
0N/A {
0N/A finish("Canceled");
0N/A }
0N/A
0N/A public void printJobCompleted(PrintJobEvent e)
0N/A {
0N/A finish("Complete");
0N/A }
0N/A
0N/A public void printJobFailed(PrintJobEvent e)
0N/A {
0N/A finish("Failed");
0N/A }
0N/A
0N/A public void printDataTransferCompleted(PrintJobEvent
0N/Apje)
0N/A {
0N/A System.out.println("data transfered");
0N/A }
0N/A
0N/A public void printJobNoMoreEvents(PrintJobEvent pje)
0N/A {
0N/A finish("Complete");
0N/A }
0N/A public void printJobRequiresAttention(PrintJobEvent pje)
0N/A{}
0N/A
0N/A });
0N/A System.out.println("Printing...");
0N/A job.print(doc, attbs);
0N/A }
0N/A
0N/A }else
0N/A {
0N/A System.out.println("no printers found");
0N/A }
0N/A
0N/A }catch(Exception e)
0N/A {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A private static void finish(String message)
0N/A {
0N/A System.out.println("Printing " + message);
0N/A System.out.flush();
0N/A }
0N/A
0N/A private static InputStream createInputStream(int num_sections)
0N/A {
0N/A 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();
0N/A
0N/A return new TestInputStream(bytes, num_sections);
0N/A }
0N/A
0N/A private static class TestInputStream extends ByteArrayInputStream
0N/A {
0N/A public TestInputStream(byte [] bytes, int sections)
0N/A {
0N/A super(bytes);
0N/A int avail = super.available();
0N/A block_size = avail / sections;
0N/A }
0N/A
0N/A public int available()
0N/A {
0N/A int true_avail = super.available();
0N/A return true_avail == 0 ? 0 : block_size;
0N/A }
0N/A
0N/A private int block_size;
0N/A }
0N/A}