0N/A/*
2362N/A * Copyright (c) 1997, 2000, 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
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/Apackage java.awt.print;
0N/A
0N/Aimport java.util.Vector;
0N/A
0N/A/**
0N/A * The <code>Book</code> class provides a representation of a document in
0N/A * which pages may have different page formats and page painters. This
0N/A * class uses the {@link Pageable} interface to interact with a
0N/A * {@link PrinterJob}.
0N/A * @see Pageable
0N/A * @see PrinterJob
0N/A*/
0N/A
0N/Apublic class Book implements Pageable {
0N/A
0N/A /* Class Constants */
0N/A
0N/A /* Class Variables */
0N/A
0N/A /* Instance Variables */
0N/A
0N/A /**
0N/A * The set of pages that make up the Book.
0N/A */
0N/A private Vector mPages;
0N/A
0N/A /* Instance Methods */
0N/A
0N/A /**
0N/A * Creates a new, empty <code>Book</code>.
0N/A */
0N/A public Book() {
0N/A mPages = new Vector();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of pages in this <code>Book</code>.
0N/A * @return the number of pages this <code>Book</code> contains.
0N/A */
0N/A public int getNumberOfPages(){
0N/A return mPages.size();
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@link PageFormat} of the page specified by
0N/A * <code>pageIndex</code>.
0N/A * @param pageIndex the zero based index of the page whose
0N/A * <code>PageFormat</code> is being requested
0N/A * @return the <code>PageFormat</code> describing the size and
0N/A * orientation of the page.
0N/A * @throws IndexOutOfBoundsException if the <code>Pageable</code>
0N/A * does not contain the requested page
0N/A */
0N/A public PageFormat getPageFormat(int pageIndex)
0N/A throws IndexOutOfBoundsException
0N/A {
0N/A return getPage(pageIndex).getPageFormat();
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@link Printable} instance responsible for rendering
0N/A * the page specified by <code>pageIndex</code>.
0N/A * @param pageIndex the zero based index of the page whose
0N/A * <code>Printable</code> is being requested
0N/A * @return the <code>Printable</code> that renders the page.
0N/A * @throws IndexOutOfBoundsException if the <code>Pageable</code>
0N/A * does not contain the requested page
0N/A */
0N/A public Printable getPrintable(int pageIndex)
0N/A throws IndexOutOfBoundsException
0N/A {
0N/A return getPage(pageIndex).getPrintable();
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>PageFormat</code> and the <code>Painter</code> for a
0N/A * specified page number.
0N/A * @param pageIndex the zero based index of the page whose
0N/A * painter and format is altered
0N/A * @param painter the <code>Printable</code> instance that
0N/A * renders the page
0N/A * @param page the size and orientation of the page
0N/A * @throws IndexOutOfBoundsException if the specified
0N/A * page is not already in this <code>Book</code>
0N/A * @throws NullPointerException if the <code>painter</code> or
0N/A * <code>page</code> argument is <code>null</code>
0N/A */
0N/A public void setPage(int pageIndex, Printable painter, PageFormat page)
0N/A throws IndexOutOfBoundsException
0N/A {
0N/A if (painter == null) {
0N/A throw new NullPointerException("painter is null");
0N/A }
0N/A
0N/A if (page == null) {
0N/A throw new NullPointerException("page is null");
0N/A }
0N/A
0N/A mPages.setElementAt(new BookPage(painter, page), pageIndex);
0N/A }
0N/A
0N/A /**
0N/A * Appends a single page to the end of this <code>Book</code>.
0N/A * @param painter the <code>Printable</code> instance that
0N/A * renders the page
0N/A * @param page the size and orientation of the page
0N/A * @throws <code>NullPointerException</code>
0N/A * If the <code>painter</code> or <code>page</code>
0N/A * argument is <code>null</code>
0N/A */
0N/A public void append(Printable painter, PageFormat page) {
0N/A mPages.addElement(new BookPage(painter, page));
0N/A }
0N/A
0N/A /**
0N/A * Appends <code>numPages</code> pages to the end of this
0N/A * <code>Book</code>. Each of the pages is associated with
0N/A * <code>page</code>.
0N/A * @param painter the <code>Printable</code> instance that renders
0N/A * the page
0N/A * @param page the size and orientation of the page
0N/A * @param numPages the number of pages to be added to the
0N/A * this <code>Book</code>.
0N/A * @throws <code>NullPointerException</code>
0N/A * If the <code>painter</code> or <code>page</code>
0N/A * argument is <code>null</code>
0N/A */
0N/A public void append(Printable painter, PageFormat page, int numPages) {
0N/A BookPage bookPage = new BookPage(painter, page);
0N/A int pageIndex = mPages.size();
0N/A int newSize = pageIndex + numPages;
0N/A
0N/A mPages.setSize(newSize);
0N/A for(int i = pageIndex; i < newSize; i++){
0N/A mPages.setElementAt(bookPage, i);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the BookPage for the page specified by 'pageIndex'.
0N/A */
0N/A private BookPage getPage(int pageIndex)
0N/A throws ArrayIndexOutOfBoundsException
0N/A {
0N/A return (BookPage) mPages.elementAt(pageIndex);
0N/A }
0N/A
0N/A /**
0N/A * The BookPage inner class describes an individual
0N/A * page in a Book through a PageFormat-Printable pair.
0N/A */
0N/A private class BookPage {
0N/A /**
0N/A * The size and orientation of the page.
0N/A */
0N/A private PageFormat mFormat;
0N/A
0N/A /**
0N/A * The instance that will draw the page.
0N/A */
0N/A private Printable mPainter;
0N/A
0N/A /**
0N/A * A new instance where 'format' describes the page's
0N/A * size and orientation and 'painter' is the instance
0N/A * that will draw the page's graphics.
0N/A * @throws NullPointerException
0N/A * If the <code>painter</code> or <code>format</code>
0N/A * argument is <code>null</code>
0N/A */
0N/A BookPage(Printable painter, PageFormat format) {
0N/A
0N/A if (painter == null || format == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A
0N/A mFormat = format;
0N/A mPainter = painter;
0N/A }
0N/A
0N/A /**
0N/A * Return the instance that paints the
0N/A * page.
0N/A */
0N/A Printable getPrintable() {
0N/A return mPainter;
0N/A }
0N/A
0N/A /**
0N/A * Return the format of the page.
0N/A */
0N/A PageFormat getPageFormat() {
0N/A return mFormat;
0N/A }
0N/A }
0N/A}