1 package net.suberic.pooka.gui; 2 import net.suberic.pooka.*; 3 import java.util.*; 4 import java.awt.print.*; 5 import java.awt.*; 6 import javax.mail.internet.MimeMessage ; 7 import javax.swing.*; 8 import javax.swing.text.*; 9 import javax.mail.MessagingException ; 10 11 public class MessagePrinter implements Printable { 12 13 private MessageInfo message; 14 private int offset; 15 private MessagePrinterDisplay mDisplay = null; 16 17 JTextPane jtp = null; 18 19 int mPageCount = 0; 20 double[] mPageBreaks = null; 21 double mScale = 1; 22 23 String mContentType = null; 24 StringBuffer mMessageText = null; 25 26 29 public MessagePrinter(MessageInfo mi, int newOffset) { 30 message = mi; 31 offset = newOffset; 32 } 33 34 public MessagePrinter(MessageInfo mi) { 35 this(mi, 0); 36 } 37 38 42 public int getPageCount() { 43 return mPageCount; 44 } 45 46 49 public void doPageCalculation(PageFormat pageFormat) { 50 double pageHeight = pageFormat.getImageableHeight(); 51 double pageWidth = pageFormat.getImageableWidth(); 52 53 java.awt.Dimension minSize = jtp.getMinimumSize(); 54 55 double newWidth = Math.max(minSize.getWidth(), pageWidth); 56 57 java.awt.Dimension newSize = new java.awt.Dimension (); 58 newSize.setSize(newWidth, jtp.getSize().getHeight()); 59 jtp.setSize(newSize); 60 61 if (jtp.getSize().getHeight() < jtp.getPreferredSize().getHeight()) { 62 java.awt.Dimension finalSize = new java.awt.Dimension (); 63 finalSize.setSize(jtp.getSize().getWidth(), jtp.getPreferredSize().getHeight()); 64 jtp.setSize(finalSize); 65 } 66 67 java.awt.Dimension d = jtp.getSize(); 68 69 double panelWidth = d.getWidth(); 70 double panelHeight = d.getHeight(); 71 72 jtp.setVisible(true); 73 74 mScale = Math.min(1,pageWidth/panelWidth); 76 77 79 int counter = 0; 80 81 paginate(pageHeight); 82 83 mPageCount = mPageBreaks.length; 84 85 if (mDisplay != null) { 86 mDisplay.setPageCount(mPageCount); 87 } 88 89 } 90 91 double pageEnd = 0; 92 93 96 public void paginate(double pageHeight) { 97 java.util.List breakList = new java.util.ArrayList (); 98 99 boolean pageExists = true; 100 double pageStart = 0; 101 pageEnd = 0; 102 103 double scaledPageHeight = pageHeight/mScale; 104 105 jtp.validate(); 106 107 View view = jtp.getUI().getRootView(jtp); 108 109 double pageWidth = jtp.getSize().getWidth(); 110 111 Rectangle allocation = new Rectangle(0, 0, jtp.getSize().width, jtp.getSize().height); 112 113 while (pageExists) { 114 if (mDisplay != null) { 115 mDisplay.setCurrentPage(breakList.size()); 117 } 118 119 pageStart = pageEnd; 120 pageEnd = pageStart + scaledPageHeight; 121 122 Rectangle currentPage = new Rectangle(); 123 currentPage.setRect(0d, pageStart, pageWidth, scaledPageHeight); 124 125 pageExists = calculatePageBreak(view, allocation, currentPage); 126 127 if (pageExists) { 128 breakList.add(new Double (pageStart * mScale)); 129 } 130 } 131 132 mPageBreaks = new double[breakList.size()]; 133 for (int i = 0; i < mPageBreaks.length; i++) { 134 mPageBreaks[i] = (double) ((Double )breakList.get(i)).doubleValue(); 135 } 136 137 } 138 139 142 public boolean calculatePageBreak(View view, Shape allocation, Rectangle currentPage) { 143 boolean returnValue = false; 144 145 if (view.getViewCount() > 0) { 147 for (int i = 0; i < view.getViewCount(); i++) { 148 Shape childAllocation = view.getChildAllocation(i,allocation); 149 if (childAllocation != null) { 150 View childView = view.getView(i); 151 if (calculatePageBreak(childView,childAllocation,currentPage)) { 152 returnValue = true; 153 } 154 } 155 } 156 } else { 157 if (allocation.getBounds().getMaxY() >= currentPage.getY()) { 158 returnValue = true; 159 if ((allocation.getBounds().getHeight() > currentPage.getHeight()) && 160 (allocation.intersects(currentPage))) { 161 } else { 162 if (allocation.getBounds().getY() >= currentPage.getY() && allocation.getBounds().getY() < pageEnd) { 163 164 if (allocation.getBounds().getMaxY() <= pageEnd) { 165 } else { 167 if (allocation.getBounds().getY() < pageEnd) { 168 pageEnd = allocation.getBounds().getY(); 169 } 170 } 171 } 172 } 173 } 174 } 175 176 return returnValue; 177 } 178 179 182 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { 183 try { 184 if (mMessageText == null) { 186 loadText(); 187 } 188 189 if (jtp == null) { 191 createTextPane(); 192 } 193 194 if (mPageBreaks == null) { 196 if (mDisplay != null) { 197 mDisplay.setStatus(MessagePrinterDisplay.PAGINATING); 199 } 200 doPageCalculation(pageFormat); 201 if (mDisplay != null) { 202 mDisplay.setStatus(MessagePrinterDisplay.PRINTING); 204 } 205 } 206 207 if(pageIndex >= mPageCount) { 209 return Printable.NO_SUCH_PAGE; 210 } 211 212 if (mDisplay != null) { 213 mDisplay.setCurrentPage(pageIndex + 1); 215 } 216 217 Graphics2D g2 = (Graphics2D)graphics; 218 219 if (pageIndex + 1 < mPageCount) { 221 Rectangle origClip = g2.getClipBounds(); 222 g2.clipRect(g2.getClipBounds().x, g2.getClipBounds().y, g2.getClipBounds().width, (int) (mPageBreaks[pageIndex + 1] - mPageBreaks[pageIndex])); 223 } 224 225 g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 227 228 g2.translate(0f, -mPageBreaks[pageIndex]); 230 231 g2.scale(mScale, mScale); 233 234 jtp.print(g2); 235 236 return Printable.PAGE_EXISTS; 237 238 } catch (MessagingException me) { 239 me.printStackTrace(); 240 return Printable.NO_SUCH_PAGE; 241 } 242 } 243 244 247 public void loadText() throws MessagingException { 248 mMessageText = new StringBuffer (); 249 250 String content = null; 251 252 mContentType = "text/plain"; 253 254 boolean displayHtml = false; 255 256 int msgDisplayMode = message.getMessageProxy().getDisplayMode(); 257 258 if (Pooka.getProperty("Pooka.displayHtml", "").equalsIgnoreCase("true")) { 260 if (message.isHtml()) { 261 if (msgDisplayMode > MessageProxy.TEXT_ONLY) 262 displayHtml = true; 263 264 } else if (message.containsHtml()) { 265 if (msgDisplayMode >= MessageProxy.HTML_PREFERRED) 266 displayHtml = true; 267 268 } else { 269 } 271 } 272 273 if (msgDisplayMode == MessageProxy.RFC_822) { 275 content = message.getRawText(); 276 } else { 277 if (displayHtml) { 278 mContentType = "text/html"; 279 280 if (Pooka.getProperty("Pooka.displayTextAttachments", "").equalsIgnoreCase("true")) { 281 content = message.getHtmlAndTextInlines(true, false); 282 } else { 283 content = message.getHtmlPart(true, false); 284 } 285 } else { 286 if (Pooka.getProperty("Pooka.displayTextAttachments", "").equalsIgnoreCase("true")) { 287 if (message.isHtml()) 290 content = message.getHtmlAndTextInlines(true, false); 291 else 292 content = message.getTextAndTextInlines(true, false); 293 } else { 294 if (message.isHtml()) 297 content = message.getHtmlPart(true, false); 298 else 299 content = message.getTextPart(true, false); 300 } 301 } 302 } 303 304 if (content != null) 305 mMessageText.append(content); 306 307 } 308 309 312 public void createTextPane() { 313 jtp = new JTextPane(); 314 315 java.awt.Insets newMargin = new java.awt.Insets (0,0,0,0); 316 jtp.setMargin(newMargin); 317 318 String fontName = Pooka.getProperty("MessageWindow.editorPane.printing.font.name", "monospaced"); 320 int fontSize = Integer.parseInt(Pooka.getProperty("MessageWindow.editorPane.printint.font.size", "10")); 321 322 Font f = new Font(fontName, Font.PLAIN, fontSize); 323 324 if (f != null) 325 jtp.setFont(f); 326 327 jtp.validate(); 328 329 jtp.setContentType(mContentType); 330 jtp.setText(mMessageText.toString()); 331 332 jtp.setEditable(false); 333 334 jtp.setSize(jtp.getPreferredSize()); 335 336 338 } 339 340 343 public void setDisplay(MessagePrinterDisplay pDisplay) { 344 mDisplay = pDisplay; 345 } 346 347 350 public JTextPane getTextPane() { 351 return jtp; 352 } 353 } 354 | Popular Tags |