PrintPanel is the base for an open-ended series of classes : Print « 2D Graphics GUI « Java






PrintPanel is the base for an open-ended series of classes

PrintPanel is the base for an open-ended series of classes
    
/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 * 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * PrintPanel is the base for an open-ended series of classes that implement
 * printing of one type. We provide two examples to start: First prints a phone
 * book, second prints labels, etc. To add more, for example a Mail List/Form
 * Letter Merge, define a class for it below and insert in the "add" loop in the
 * main constructor.
 */
abstract class PrintPanel extends Panel {
  /** Returns the string to use in a Choice to display this panel */
  public abstract String getChoice();

  /** Print the data in the format for this type of printout */
  public abstract void doPrint();
}

/**
 * PhonesPanel extends PrintPanel to provide the UI for printing a user's phone
 * book.
 */

class PhonesPanel extends PrintPanel {
  PhonesPanel() {
    super();
    setBackground(Color.red);
    setLayout(new FlowLayout());
    add(new Label("Tab markers at edge of page?"));
    add(new Checkbox());
    add(new Label("Each letter starts page?"));
    add(new Checkbox());
  }

  public String getChoice() {
    return "Phone Book";
  }

  public void doPrint() {
    // code here to print Phone book
  }
}

/**
 * LabelsPanel extends PrintPanel to provide the UI for printing name and
 * address labels
 */

class LabelsPanel extends PrintPanel {
  LabelsPanel() {
    super();
    setBackground(Color.green);
    setLayout(new GridLayout(3, 2));
    add(new Label("Left Offset:"));
    add(new TextField(5));
    add(new Label("Rows:"));
    add(new TextField(5));
    add(new Label("Cols:"));
    add(new TextField(5));
  }

  public String getChoice() {
    return "Labels";
  }

  public void doPrint() {
    // code here to print Labels
  }
}

/**
 * CardLayDemo2 -- Prototype of a Print Dialog for JabaDex
 * 
 * @author Ian F. Darwin
 * @version 0.0, September, 1996
 */

public class CardLayDemo2 extends Frame {
  PrintPanel[] pps = new PrintPanel[2];

  private int runType = 0;

  private Choice runTypeChoice;

  private Panel tp, mainP, bp; // top, middle, bottom.

  CardLayout cardlay;

  private Button printButton, sampleButton, cancelButton;

  /** Construct a Print dialog. */
  CardLayDemo2(String title) {
    super(title);

    // Top panel (tp) has choices for labels/phonebook/etc.
    // and paper size.
    // Middle panel (mainP, managed by CardLayout) is details
    // for either Labels or Phonebook
    //  Shows either a PhonesPanel or a LabelsPanel or ...
    // Bottom panel (bp) has Print/Preview/Cancel buttons.
    tp = new Panel();
    tp.setLayout(new FlowLayout());

    mainP = new Panel();
    mainP.setLayout(cardlay = new CardLayout());

    tp.add(runTypeChoice = new Choice());
    runTypeChoice.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        runType = runTypeChoice.getSelectedIndex();
        cardlay.show(mainP, pps[runType].getChoice());
      }
    });

    /* create one instance of each PrintPanel type. */
    pps[0] = new PhonesPanel();
    pps[1] = new LabelsPanel();

    /* Add each print type to the choice and to mainP */
    for (int i = 0; i < pps.length; i++) {
      runTypeChoice.add(pps[i].getChoice());
      mainP.add(pps[i].getChoice(), pps[i]);
    }

    cardlay.show(mainP, pps[runType].getChoice());

    // Bottom has a Panel with push buttons
    bp = new Panel();
    bp.setLayout(new FlowLayout());
    bp.add(printButton = new Button("Print"));
    printButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        doPrint(true);
        setVisible(false);
        System.exit(0);
      }
    });
    bp.add(sampleButton = new Button("Print Sample"));
    sampleButton.setEnabled(false);
    bp.add(cancelButton = new Button("Cancel"));
    cancelButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Printing canceled");
        setVisible(false);
        System.exit(0);
      }
    });

    setLayout(new BorderLayout());
    add(tp, BorderLayout.NORTH);
    add(mainP, BorderLayout.CENTER);
    add(bp, BorderLayout.SOUTH);

    pack();

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        // If windowClosing() does setVisible and dispose,
        // then the close action completes
        setVisible(false);
        dispose();
        System.exit(0);
      }
    });
  }

  /** Print the current list. */
  protected void doPrint(boolean toRealDevice) {

    // open a PrintStream to the printer device to file on disk.
    // PrintStream pf = ...

    // call the appropriate doPrint()
    // pps[runType].doPrint(pf);

    System.err.println("Print completed");
  }

  public static void main(String[] args) {

    // Generate some data...
    // ...

    // pop up the print dialog to print it
    (new CardLayDemo2("Print Tester")).setVisible(true);
  }
}

           
         
    
    
    
  








Related examples in the same category

1.The Printing code which implements Printable
2.Print an Image to print directly
3.Simplest SWT Print ExampleSimplest SWT Print Example
4.Print in Java 2: PrinterJob
5.Print in Java: page format and document
6.Print in Java: Multi page
7.Print in Java 5
8.Print in Java 6
9.Simple Book for printingSimple Book for printing
10.Shapes PrintShapes Print
11.Display the print dialog and print
12.Print the printable area outlinePrint the printable area outline
13.Print the text file and print preview themPrint the text file and print preview them
14.Printable demoPrintable demo
15.Print Swing componentsPrint Swing components
16.BookBook
17.Another print demoAnother print demo
18.Book demoBook demo
19.Printing the Combined-Java 1.2-and-1.4 WayPrinting the Combined-Java 1.2-and-1.4 Way
20.Printing the Java 1.4 Way
21.Prompting for a Printer
22.Printing the Java 1.1 WayPrinting the Java 1.1 Way
23.ScribbleScribble
24.Printable Document
25.PrintFile -- Print a file named on the command linePrintFile -- Print a file named on the command line
26.Print to the standard output
27.Pageable TextPageable Text
28.The area of the printable area
29.The area of the actual page
30.Printing Pages with Different Formats
31.Setting the Orientation of a Printed Page
32.Print Dialog: change the default printer settings(default printer, number of copies, range of pages)
33.Printing to a File
34.Listening for Print Service Status Changes
35.Print Image
36.Overriding the Default Action of a JTextComponent
37.Displaying the Page Format Dialog: changes the default page format such as orientation and paper size.
38.Printable Component
39.Create PageFormats on a higher level
40.Printing of a multi-page bookPrinting of a multi-page book