22. November 2006
If
you build your own editor in the most cases you have to provide the
capability to print its content. In addition you probably also have to
print different business-logic that is not presented by an editor or
viewpart. SWT gives you the possibility to generate printing jobs, what
is a bit complex. With the Open-Source API PaperClips
there is a possibility to generate data that can be sent to a printer
in a very easy way. In addition it provides cool UI-Elments, e.g. a
Print Preview. In this article is explained how to register a
Print-Action as GlobalAction Handler, with formatting the data you want
to print and a Print-Preview.
Global ActionHandler
At first you have to implement your custom editor. After your editor
can be opened you have to set up a GlobalActionHandler and assign this
handler with an Action. In the example there is an Action that justs
open a wizard.
JAVA:
-
PrintAction printAction = new PrintAction(this.model);
-
site.getActionBars().setGlobalActionHandler(ActionFactory.PRINT.getId(),printAction);
If the editor is activated the Print-button is enabled.
Integrating PaperClips
After you have added the paperclips libraries you can
build printer-data with special formatting possibilites that are
shipped with the API. In this example a simple list with a header and
footer is generated. A big benefit is the runtime-generation of the
data you want to print. If you see the Wizard you have the possibility
to select special properties of your business-data. The preview will be
actualized every time your selection changes.
If you click on the "Finish" Button the system-specific print dialog will be opened and a print-job is queued.
JAVA:
-
PrintDialog dialog = new PrintDialog(Display.getDefault().getActiveShell(), SWT.NONE);
-
PrinterData printerData = dialog.open ();
-
if (printerData != null) {
-
PaperClips.print(PrintingJob.this.jobDelegate, printerData);
-
} else {
-
canceled = true;
-
}
Download
Download the Print Example as RCP (Source included - 10 Mbyte)
CVS-Checkout (more info)