http://www.richclient2.eu/2006_11_22/printing-with-swt-an-eclipse-editor-example/
Printing with SWT - An Eclipse Editor Example
22. November 2006
Tom Seidel @ 21:14

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.

PLAIN TEXT
JAVA:
  1. PrintAction printAction = new PrintAction ( this. model );
  2. 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.

PLAIN TEXT
JAVA:
  1. PrintDialog dialog = new PrintDialog (Display. getDefault ( ). getActiveShell ( ), SWT. NONE );
  2. PrinterData printerData = dialog. open ( );
  3. if (printerData != null ) {
  4.     PaperClips. print (PrintingJob. this. jobDelegate, printerData );
  5. } else {
  6. canceled = true;
  7. }

Download

Download the Print Example as RCP (Source included - 10 Mbyte)
CVS-Checkout (more info)