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)
Well this could be achieved if you create your own ApplicationWorkbenchWindowAdvisor like I did it to create logo for my RCP application, but you can use it for this task too. You can find what I mean if you look at http://birosoft.zexxo.net/demo/UpdateDemo.htm
. ApplicationLogo class in my case is a simple composite.
Source example:
package com.birosoft.workday.platform.intro;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.internal.progress.ProgressRegion;
import com.birosoft.workday.platform.Activator;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
private Control logo;
private Control toolbar;
private Control page;
private Control statusline;
private ProgressRegion progressRegion;
public ApplicationWorkbenchWindowAdvisor(
IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
public ActionBarAdvisor createActionBarAdvisor(
IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
Preferences prefs = Activator.getDefault().getPluginPreferences();
if (prefs.getInt(Application.APP_WIN_WIDTH) == 0
|| prefs.getInt(Application.APP_WIN_HEIGHT) == 0) {
configurer.setInitialSize(new Point(800, 600));
} else {
int width = prefs.getInt(Application.APP_WIN_WIDTH);
int height = prefs.getInt(Application.APP_WIN_HEIGHT);
configurer.setInitialSize(new Point(width, height));
}
configurer.setShowCoolBar(true);
configurer.setShowStatusLine(true);
configurer.setShowProgressIndicator(true);
configurer.setTitle(”Birosoft Workday Client - ”
+ Application.getCurrentClient().toString() + ” (”
+ Application.getUsername() + “)”);
}
public void postWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
Preferences prefs = Activator.getDefault().getPluginPreferences();
if (prefs.getInt(Application.APP_WIN_POS_X) == 0
|| prefs.getInt(Application.APP_WIN_POS_Y) == 0) {
configurer.getWindow().getShell().setLocation(0, 0);
} else {
int x = prefs.getInt(Application.APP_WIN_POS_X);
int y = prefs.getInt(Application.APP_WIN_POS_Y);
configurer.getWindow().getShell().setLocation(x, y);
}
}
@Override
public void postWindowClose() {
Preferences prefs = Activator.getDefault().getPluginPreferences();
Point size = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getShell().getSize();
prefs.setValue(Application.APP_WIN_WIDTH, size.x);
prefs.setValue(Application.APP_WIN_HEIGHT, size.y);
Point position = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getShell().getLocation();
prefs.setValue(Application.APP_WIN_POS_X, position.x);
prefs.setValue(Application.APP_WIN_POS_Y, position.y);
super.postWindowClose();
}
public void createWindowContents(Shell shell) {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
Menu menu = configurer.createMenuBar();
shell.setMenuBar(menu);
FormLayout layout = new FormLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
shell.setLayout(layout);
logo = new ApplicationLogo(
getWindowConfigurer().getWindow().getShell(), SWT.NONE);
Application.setApplicationLogo(((ApplicationLogo) logo));
toolbar = configurer.createCoolBarControl(shell);
((CoolBar) toolbar).setLocked(true);
page = configurer.createPageComposite(shell);
statusline = configurer.createStatusLineControl(shell);
createProgressIndicator(shell);
// The layout method does the work of connecting the
// controls together.
layoutNormal();
}
private void layoutNormal() {
// APPLOGO
FormData data = new FormData();
data.top = new FormAttachment(0, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
logo.setLayoutData(data);
// TOOLBAR
data = new FormData();
data.top = new FormAttachment(logo, 5, SWT.BOTTOM);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(page, 0, SWT.LEFT);
toolbar.setLayoutData(data);
// STATUS LINE
data = new FormData();
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
statusline.setLayoutData(data);
// PAGE CONTENTS
data = new FormData();
data.top = new FormAttachment(logo, 5, SWT.BOTTOM);
// data.left = new FormAttachment(toolbar, 0, SWT.RIGHT);
data.left = new FormAttachment(0, 5);
data.right = new FormAttachment(100, -5);
data.bottom = new FormAttachment(statusline);
page.setLayoutData(data);
layout();
}
private void layout() {
getWindowConfigurer().getWindow().getShell().layout(true);
if (page != null) {
((Composite) page).layout(true);
}
}
/**
* Create the progress indicator for the receiver.
* @param shell the parent shell
*/
private void createProgressIndicator(Shell shell) {
if (getWindowConfigurer().getShowProgressIndicator()) {
WorkbenchWindow window = (WorkbenchWindow) getWindowConfigurer()
.getWindow();
progressRegion = new ProgressRegion();
progressRegion.createContents(shell, window);
}
}
}
Comment by Mickey 鈥?13. March 2007 @ 19:41
Yes, you’re right.
Thanks for your addition
Comment by Tom Seidel 鈥?13. March 2007 @ 19:58