2012-11-19 147 views
0

主要的GUI是基於SWT的。我正在通過單擊按鈕從printPDF類執行打印操作。SWT - AWT橋樑

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(); 
      } 
      } 
     }); 
} 

printPDF類沒有任何組件或GUI。它只是基本上創建一個打印作業。

public class PDFPrintPage implements Printable { 

只有兩個方法在類

​​

在printFile方法有打開本地打印機對話框

pjob.printDialog() 

對話框總部設在AWT的代碼行。

如何讓此對話框打開,以便我的用戶可以選擇打印機和打印份數?

我已經閱讀了SWT_AWT橋文檔,它看起來像你需要將AWT嵌入到SWT組件中,但是我的類沒有任何組件。

我是否需要創建組件方法並在組件中運行printFile代碼?

我知道如果我能弄清楚這件作品,它也會幫助我解決我所有的其他問題。

編輯

請看看我的代碼,並告訴我在哪裏我又理解錯了。它遵循並運行,但我在對話框中獲得了SWT線程異常。

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 

我可能會在完全錯誤的位置添加您的建議代碼。我的想法是在focusGained(FocusEvent e)方法中添加printDialog調用的位置。

回答

1

當您打開打印機對話框時,需要打開一個大小爲零的外殼,以使其看起來像您的主SWT Shell處於非活動狀態,並且您的Swing模式對話框位於其頂部。同樣,您需要在關閉擺動對話框時關閉零大小的Shell。

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(); 
      } 
     }); 

參考: http://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html#sec-event-threads

+0

我收到以下錯誤 - 在類型的方法addFocusListener(的FocusListener)控制是不適用的參數(新FocusAdapter(){}) - – jkteater

+0

我解決了它 - 謝謝 – jkteater

+0

你能檢查我的編輯嗎?請看看您是否可以使用我的代碼向我顯示我的位置?我在Dialog行收到線程異常。 – jkteater