Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to print the contents of a ViewPart
How to print the contents of a ViewPart [message #465655] Fri, 16 December 2005 10:00 Go to next message
Eclipse UserFriend
Originally posted by: faizy.chaudhary.symbian.com

Hi,

I am a newbie to eclipse plug-in development and writing an application
which has to plot chart on a Panel. I managed to enable the RetargetAction
for 'Print', but I don't know exactly how to get the Image on the Panel,
which resides in the ViewPart and draw it on the GC of Printer. I tried
the following snippet, but it gives a blank printout.

printAction = new Action(){
public void run(){
PrintDialog dialog = new PrintDialog(_parent.getShell(), SWT.NONE);
dialog.setText("Select printer");
dialog.setScope(PrinterData.ALL_PAGES);
dialog.setPrintToFile(false);
PrinterData pdata = dialog.open();
&n bsp; if (pdata==null) {
_parent.getShell().setCursor(null);
return;
}

Printer printer = new Printer(pdata);
GC gc = new GC(printer);
//Rectangle dm = printer.getClientArea();
Point dpi = printer.getDPI();
int scale = dpi.x/_parent.getDisplay().getDPI().x;
Rectangle offset = _parent.getDisplay().getBounds();
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
Image _printImage=null, _image = null;
GC gcView = null;
try {
if (printer.startJob("Jobname") && printer.startPage())
{
_image =
new Image(
_parent.getDisplay(),
_parent.getClientArea().width,
_parent.getClientArea().height);
gcView = new GC(_parent.getDisplay());
gcView.copyArea(_image, offset.width, offset.height);
ImageData imageData = _image.getImageData();
_printImage = new Image(printer,imageData);
gc.drawImage(_printImage,
0, 0,
_printImage.getImageData().width,
_printImage.getImageData().width,
-trim.x,
-trim.y,
(int) scale * _printImage.getImageData().width,
(int) scale * _printImage.getImageData().width);
}
} catch (Throwable t) {
}
_printImage.dispose();
_image.dispose();
gcView.dispose();
gc.dispose();
printer.endPage();
printer.endJob();
System.out.println("done...");
}
};
printAction.setToolTipText("Print");
printAction.setEnabled(true);

getViewSite().getActionBars().setGlobalActionHandler(ActionF actory.PRINT.getId(),
printAction );

Any help will be highly appreciated.

Thanks and regards,

Faizy
Re: How to print the contents of a ViewPart [message #465868 is a reply to message #465655] Tue, 20 December 2005 22:23 Go to previous message
Carolyn MacLeod is currently offline Carolyn MacLeodFriend
Messages: 149
Registered: July 2009
Senior Member
Hi, Faizy.

Here is an old snippet that prints a snapshot of a widget. (It happens to be
a tree, but it should work similarly for your chart view).
I didn't try your code, but it looks to me like you are trying to snap the
whole screen (display.getBounds) and then only print a part of it
(parent.getClientArea).
You will also want to watch out for using (width, width) instead of (width,
height) in your call to drawImage.

Anyhow, I include this snippet because it works... but you really shouldn't
be trying to "snap" your chart to print it. Instead, you should be drawing
the chart on the printer. In other words, make your chart drawing code take
the "device" as a parameter. Then if you're drawing on the display, just
draw your chart like you do now, but if you are drawing to the printer, you
can calculate the scale factor and margins, etc., before drawing. Create all
graphics resources on the device that was passed in.

The "take a screen snapshot and send it to the printer" snippet is pasted
below, but remember, this is a hack just so that you can see some output.
You don't want to rely on a screen snapshot for printing because other
windows can obscure it, etc., etc.

Hope this helps,
Carolyn
/* print a tree to the printer */

import org.eclipse.swt.*;

import org.eclipse.swt.graphics.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.events.*;

import org.eclipse.swt.layout.*;

import org.eclipse.swt.printing.*;

public class PrintTreeToPrinter {

Display display;

Shell shell;

Tree tree;

Image treeImage;

Printer printer;

GC gc;

int leftMargin, rightMargin, topMargin, bottomMargin;

int x, y;

public static void main(String[] args) {

new PrintTreeToPrinter().open();

}


void open() {

display = new Display();

shell = new Shell(display);

shell.setLayout(new FillLayout());

shell.setText("Print Tree");

/* Create a sample Tree widget */

tree = new Tree (shell, SWT.BORDER);

for (int i=0; i<4; i++) {

TreeItem iItem = new TreeItem (tree, 0);

iItem.setText ("TreeItem (0) -" + i);

for (int j=0; j<4; j++) {

TreeItem jItem = new TreeItem (iItem, 0);

jItem.setText ("TreeItem (1) -" + j);

for (int k=0; k<4; k++) {

TreeItem kItem = new TreeItem (jItem, 0);

kItem.setText ("TreeItem (2) -" + k);

for (int l=0; l<4; l++) {

TreeItem lItem = new TreeItem (kItem, 0);

lItem.setText ("TreeItem (3) -" + l);

}

}

}

}


Menu menuBar = new Menu(shell, SWT.BAR);

shell.setMenuBar(menuBar);

MenuItem item = new MenuItem(menuBar, SWT.CASCADE);

item.setText("&File");

Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);

item.setMenu(fileMenu);

item = new MenuItem(fileMenu, SWT.NULL);

item.setText("&Print...");

item.setAccelerator(SWT.CTRL + 'P');

item.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent event) {

menuPrint();

}

});

