KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > MessagePrinter


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 JavaDoc;
7 import javax.swing.*;
8 import javax.swing.text.*;
9 import javax.mail.MessagingException JavaDoc;
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 JavaDoc mContentType = null;
24   StringBuffer JavaDoc mMessageText = null;
25
26   /**
27    * This creates a new MessagePrinter for the given MessageInfo.
28    */

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   /**
39    * This calculates the number of pages using the given PageFormat
40    * that it will take to print the message.
41    */

42   public int getPageCount() {
43     return mPageCount;
44   }
45
46   /**
47    * Calculates the page breaks for this component.
48    */

49   public void doPageCalculation(PageFormat pageFormat) {
50     double pageHeight = pageFormat.getImageableHeight();
51     double pageWidth = pageFormat.getImageableWidth();
52
53     java.awt.Dimension JavaDoc minSize = jtp.getMinimumSize();
54
55     double newWidth = Math.max(minSize.getWidth(), pageWidth);
56
57     java.awt.Dimension JavaDoc newSize = new java.awt.Dimension JavaDoc();
58     newSize.setSize(newWidth, jtp.getSize().getHeight());
59     jtp.setSize(newSize);
60
61     if (jtp.getSize().getHeight() < jtp.getPreferredSize().getHeight()) {
62       java.awt.Dimension JavaDoc finalSize = new java.awt.Dimension JavaDoc();
63       finalSize.setSize(jtp.getSize().getWidth(), jtp.getPreferredSize().getHeight());
64       jtp.setSize(finalSize);
65     }
66
67     java.awt.Dimension JavaDoc d = jtp.getSize();
68
69     double panelWidth = d.getWidth();
70     double panelHeight = d.getHeight();
71     
72     jtp.setVisible(true);
73
74     // don't scale below 1.
75
mScale = Math.min(1,pageWidth/panelWidth);
76
77     //mScale = 1;
78

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   /**
94    * Paginates.
95    */

96   public void paginate(double pageHeight) {
97     java.util.List JavaDoc breakList = new java.util.ArrayList JavaDoc();
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     // update the display
116
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 JavaDoc(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 JavaDoc)breakList.get(i)).doubleValue();
135     }
136     
137   }
138
139   /**
140    * Calculates the next pageBreak.
141    */

142   public boolean calculatePageBreak(View view, Shape allocation, Rectangle currentPage) {
143     boolean returnValue = false;
144
145     // only check leaf views--if it's a branch, get the children.
146
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           // don't bother--we're fine.
166
} 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   /**
180    * This actually prints the given page.
181    */

182   public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
183     try {
184       // load the text if we haven't already
185
if (mMessageText == null) {
186     loadText();
187       }
188
189       // create the JTextPage if we haven't already.
190
if (jtp == null) {
191     createTextPane();
192       }
193
194       // paginate.
195
if (mPageBreaks == null) {
196     if (mDisplay != null) {
197       // update the display
198
mDisplay.setStatus(MessagePrinterDisplay.PAGINATING);
199     }
200     doPageCalculation(pageFormat);
201     if (mDisplay != null) {
202       // update the display
203
mDisplay.setStatus(MessagePrinterDisplay.PRINTING);
204     }
205       }
206
207       // if we're done, we're done.
208
if(pageIndex >= mPageCount) {
209     return Printable.NO_SUCH_PAGE;
210       }
211       
212       if (mDisplay != null) {
213     // update the display
214
mDisplay.setCurrentPage(pageIndex + 1);
215       }
216       
217       Graphics2D g2 = (Graphics2D)graphics;
218
219       // only print a single page.
220
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       //shift Graphic to line up with beginning of print-imageable region
226
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
227       
228       //shift Graphic to line up with beginning of next page to print
229
g2.translate(0f, -mPageBreaks[pageIndex]);
230       
231       //scale the page so the width fits...
232
g2.scale(mScale, mScale);
233
234       jtp.print(g2);
235       
236       return Printable.PAGE_EXISTS;
237       
238     } catch (MessagingException JavaDoc me) {
239       me.printStackTrace();
240       return Printable.NO_SUCH_PAGE;
241     }
242   }
243
244   /**
245    * Loads the text and sets the content type.
246    */

247   public void loadText() throws MessagingException JavaDoc {
248     mMessageText = new StringBuffer JavaDoc();
249
250     String JavaDoc content = null;
251     
252     mContentType = "text/plain";
253     
254     boolean displayHtml = false;
255     
256     int msgDisplayMode = message.getMessageProxy().getDisplayMode();
257     
258     // figure out html vs. text
259
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     // if we don't have any html, just display as text.
270
}
271     }
272     
273     // set the content
274
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       // Is there only an HTML part? Regardless, we've determined that
288
// we will still display it as text.
289
if (message.isHtml())
290         content = message.getHtmlAndTextInlines(true, false);
291       else
292         content = message.getTextAndTextInlines(true, false);
293     } else {
294       // Is there only an HTML part? Regardless, we've determined that
295
// we will still display it as text.
296
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   /**
310    * Creates the appropriate JTextPane.
311    */

312   public void createTextPane() {
313     jtp = new JTextPane();
314
315     java.awt.Insets JavaDoc newMargin = new java.awt.Insets JavaDoc(0,0,0,0);
316     jtp.setMargin(newMargin);
317
318     // pull in the correct font.
319
String JavaDoc 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     //jtp.setVisible(true);
337

338   }
339
340   /**
341    * Sets the PrinterDisplay for this MessagePrinter.
342    */

343   public void setDisplay(MessagePrinterDisplay pDisplay) {
344     mDisplay = pDisplay;
345   }
346
347   /**
348    * Returns the JTextPane for this Printer.
349    */

350   public JTextPane getTextPane() {
351     return jtp;
352   }
353 }
354
Popular Tags