org.netbeans.modules.print 7.3.1

org.netbeans.api.print
Class PrintManager

java.lang.Object
  extended by org.netbeans.api.print.PrintManager

public final class PrintManager
extends Object

The Print Manager is powerful functionality to preview and send data out to printer. Print action from File menu (Ctrl+Alt+Shift+P shortcut) invokes the Print Preview dialog. The Print Preview dialog provides page layout, the set of options including font, color, header, footer, printer settings such as paper size and orientation, number of copies, margins, collation and system properties.

There are several ways to enable printing for a custom data:

If the data is a Swing component which extends JComponent and shown in a TopComponent, the key PRINT_PRINTABLE with value "Boolean.TRUE" in the component must be set as a client property. See example:

 public class MyComponent extends javax.swing.JComponent {
   public MyComponent() {
     ...
     putClientProperty("print.printable", Boolean.TRUE); // NOI18N
   }
   ...
 }
The key PRINT_NAME is used to specify the name of the component which will be printed in the header/footer:
 putClientProperty("print.name", <name>); // NOI18N
If the key is not set at all, the display name of the top component is used by default. The content of the header/footer can be adjusted in the Print Options dialog.

If the size of the custom component for printing differs from visual dimension, specify this with the key PRINT_SIZE:

 putClientProperty("print.size", new Dimension(printWidth, printHeight)); // NOI18N
If the custom data is presented by several components, all of them can be enabled for print preview. The key PRINT_ORDER is used for this purpose, all visible and printable components are ordered and shown in the Print Preview dialog from the left to right:
 putClientProperty("print.order", <order>); // NOI18N
If the custom data is presented by another classes, a PrintProvider should be implemented and put in the lookup of the top component where the custom data lives. How to put the Print action on custom Swing tool bar:
 public class MyComponent extends javax.swing.JComponent {
   ...
   JToolBar toolbar = new JToolBar();
   // print
   toolbar.addSeparator();
   toolbar.add(PrintManager.printAction(this));
   ...
 }
How does Print action from the main menu decide what to print?

At first, the manager searches for PrintProvider in the lookup of the active top component. If a print provider is found, it is used by the print manager for print preview.

Otherwise, it tries to obtain printable components among the descendants of the active top component. All found printable components are passed into the Print Preview dialog. Note that print method is invoked by the manager for preview and printing the component.

If there are no printable components, printable data are retrieved from the selected nodes of the active top component. The Print manager gets EditorCookie from the DataObject of the Nodes. The StyledDocuments, returned by the editor cookies, contain printing information (text, font, color). This information is shown in the print preview. So, any textual documents (Java/C++/Php/... sources, html, xml, plain text, etc.) are printable by default.

See Also:
PrintProvider

Field Summary
static String PRINT_NAME
          This key indicates the name of the component being printed.
static String PRINT_ORDER
          This key indicates the order of the component being printed.
static String PRINT_PRINTABLE
          This key indicates whether the component is printable.
static String PRINT_SIZE
          This key indicates the size of the component being printed.
 
Method Summary
static Action printAction(JComponent component)
          Returns the Print action for a component.
static Action printAction(PrintProvider[] providers)
          Returns the Print action for the given print providers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRINT_NAME

public static final String PRINT_NAME
This key indicates the name of the component being printed. By default, the name is shown in the left part of the header.

See Also:
Constant Field Values

PRINT_ORDER

public static final String PRINT_ORDER
This key indicates the order of the component being printed. The value of the key must be Integer. All visible and printable components are ordered and shown in the Print Preview dialog from the left to right.

See Also:
Constant Field Values

PRINT_SIZE

public static final String PRINT_SIZE
This key indicates the size of the component being printed. The value of the key must be Dimension.

See Also:
Constant Field Values

PRINT_PRINTABLE

public static final String PRINT_PRINTABLE
This key indicates whether the component is printable. To be printable the value Boolean.TRUE must be set as a client property of the component.

See Also:
Constant Field Values
Method Detail

printAction

public static Action printAction(JComponent component)
Returns the Print action for a component. All printable components are obtained among the descendants of the given component. All found printable components are passed into the Print Preview dialog.

Parameters:
component - is the component being printed
Returns:
the Print action
See Also:
PrintProvider

printAction

public static Action printAction(PrintProvider[] providers)
Returns the Print action for the given print providers. All PrintPages returned by the providers are shown in the Print Preview dialog.

Parameters:
providers - is the array of print providers
Returns:
the Print action
See Also:
PrintProvider

org.netbeans.modules.print 7.3.1

Built on February 22 2011.  |  Portions Copyright 1997-2011 Sun Microsystems, Inc. All rights reserved.