new MenuItem(fileMenu, SWT.SEPARATOR);

item = new MenuItem(fileMenu, SWT.NULL);

item.setText("E&xit");

item.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent event) {

System.exit(0);

}

});


shell.pack();

shell.open();

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) display.sleep();

}

treeImage.dispose();

}

void menuPrint() {

PrintDialog dialog = new PrintDialog(shell, SWT.NULL);

PrinterData data = dialog.open();

if (data == null) return;


/* Snap the Tree widget. */

Rectangle treeBounds = tree.getBounds();

if (treeImage != null) treeImage.dispose();

treeImage = new Image(display, treeBounds.width, treeBounds.height);

GC gc = new GC(tree);

gc.copyArea(treeImage, 0, 0);

gc.dispose();


/* Copy the image to a printer-compatible image. */

ImageData imageData = treeImage.getImageData();

if (treeImage != null) treeImage.dispose();

treeImage = new Image(printer, imageData);

/* Do the printing in a background thread so that spooling does not freeze
the UI. */

printer = new Printer(data);

Thread printingThread = new Thread("Printing") {

public void run() {

print(printer);

printer.dispose();

}

};

printingThread.start();

}


void print(Printer printer) {

if (printer.startJob("Tree")) {

Rectangle clientArea = printer.getClientArea();

Rectangle trim = printer.computeTrim(0, 0, 0, 0);

Point dpi = printer.getDPI();

leftMargin = dpi.x + trim.x; // one inch from left side of paper

rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch
from right side of paper

topMargin = dpi.y + trim.y; // one inch from top edge of paper

bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch
from bottom edge of paper


/* Create printer GC and print image to it. */

gc = new GC(printer);

printer.startPage();

Rectangle imageBounds = treeImage.getBounds();

x = clientArea.width / 2 - dpi.x; // center image horizontally

y += dpi.y; // draw 2" wide image 1" below text

gc.drawImage(treeImage, 0, 0, imageBounds.width, imageBounds.height,

x, y, dpi.x * 2, dpi.y * 2 * imageBounds.height / imageBounds.width);

printer.endPage();

printer.endJob();

/* Cleanup graphics resources used in printing */

gc.dispose();

}

}

}


"Faizy Chaudhary" <faizy.chaudhary@symbian.com> wrote in message
news:2eab2671615334af73c16636043cab3d$1@www.eclipse.org...
> Hi,
>
> I am a newbie to eclipse plug-in development and writing an application
> which has to plot chart on a Panel. I managed to enable the RetargetAction
> for 'Print', but I don't know exactly how to get the Image on the Panel,
> which resides in the ViewPart and draw it on the GC of Printer. I tried
> the following snippet, but it gives a blank printout.
> printAction = new Action(){
> public void run(){
> PrintDialog dialog = new PrintDialog(_parent.getShell(), SWT.NONE);
> dialog.setText("Select printer");
> dialog.setScope(PrinterData.ALL_PAGES);
> dialog.setPrintToFile(false);
> PrinterData pdata = dialog.open();
> &n bsp; if (pdata==null) {
> _parent.getShell().setCursor(null);
> return;
> }
> Printer printer = new Printer(pdata);
> GC gc = new GC(printer);
> //Rectangle dm = printer.getClientArea();
> Point dpi = printer.getDPI();
> int scale = dpi.x/_parent.getDisplay().getDPI().x;
> Rectangle offset = _parent.getDisplay().getBounds();
> Rectangle trim = printer.computeTrim(0, 0, 0, 0);
> Image _printImage=null, _image = null;
> GC gcView = null;
> try {
> if (printer.startJob("Jobname") && printer.startPage())
> {
> _image =
> new Image(
> _parent.getDisplay(),
> _parent.getClientArea().width,
> _parent.getClientArea().height);
> gcView = new GC(_parent.getDisplay());
> gcView.copyArea(_image, offset.width, offset.height);
> ImageData imageData = _image.getImageData();
> _printImage = new Image(printer,imageData);
> gc.drawImage(_printImage,
> 0, 0,
> _printImage.getImageData().width,
> _printImage.getImageData().width,
> -trim.x,
> -trim.y,
> (int) scale * _printImage.getImageData().width,
> (int) scale * _printImage.getImageData().width);
> }
> } catch (Throwable t) {
> }
> _printImage.dispose();
> _image.dispose();
> gcView.dispose();
> gc.dispose();
> printer.endPage();
> printer.endJob();
> System.out.println("done...");
> }
> };
> printAction.setToolTipText("Print");
> printAction.setEnabled(true);
>
> getViewSite().getActionBars().setGlobalActionHandler(ActionF actory.PRINT.getId(),
> printAction );
>
> Any help will be highly appreciated.
>
> Thanks and regards,
>
> Faizy
>
Previous Topic:SWT Printing with Windows to a Tray
Next Topic:Anyone know where I can get Firefox with linkable Gecko libraries?
Goto Forum:
  


Current Time: Tue Apr 23 07:58:58 GMT 2024

Powered by FUDForum. Page generated in 0.03046 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top