1 package org.columba.core.print; 17 18 import java.awt.print.Book ; 19 import java.awt.print.Paper ; 20 import java.awt.print.PrinterException ; 21 import java.awt.print.PrinterJob ; 22 import java.util.Enumeration ; 23 import java.util.List ; 24 import java.util.Vector ; 25 26 27 public class cDocument { 28 private List objects; 29 private List pages; 30 private cPrintObject header; 31 private cPrintObject footer; 32 private String docName; 33 private boolean uptodate; 34 private PrinterJob printJob; 35 36 public cDocument() { 37 objects = new Vector (); 38 pages = new Vector (); 39 uptodate = false; 40 41 printJob = PrinterJob.getPrinterJob(); 42 } 43 44 public int getPageCount() { 45 if (!uptodate) { 46 createPages(); 47 } 48 49 return pages.size(); 50 } 51 52 public void print() { 53 if (!uptodate) { 54 createPages(); 55 } 56 57 print(1, getPageCount()); 58 } 59 60 public void print(int startPage, int endPage) { 61 if (!uptodate) { 62 createPages(); 63 } 64 65 if (docName != null) { 66 printJob.setJobName(docName); 67 } 68 69 Book book = new Book (); 70 71 for (int i = 0; i < endPage; i++) { 72 book.append((cPage) pages.get(i), printJob.defaultPage()); 73 } 74 75 printJob.setPageable(book); 76 77 if (printJob.printDialog()) { 78 try { 79 printJob.print(); 80 } catch (PrinterException e) { 81 e.printStackTrace(); 82 } 83 } 84 } 85 86 public void setHeader(cPrintObject h) { 87 header = h; 88 h.setType(cPrintObject.HEADER); 89 } 90 91 public cPrintObject getHeader() { 92 return header; 93 } 94 95 public cPrintObject getFooter() { 96 return footer; 97 } 98 99 public void setFooter(cPrintObject f) { 100 footer = f; 101 f.setType(cPrintObject.FOOTER); 102 } 103 104 public void setDocumentName(String n) { 105 docName = n; 106 } 107 108 public int getPageNr(cPage p) { 109 if (!uptodate) { 110 createPages(); 111 } 112 113 return pages.indexOf(p) + 1; 114 } 115 116 public void appendPrintObject(cPrintObject obj) { 117 objects.add(obj); 118 uptodate = false; 119 } 120 121 private void createPages() { 122 pages.clear(); 123 124 Enumeration objEnum = ((Vector ) objects).elements(); 125 126 Paper paper = printJob.defaultPage().getPaper(); 127 128 cCmUnit pWidth = new cCmUnit(); 129 pWidth.setPoints(paper.getImageableWidth()); 130 131 cCmUnit pHeight = new cCmUnit(); 132 pHeight.setPoints(paper.getImageableHeight()); 133 134 if (getHeader() != null) { 135 pHeight.subI(getHeader().getSize(pWidth).getHeight()); 136 } 137 138 if (getFooter() != null) { 139 pHeight.subI(getFooter().getSize(pWidth).getHeight()); 140 } 141 142 cUnit remainHeight; 143 cPrintObject remainObj; 144 145 cPrintObject nextObj; 146 cUnit objHeight; 147 148 cPage nPage = new cPage(this); 149 remainHeight = new cPointUnit(pHeight.getPoints()); 150 151 cUnit hLocation = new cCmUnit(); 152 153 nextObj = (cPrintObject) objEnum.nextElement(); 154 155 while (true) { 156 objHeight = nextObj.getSize(pWidth).getHeight(); 157 158 if (objHeight.getPoints() <= remainHeight.getPoints()) { 159 nextObj.setLocation(new cPoint(new cCmUnit(), 160 new cCmUnit(hLocation))); 161 remainHeight.setPoints(remainHeight.sub(objHeight).getPoints()); 162 hLocation.setPoints(hLocation.add(objHeight).getPoints()); 163 nPage.add(nextObj); 164 165 if (objEnum.hasMoreElements()) { 166 nextObj = (cPrintObject) objEnum.nextElement(); 167 } else { 168 break; 169 } 170 } else { 171 remainObj = nextObj.breakBlock(pWidth, remainHeight); 172 173 if (remainObj != null) { 174 remainObj.setLocation(new cPoint(new cCmUnit(), 175 new cCmUnit(hLocation))); 176 nPage.add(remainObj); 177 } 178 179 pages.add(nPage); 180 nPage = new cPage(this); 181 remainHeight.setPoints(pHeight.getPoints()); 182 hLocation.setUnits(0); 183 } 184 } 185 186 if (nPage.countObjects() != 0) { 187 pages.add(nPage); 188 } 189 190 uptodate = true; 191 } 192 } 193 | Popular Tags |