マネージャーの内容を印刷する Swing アプリケーション

この例では、 IlvManagerPrintingController および IlvManagerPrintableDocument クラスを使用している完全アプリケーションが、マネージャーの内容を複数ページに印刷します。
この例には、マネージャーの内容を表示する IlvManagerView のインスタンスを含む Swing フレームがあります。 また、このアプリケーションには、標準印刷メニュー・コマンド (Print...Print PreviewPage Setup... など) を含むメニュー・バーもあります。
import java.awt.*;
import java.awt.print.*;
import java.awt.event.*;
import javax.swing.*;
import ilog.views.print.*;
import ilog.views.util.print.*;
import ilog.views.*;
import ilog.views.interactor.*;
import ilog.views.swing.*;

/**
 * This is a very simple example to show how to use 
 * the IlvManagerPrintableDocument class.
 */
public class ExamplePrint extends JFrame
{
  /**
   * The manager to print.
   */
  IlvManager manager;

  /**
   * An IlvManagerView to display the content of a manager.
   */
  IlvManagerView mgrview;

  /**
   * The printing controller.
   */
  IlvManagerPrintingController controller;

  /**
   * The interactor that allows you to specify the area to print.
   */
  IlvPrintAreaInteractor printAreaInteractor;

  /**
   * Creates and initializes the example.
   */
  public ExamplePrint() {

    super("Printing Example");
    getContentPane().setLayout(new BorderLayout());

    // Creates the manager to print.
    manager = new IlvManager();

    // Fills the manager with some data.
    try {
       manager.read("data.ivl");
    } catch (Exception e) {
    }    


    // Creates a view of the manager.
    mgrview  = new IlvManagerView(manager);
    mgrview.setBackground(Color.white);
    mgrview.setKeepingAspectRatio(true);

    // Creates the printing controller
    controller = new IlvManagerPrintingController(mgrview) ;

    // Initializes the document with some parameters.
    IlvPrintableDocument document = controller.getDocument();

    document.setName("data.ivl");
    document.setAuthor("My name");

    // Creates the interactor.
    printAreaInteractor = new IlvPrintAreaInteractor(controller);

    // Adds a toolbar to edit/zoom/pan.
    IlvJManagerViewControlBar toolbar 
          = new IlvJManagerViewControlBar();
    toolbar.setView(mgrview);

    // Creates a scroll manager view.
    IlvJScrollManagerView scrollview 
             = new IlvJScrollManagerView(mgrview);
    getContentPane().add(scrollview, BorderLayout.CENTER);
    getContentPane().add(toolbar, BorderLayout.NORTH);
    scrollview.setPreferredSize(new Dimension(300,300));
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    addWindowListener(new WindowAdapter () {
        public void windowClosed (WindowEvent e) {
          System.exit(0);
        }
    });

    // Creates the menu bar.
    setJMenuBar(createMenu());

  }

  private JMenuBar createMenu() {

    JMenuBar bar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu parea = new JMenu("Print Area");
    JMenuItem preview = new JMenuItem("Print Preview...");
    JMenuItem setup = new JMenuItem("Page Setup...");
    JMenuItem setarea = new JMenuItem("Set Print Area");
    JMenuItem cleararea = new JMenuItem("Clear Print Area");
    JMenuItem print = new JMenuItem("Print...");
   
   
    // Action to open the print preview dialog.
    preview.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
          controller.printPreview(ExamplePrint.this);
        }
      });

    // Action to open the setup dialog box.
    setup.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
          controller.setupDialog(ExamplePrint.this, true, true);
         
        }
      });

    // Action to print the document.
    print.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
          try {
            controller.print(true);
          } catch (Exception e) { }
         
        }
      });

    // Action to install the print area interactor.
    setarea.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
          mgrview.setInteractor(printAreaInteractor);
        }
      });


    // Action to reset the print area to full manager size.
    cleararea.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
          ((IlvManagerPrintableDocument)controller.
                getDocument()).setPrintArea(null);
        }
      });


    file.add(setup);
    file.add(parea);
    parea.add(setarea);
    parea.add(cleararea);
    file.add(preview);
    file.add(print);

    bar.add(file);

    return bar;

  }

public static void main(String[] args)
{
  SwingUtilities.invokeLater(
    new Runnable() {
      public void run() {
        ExamplePrint example = new ExamplePrint();
        example.pack();
        example.setVisible(true);
      }
    });
}