[29] | 1 | /* |
---|
| 2 | * DocumentPrinting.java (original classname: PrintMe) |
---|
| 3 | * (C) Jan Michael Soan (http://it.toolbox.com/wiki/index.php/How_to_print_in_Java) |
---|
| 4 | * (C) 2009 University of Waikato, Hamilton NewZealand |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | package weka.gui; |
---|
| 8 | |
---|
| 9 | import java.awt.Graphics; |
---|
| 10 | import java.awt.Graphics2D; |
---|
| 11 | import java.awt.Rectangle; |
---|
| 12 | import java.awt.Shape; |
---|
| 13 | import java.awt.print.PageFormat; |
---|
| 14 | import java.awt.print.Printable; |
---|
| 15 | import java.awt.print.PrinterException; |
---|
| 16 | import java.awt.print.PrinterJob; |
---|
| 17 | |
---|
| 18 | import javax.swing.JTextPane; |
---|
| 19 | import javax.swing.text.Document; |
---|
| 20 | import javax.swing.text.View; |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * DocumentPrinting is a class that lets you print documents on |
---|
| 24 | * the fly for free ;) Printing in JDK 1.2 - 1.5 is hard. |
---|
| 25 | * With this, you just simply call it in your application |
---|
| 26 | * and add your text component like JTextPane: |
---|
| 27 | * <pre> |
---|
| 28 | * new DocumentPrinting().print(YourJTextComponent); |
---|
| 29 | * </pre> |
---|
| 30 | * Reminder: Just make sure there is a text on your component ;P |
---|
| 31 | * <pre> |
---|
| 32 | * Author : Jan Michael Soan |
---|
| 33 | * WebSite: http://www.jmsoan.com |
---|
| 34 | * Date : 04/17/2004 |
---|
| 35 | * Time : 2:20 PM |
---|
| 36 | * </pre> |
---|
| 37 | * |
---|
| 38 | * Found on <a href="http://it.toolbox.com/wiki/index.php/How_to_print_in_Java" target="_blank">Toolbox</a> |
---|
| 39 | * (<a href="http://www.toolbox.com/TermsofUse.aspx" target="_blank">Terms of Use</a>). |
---|
| 40 | * |
---|
| 41 | * @author Jan Michael Soan (<a href="http://it.toolbox.com/wiki/index.php/How_to_print_in_Java" target="_blank">original code</a>) |
---|
| 42 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
| 43 | */ |
---|
| 44 | public class DocumentPrinting |
---|
| 45 | implements Printable { |
---|
| 46 | |
---|
| 47 | /** the current page. */ |
---|
| 48 | protected int m_CurrentPage = -1; |
---|
| 49 | |
---|
| 50 | /** the JTextPane which content is to be printed. */ |
---|
| 51 | protected JTextPane m_PrintPane; |
---|
| 52 | |
---|
| 53 | /** the page end. */ |
---|
| 54 | protected double m_PageEndY = 0; |
---|
| 55 | |
---|
| 56 | /** the page start. */ |
---|
| 57 | protected double m_PageStartY = 0; |
---|
| 58 | |
---|
| 59 | /** whether to scale the width to fit. */ |
---|
| 60 | protected boolean m_ScaleWidthToFit = true; |
---|
| 61 | |
---|
| 62 | /** the pageformat. */ |
---|
| 63 | protected PageFormat m_PageFormat; |
---|
| 64 | |
---|
| 65 | /** the printer job. */ |
---|
| 66 | protected PrinterJob m_PrinterJob; |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Initializes the printing. |
---|
| 70 | */ |
---|
| 71 | public DocumentPrinting() { |
---|
| 72 | m_PrintPane = new JTextPane(); |
---|
| 73 | m_PageFormat = new PageFormat(); |
---|
| 74 | m_PrinterJob = PrinterJob.getPrinterJob(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Brings up the page dialog. |
---|
| 79 | */ |
---|
| 80 | protected void pageDialog() { |
---|
| 81 | m_PageFormat = m_PrinterJob.pageDialog(m_PageFormat); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Prints the page. |
---|
| 86 | * |
---|
| 87 | * @param graphics the graphics context |
---|
| 88 | * @param pageFormat the format of the page |
---|
| 89 | * @param pageIndex the page index |
---|
| 90 | * @return either NO_SUCH_PAGE or PAGE_EXISTS |
---|
| 91 | * @see Printable#NO_SUCH_PAGE |
---|
| 92 | * @see Printable#PAGE_EXISTS |
---|
| 93 | */ |
---|
| 94 | public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { |
---|
| 95 | double scale = 1.0; |
---|
| 96 | Graphics2D graphics2D; |
---|
| 97 | View rootView; |
---|
| 98 | |
---|
| 99 | graphics2D = (Graphics2D) graphics; |
---|
| 100 | m_PrintPane.setSize((int) pageFormat.getImageableWidth(),Integer.MAX_VALUE); |
---|
| 101 | m_PrintPane.validate(); |
---|
| 102 | |
---|
| 103 | rootView = m_PrintPane.getUI().getRootView(m_PrintPane); |
---|
| 104 | |
---|
| 105 | if ((m_ScaleWidthToFit) && (m_PrintPane.getMinimumSize().getWidth() > pageFormat.getImageableWidth())) { |
---|
| 106 | scale = pageFormat.getImageableWidth()/ |
---|
| 107 | m_PrintPane.getMinimumSize().getWidth(); |
---|
| 108 | graphics2D.scale(scale,scale); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | graphics2D.setClip( |
---|
| 112 | (int) (pageFormat.getImageableX()/scale), |
---|
| 113 | (int) (pageFormat.getImageableY()/scale), |
---|
| 114 | (int) (pageFormat.getImageableWidth()/scale), |
---|
| 115 | (int) (pageFormat.getImageableHeight()/scale)); |
---|
| 116 | |
---|
| 117 | if (pageIndex > m_CurrentPage) { |
---|
| 118 | m_CurrentPage = pageIndex; |
---|
| 119 | m_PageStartY += m_PageEndY; |
---|
| 120 | m_PageEndY = graphics2D.getClipBounds().getHeight(); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | graphics2D.translate( |
---|
| 124 | graphics2D.getClipBounds().getX(), |
---|
| 125 | graphics2D.getClipBounds().getY()); |
---|
| 126 | Rectangle allocation = new Rectangle( |
---|
| 127 | 0, |
---|
| 128 | (int) -m_PageStartY, |
---|
| 129 | (int) (m_PrintPane.getMinimumSize().getWidth()), |
---|
| 130 | (int) (m_PrintPane.getPreferredSize().getHeight())); |
---|
| 131 | |
---|
| 132 | if (printView(graphics2D,allocation,rootView)) { |
---|
| 133 | return Printable.PAGE_EXISTS; |
---|
| 134 | } |
---|
| 135 | else { |
---|
| 136 | m_PageStartY = 0; |
---|
| 137 | m_PageEndY = 0; |
---|
| 138 | m_CurrentPage = -1; |
---|
| 139 | return Printable.NO_SUCH_PAGE; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Prints the document in the JTextPane. |
---|
| 145 | * |
---|
| 146 | * @param pane the document to print |
---|
| 147 | */ |
---|
| 148 | public void print(JTextPane pane) { |
---|
| 149 | setDocument(pane); |
---|
| 150 | printDialog(); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Shows the print dialog. |
---|
| 155 | */ |
---|
| 156 | public void printDialog() { |
---|
| 157 | if (m_PrinterJob.printDialog()) { |
---|
| 158 | m_PrinterJob.setPrintable(this,m_PageFormat); |
---|
| 159 | try { |
---|
| 160 | m_PrinterJob.print(); |
---|
| 161 | } |
---|
| 162 | catch (PrinterException printerException) { |
---|
| 163 | m_PageStartY = 0; |
---|
| 164 | m_PageEndY = 0; |
---|
| 165 | m_CurrentPage = -1; |
---|
| 166 | System.out.println("Error Printing Document"); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Shows a print view. |
---|
| 173 | * |
---|
| 174 | * @param graphics2D the graphics context |
---|
| 175 | * @param allocation the allocation |
---|
| 176 | * @param view the view |
---|
| 177 | * @return true if the page exists |
---|
| 178 | */ |
---|
| 179 | protected boolean printView(Graphics2D graphics2D, Shape allocation, View view) { |
---|
| 180 | boolean pageExists = false; |
---|
| 181 | Rectangle clipRectangle = graphics2D.getClipBounds(); |
---|
| 182 | Shape childAllocation; |
---|
| 183 | View childView; |
---|
| 184 | |
---|
| 185 | if (view.getViewCount() > 0) { |
---|
| 186 | for (int i = 0; i < view.getViewCount(); i++) { |
---|
| 187 | childAllocation = view.getChildAllocation(i,allocation); |
---|
| 188 | if (childAllocation != null) { |
---|
| 189 | childView = view.getView(i); |
---|
| 190 | if (printView(graphics2D,childAllocation,childView)) { |
---|
| 191 | pageExists = true; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | } else { |
---|
| 196 | if (allocation.getBounds().getMaxY() >= clipRectangle.getY()) { |
---|
| 197 | pageExists = true; |
---|
| 198 | if ((allocation.getBounds().getHeight() > clipRectangle.getHeight()) && |
---|
| 199 | (allocation.intersects(clipRectangle))) { |
---|
| 200 | view.paint(graphics2D,allocation); |
---|
| 201 | } else { |
---|
| 202 | if (allocation.getBounds().getY() >= clipRectangle.getY()) { |
---|
| 203 | if (allocation.getBounds().getMaxY() <= clipRectangle.getMaxY()) { |
---|
| 204 | view.paint(graphics2D,allocation); |
---|
| 205 | } else { |
---|
| 206 | if (allocation.getBounds().getY() < m_PageEndY) { |
---|
| 207 | m_PageEndY = allocation.getBounds().getY(); |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | return pageExists; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * Sets the content type. |
---|
| 219 | * |
---|
| 220 | * @param type the content type |
---|
| 221 | */ |
---|
| 222 | public void setContentType(String type) { |
---|
| 223 | m_PrintPane.setContentType(type); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | /** |
---|
| 227 | * Returns the document to print. |
---|
| 228 | * |
---|
| 229 | * @return the document or null |
---|
| 230 | */ |
---|
| 231 | public Document getDocument() { |
---|
| 232 | if (m_PrintPane != null) |
---|
| 233 | return m_PrintPane.getDocument(); |
---|
| 234 | else |
---|
| 235 | return null; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Sets the document from the given JTextPane. |
---|
| 240 | * |
---|
| 241 | * @param pane the JTextPane to get the document from |
---|
| 242 | */ |
---|
| 243 | public void setDocument(JTextPane pane) { |
---|
| 244 | m_PrintPane = new JTextPane(); |
---|
| 245 | setDocument(pane.getContentType(), pane.getDocument()); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * Sets the document and the according content type. |
---|
| 250 | * |
---|
| 251 | * @param type the content type |
---|
| 252 | * @param document the document to print |
---|
| 253 | */ |
---|
| 254 | public void setDocument(String type, Document document) { |
---|
| 255 | setContentType(type); |
---|
| 256 | m_PrintPane.setDocument(document); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | * Sets whether to scale the width to fit. |
---|
| 261 | * |
---|
| 262 | * @param scaleWidth if true then the width will be scaled |
---|
| 263 | */ |
---|
| 264 | public void setScaleWidthToFit(boolean scaleWidth) { |
---|
| 265 | m_ScaleWidthToFit = scaleWidth; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | /** |
---|
| 269 | * Returns whether the width is to be scaled. |
---|
| 270 | * |
---|
| 271 | * @return true if scaled |
---|
| 272 | */ |
---|
| 273 | public boolean getScaleWidthToFit() { |
---|
| 274 | return m_ScaleWidthToFit; |
---|
| 275 | } |
---|
| 276 | } |
---|