KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > layout > swing > SwingPrintBook


1 package jimm.datavision.layout.swing;
2 import jimm.datavision.UserCancellationException;
3 import jimm.datavision.gui.StatusDialog;
4 import jimm.util.I18N;
5 import java.awt.print.*;
6 import java.util.List JavaDoc;
7
8 /**
9  * Returns printable pages to a print job.
10  */

11 class SwingPrintBook implements Pageable {
12
13 protected StatusDialog statusDialog;
14 protected List JavaDoc pageContents;
15 protected PageFormat pageFormat;
16 protected boolean wasBuiltForUs;
17
18 SwingPrintBook(List JavaDoc pages, PageFormat format) {
19     pageContents = pages;
20     pageFormat = format;
21 }
22
23 void setStatusDialog(StatusDialog statusDialog) {
24     this.statusDialog = statusDialog;
25 }
26
27 public int getNumberOfPages() {
28     return pageContents.size();
29 }
30
31 public PageFormat getPageFormat(int pageIndex) {
32     return pageFormat;
33 }
34
35 /**
36  * Returns the specified swing page. If the page has already been constructed
37  * for display, we return that page. If it has not, we create and return
38  * a new, temporary page.
39  */

40 public Printable getPrintable(int pageIndex) {
41     SwingPageContents contents;
42
43     // "Forget" previous page if it was built 'specially for this print job.
44
if (wasBuiltForUs) {
45     contents = (SwingPageContents)pageContents.get(pageIndex - 1);
46     contents.forgetPage();
47     }
48
49     // If user cancelled, tell the rest of the world.
50
if (statusDialog.isCancelled()) {
51     throw new UserCancellationException();
52     }
53     statusDialog.update(I18N.get("SwingPrintBook.printing_page")
54             + ' ' + (pageIndex + 1) + ' '
55             + I18N.get("SwingPrintBook.of") + ' '
56             + getNumberOfPages());
57
58     contents = (SwingPageContents)pageContents.get(pageIndex);
59     Printable page = null;
60
61     page = contents.getPage();
62     wasBuiltForUs = !contents.isPageBuilt();
63
64     return page;
65 }
66
67 }
68
Popular Tags