Printing with SWT - An Eclipse Editor Example
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.
-
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.
-
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)



There is not dependency in project from CVS to PaperClips plugin
Comment by Siarhei Berdachuk — 1. December 2006 @ 18:35
Need help: Where is the source-code in the zip-package?
Comment by Christian — 10. August 2007 @ 10:27
Unzip the package, open the file plugins/de.spiritlink.editor.printing_1.0.0.jar with an Unzipper. In this package you’ll find the source-code under the ’src’-Folder.
Comment by Tom Seidel — 10. August 2007 @ 13:54
[…] with SWT - An Eclipse Editor Example August 20, 2007 Posted by kathayat in Eclipse RCP. trackback >> How to - Printing with SWT - An Eclipse EditorExample […]
Pingback by Printing with SWT - An Eclipse Editor Example « Surya ’s Blog — 20. August 2007 @ 10:13
Sorry for a somewhat tardy and irrelevant question: Shouldn’t the printAction be unregistered somewhere in the editor? site.getActionBars().setGlobalActionHandler() registers it as a print handler but what will happend when the editor is closed? I’ve searched for places in the SDK where the print command is associated with a handler but found nothing eloquent. Could you possibly give a hint?
Comment by Ivan L — 12. November 2010 @ 12:10