This example has a Swing frame containing an instance
of
IlvManagerView that displays the contents of a manager. The application
has also a menu bar with standard printing menu commands such as
Print...
,
Print
Preview
,
Page Setup...
, and
so on.
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);
}
});
}