package slipapp; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.awt.print.PageFormat; import java.awt.print.Paper; import java.awt.print.PrinterJob; import java.net.MalformedURLException; import java.net.URL; import javax.print.PrintService; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; /** * * @author Doys */ public class PrintingApplet implements ActionListener { private AppletPrinting applet; private Container comp; private RunMode runMode = RunMode.TEST; private Printing prt; private BufferedImage[] buff; private int currentPage = 0; private PanelToolBar pToolbar; private PanelWriter pWriter; //--------------------------------------- private int frameWidth, frameHeight; private String urlXmlData, urlXmlMaster, urlXmlDetail; public PrintingApplet(String urlXmlMaster, String urlXmlDetail) { this.frameWidth = 900; this.frameHeight = 700; this.urlXmlMaster = urlXmlMaster; this.urlXmlDetail = urlXmlDetail; this.urlXmlData = ""; JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); this.comp = jframe; try { init();//เรียก init() ************************ } catch (Exception ex) { } jframe.setSize(this.frameWidth, this.frameHeight); jframe.setLocationRelativeTo(null); jframe.setTitle("Java Applet Printing (" + comp.getClass().getSimpleName() + ")"); jframe.setVisible(true); } public PrintingApplet(AppletPrinting applet, String urlXmlData, String urlXmlMaster, String urlXmlDetail) { this.comp = applet.getContentPane(); this.applet = applet; this.runMode = RunMode.RUNTIME; this.urlXmlMaster = urlXmlMaster; this.urlXmlDetail = urlXmlDetail; this.urlXmlData = urlXmlData; try { init(); } catch (Exception ex) { //applet.setError(ex.toString()); //applet.alert(ex.toString()); } applet.setVisible(true); } private void init() throws Exception { try { PrintingInterfaceTable pt = new PrintingInterfaceTable(this.urlXmlData, this.urlXmlMaster, this.urlXmlDetail); prt = pt.init(runMode); buff = prt.buffImage; if (pt.frameSize.getWidth() > 0 && pt.frameSize.getHeight() > 0) { this.frameWidth = pt.frameSize.width; this.frameHeight = pt.frameSize.height; } if (runMode == RunMode.RUNTIME && prt.autoPrint) { printToPrinter(true); } } catch (Exception e) { } //ถ้าเป็น Applet และ auto close & print ให้ปิด Dialog ไปเลย ไม่ต้องวาดแล้ว if (runMode == RunMode.RUNTIME && prt.autoCloseDlg && prt.autoPrint) { try { //Applet applet = (Applet) comp; applet.getAppletContext().showDocument(new URL("javascript:window.close()")); System.exit(0); } catch (MalformedURLException me) { } } //JOptionPane.showMessageDialog(comp, "test 1"); JPanel msp = new JPanel(); msp.setBackground(Color.GRAY); msp.setLayout(new FlowLayout(FlowLayout.CENTER)); msp.setPreferredSize(new Dimension(800, 850)); comp.add(msp); pToolbar = new PanelToolBar(this, prt, currentPage); pToolbar.setPreferredSize(new Dimension(800, 60)); pToolbar.setBackground(Color.WHITE); pToolbar.setLayout(new FlowLayout(FlowLayout.LEFT)); msp.add(pToolbar); pWriter = new PanelWriter(buff, 800, 850); msp.add(pWriter); JScrollPane s = new JScrollPane(msp); s.getVerticalScrollBar().setUnitIncrement(16); comp.add(s); } public int getCurrentPage() { return this.currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; this.pToolbar.setCurrentPage(currentPage); this.pWriter.setCurrentPage(currentPage); } public void printToPrinter(boolean printAll) throws Exception { //หาชื่อเครื่อง Printer String[] printerNames = prt.printerName.split(","); PrinterJob pj = PrinterJob.getPrinterJob(); PrintService printService = null; for (int i = 0; i < printerNames.length; i++) { printService = this.getService(printerNames[i]); if (printService != null) { break; } } //นำชื่อเครื่อง Printer มา set pj.setPrintService(printService); //หาค่า Default paper size PageFormat pf = pj.defaultPage(); Paper paper = pf.getPaper(); //พิมพ์ค่า paper size ที่เป็น pixel ออก Command line System.out.println("size " + paper.getWidth() + ":" + paper.getHeight()); //เปลี่ยนค่า paper size double width = prt.paperWidth * 28.34645669291339;// 595.3322834645669; double height = prt.paperHeight * 28.34645669291339;// 841.9181102362205; paper.setSize(width, height); paper.setImageableArea(0, 0, width, height); pf.setPaper(paper); pf.setOrientation(prt.orientation); //ประกาศ interface Printable AppletPrintable pt = new AppletPrintable(prt, printAll, currentPage); pj.setPrintable(pt, pf); pj.print(); } public PrintService getService(String printerName) { printerName = printerName.toLowerCase(); PrintService ps = null; PrintService[] printServices = PrinterJob.lookupPrintServices(); for (PrintService printService : printServices) { String name = printService.getName(); if (name.toLowerCase().indexOf(printerName) >= 0) { ps = printService; break; } } return ps; } @Override public void actionPerformed(ActionEvent e) { try { JButton bt = (JButton) e.getSource(); String bt_name = bt.getName(); if (bt_name.equals("print_page")) { int result = JOptionPane.showConfirmDialog(comp, "ยืนยันการ" + bt.getText(), "ยืนยันการพิมพ์", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { printToPrinter(false); } } else if (bt_name.equals("print_all")) { int result = JOptionPane.showConfirmDialog(comp, "ยืนยันการ" + bt.getText(), "ยืนยันการพิมพ์", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { printToPrinter(true); } } else if (bt_name.equals("close_window")) { if (runMode == RunMode.RUNTIME) { applet.getAppletContext().showDocument(new URL("javascript:window.close()")); System.exit(0); } else { ((JFrame) comp).dispose(); } } else if (bt_name.indexOf("page_") == 0) { String[] pp = bt_name.split("_"); int newPage = Integer.parseInt(pp[1]); this.setCurrentPage(newPage); } } catch (Exception ex) { } } }