Handler for File → Print.
()
| 2733 | * Handler for File → Print. |
| 2734 | */ |
| 2735 | public void handlePrint() { |
| 2736 | statusNotice(Language.text("editor.status.printing")); |
| 2737 | |
| 2738 | StringBuilder html = new StringBuilder("<html><body>"); |
| 2739 | for (SketchCode tab : sketch.getCode()) { |
| 2740 | html.append("<b>" + tab.getPrettyName() + "</b><br>"); |
| 2741 | html.append(textarea.getTextAsHtml((SyntaxDocument)tab.getDocument())); |
| 2742 | html.append("<br>"); |
| 2743 | } |
| 2744 | html.setLength(html.length() - 4); // Don't want last <br>. |
| 2745 | html.append("</body></html>"); |
| 2746 | JTextPane jtp = new JTextPane(); |
| 2747 | // Needed for good line wrapping; otherwise one very long word breaks |
| 2748 | // wrapping for the whole document. |
| 2749 | jtp.setEditorKit(new HTMLEditorKit() { |
| 2750 | public ViewFactory getViewFactory() { |
| 2751 | return new HTMLFactory() { |
| 2752 | public View create(Element e) { |
| 2753 | View v = super.create(e); |
| 2754 | if (!(v instanceof javax.swing.text.html.ParagraphView)) |
| 2755 | return v; |
| 2756 | else |
| 2757 | return new javax.swing.text.html.ParagraphView(e) { |
| 2758 | protected SizeRequirements calculateMinorAxisRequirements( |
| 2759 | int axis, SizeRequirements r) { |
| 2760 | r = super.calculateMinorAxisRequirements(axis, r); |
| 2761 | r.minimum = 1; |
| 2762 | return r; |
| 2763 | } |
| 2764 | }; |
| 2765 | } |
| 2766 | }; |
| 2767 | } |
| 2768 | }); |
| 2769 | jtp.setFont(new Font(Preferences.get("editor.font.family"), Font.PLAIN, 10)); |
| 2770 | jtp.setText(html.toString().replace("\n", "<br>") // Not in a <pre>. |
| 2771 | .replaceAll("(?<! ) ", " ")); // Allow line wrap. |
| 2772 | |
| 2773 | //printerJob = null; |
| 2774 | if (printerJob == null) { |
| 2775 | printerJob = PrinterJob.getPrinterJob(); |
| 2776 | } |
| 2777 | if (pageFormat != null) { |
| 2778 | //System.out.println("setting page format " + pageFormat); |
| 2779 | printerJob.setPrintable(jtp.getPrintable(null, null), pageFormat); |
| 2780 | } else { |
| 2781 | printerJob.setPrintable(jtp.getPrintable(null, null)); |
| 2782 | } |
| 2783 | // set the name of the job to the code name |
| 2784 | printerJob.setJobName(sketch.getCurrentCode().getPrettyName()); |
| 2785 | |
| 2786 | if (printerJob.printDialog()) { |
| 2787 | try { |
| 2788 | printerJob.print(); |
| 2789 | statusNotice(Language.text("editor.status.printing.done")); |
| 2790 | |
| 2791 | } catch (PrinterException pe) { |
| 2792 | statusError(Language.text("editor.status.printing.error")); |
no test coverage detected