Reputation: 1391
Main GUI is SWT based. I am running a print operation from a printPDF class with a button click.
public void startPDFPrint() throws Exception {
Display.getCurrent().syncExec(
new Runnable() {
public void run(){
try {
new AplotPdfPrintLocal().printPDF("c:\\temp\\file.pdf", "PDF Print Job");
}
catch (IOException e) {
e.printStackTrace();
}
catch (PrinterException e) {
e.printStackTrace();
}
}
});
}
The printPDF class is does not have any components or GUI. It just basically creates a run a print job.
public class PDFPrintPage implements Printable {
The only two methods in the class
public void printFile(String filename) throws IOException { (setups the print)
public int print(Graphics g, PageFormat format, int index)
throws PrinterException {
In the printFile method there is a line of code that opens a local printer dialog
pjob.printDialog()
The dialog is based in AWT.
How can I get this dialog to open, so my user can select a printer and number of copies?
I have read over the SWT_AWT bridge documentation, It looks like you need to embed AWT in a SWT Component, but my class does not have any components.
Do I need to create a component method and run the printFile code in the component?
I know if I can figure out this piece, it will also help with all my other issues I am having.
EDIT
Please look at my code and show me where I have it wrong. It complies and runs, but I am getting SWT Thread exception at the Dialog line.
public class PDFPrintPage extends ApplicationWindow{
private String fileURL;
private PageFormat pfDefault;
private PrinterJob pjob;
private PDFFile pdfFile;
public PDFPrintPage(Shell parent, String inputFileName) {
super(parent);
this.fileURL = inputFileName;
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected Control createContents(Composite parent) {
final Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
final java.awt.Frame frame = SWT_AWT.new_Frame( swtAwtComponent );
final javax.swing.JPanel panel = new javax.swing.JPanel( );
frame.add(panel);
JButton swingButton = new JButton("Print");
panel.add(swingButton);
swingButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent actionevent) {
try {
printFile(fileURL, frame);
}
catch (IOException e) {
e.printStackTrace();
}
}
});
return swtAwtComponent;
}
public void printFile(String filename, Frame panel) throws IOException {
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
pdfFile = new PDFFile(bb); // Create PDF Print Page
final PrintPage pages = new PrintPage(pdfFile);
pjob = PrinterJob.getPrinterJob();
pfDefault = PrinterJob.getPrinterJob().defaultPage();
Paper defaultPaper = new Paper();
defaultPaper.setImageableArea(0, 0, defaultPaper.getWidth(), defaultPaper.getHeight());
pfDefault.setPaper(defaultPaper);
pjob.setJobName(file.getName());
final Dialog awtDialog = new Dialog(panel);
Shell parent = getParentShell();
Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
shell.setSize(100, 100);
shell.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
awtDialog.requestFocus();
awtDialog.toFront();
}
});
//if (pjob.printDialog()) {
pfDefault = pjob.validatePage(pfDefault);
Book book = new Book();
book.append(pages, pfDefault, pdfFile.getNumPages());
pjob.setPageable(book);
try {
pjob.print();
}
catch (PrinterException exc) {
System.out.println(exc);
}
//}
}
class PrintPage implements Printable {
private PDFFile file;
PrintPage(PDFFile file) {
this.file = file;
}
public int print(Graphics g, PageFormat format, int index) throws PrinterException {
int pagenum = index + 1;
if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
Graphics2D g2 = (Graphics2D) g;
PDFPage page = file.getPage(pagenum);
Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
(int) format.getImageableWidth(), (int) format.getImageableHeight());
g2.translate(0, 0);
PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
try {
page.waitForFinish();
pgs.run();
} catch (InterruptedException ie) {
}
return PAGE_EXISTS;
}
else {
return NO_SUCH_PAGE;
}
}
}//End PrintPage Class
}//End PDFPrintPage Class
I may be adding your suggestion code in completely the wrong spot. My thoughts where to add the printDialog call in the focusGained(FocusEvent e) method.
Upvotes: 0
Views: 2818
Reputation: 143
As I don't have the reputation to add it as a comment, I'll post the answer instead. For anyone else who stumbles across this looking for the issue "The method addFocusListener(FocusListener) in the type Control is not applicable for the arguments (new FocusAdapter(){})".
The issue is that you have the wrong imports. You've probably imported the awt FocusAdapter rather than the swt one, or visa-versa.
Upvotes: 0
Reputation: 3085
You need to open a shell with zero size when you open your printer dialog so that it will look like your main SWT Shell is inactive and your Swing modal dialog on top it. Similarly you need to close the zero size Shell when you close your swing dialog.
java.awt.Dialog awtDialog = ...
Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
shell.setSize(0, 0);
shell.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
awtDialog.requestFocus();
awtDialog.toFront();
}
});
Upvotes: 1