001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.print;
010:
011: import java.awt.Color;
012: import java.awt.Font;
013: import java.awt.FontMetrics;
014: import java.awt.Graphics;
015: import java.awt.print.*;
016: import java.text.DateFormat;
017: import java.util.Date;
018: import org.acm.seguin.util.FileSettings;
019: import org.acm.seguin.util.MissingSettingsException;
020: import org.acm.seguin.awt.ExceptionPrinter;
021:
022: /**
023: * Handles printing the page
024: *
025: *@author Chris Seguin
026: *@created August 8, 1999
027: */
028: public abstract class PagePrinter implements Printable {
029: private int filenameFontSize = 14;
030: private int datePageFontSize = 8;
031:
032: /**
033: * Description of the Field
034: */
035: protected static int headerHeight = 30;
036: /**
037: * Description of the Field
038: */
039: protected static PageFormat pf;
040: private static double scale = 1.0;
041:
042: /**
043: * Description of the Method
044: *
045: *@param value Description of Parameter
046: */
047: public void setFilenameFontSize(int value) {
048: filenameFontSize = value;
049: }
050:
051: /**
052: * Description of the Method
053: *
054: *@param value Description of Parameter
055: */
056: public void setDatePageCountFontSize(int value) {
057: datePageFontSize = value;
058: }
059:
060: /**
061: * Prints the header at the top of the page
062: *
063: *@param g The graphics object
064: *@param title the title
065: *@param pageNumber the number of pages
066: *@param pageCount the page count
067: */
068: protected void printHeader(Graphics g, String title,
069: String pageNumber, String pageCount) {
070: // Draw the frame
071: int x = (int) pf.getImageableX();
072: int y = (int) pf.getImageableY();
073: int wide = (int) pf.getImageableWidth();
074: int high = headerHeight;
075: g.setColor(Color.white);
076: g.fillRect(x, y, wide - 1, high - 1);
077: g.setColor(Color.black);
078: g.drawRect(x, y, wide - 1, high - 1);
079: int quarterWide = wide / 4;
080: g.drawLine(x + 2 * quarterWide, y, x + 2 * quarterWide, y
081: + headerHeight - 1);
082: g.drawLine(x + 3 * quarterWide, y, x + 3 * quarterWide, y
083: + headerHeight - 1);
084:
085: int centerY = y + headerHeight / 2;
086:
087: // Draw the filename
088: g.setFont(new Font("Serif", Font.BOLD, filenameFontSize));
089: FontMetrics fm = g.getFontMetrics();
090: int tempY = y
091: + (headerHeight + fm.getAscent() + fm.getDescent()) / 2
092: - fm.getDescent();
093: if ((title != null) && (title.length() > 0)) {
094: g.drawString(title, x + 10, tempY);
095: }
096:
097: // Draw the date
098: g.setFont(new Font("Serif", Font.BOLD, datePageFontSize));
099: fm = g.getFontMetrics();
100:
101: String now = DateFormat.getDateTimeInstance()
102: .format(new Date());
103: tempY = y + (headerHeight + fm.getAscent() + fm.getDescent())
104: / 2 - fm.getDescent();
105: g.drawString(now, x + 5 * quarterWide / 2 - fm.stringWidth(now)
106: / 2, tempY);
107:
108: // Draw the page count
109: String pages = pageNumber + " of " + pageCount;
110: g.drawString(pages, x + 7 * quarterWide / 2
111: - fm.stringWidth(pages) / 2, tempY);
112: }
113:
114: /**
115: * Sets the size of the header box
116: *
117: *@param value The size of the header box
118: */
119: public static void setHeaderHeight(int value) {
120: headerHeight = value;
121: }
122:
123: /**
124: * Returns the page
125: *
126: *@param dialog present a dialog screen if none
127: *@return the current page format
128: */
129: public static PageFormat getPageFormat(boolean dialog) {
130: if (dialog) {
131: PrinterJob job = PrinterJob.getPrinterJob();
132: pf = job.pageDialog(job.defaultPage());
133: }
134:
135: // Get the header height
136: try {
137: FileSettings bundle = FileSettings
138: .getRefactorySettings("printing");
139: setHeaderHeight(Integer.parseInt(bundle
140: .getString("header.space")));
141: } catch (MissingSettingsException mre) {
142: ExceptionPrinter.print(mre, true);
143: } catch (NumberFormatException inf) {
144: ExceptionPrinter.print(inf, true);
145: }
146:
147: return pf;
148: }
149:
150: /**
151: * Return the width of the page
152: *
153: *@return Description of the Returned Value
154: */
155: public static int getPageWidth() {
156: if (pf == null) {
157: return -1;
158: }
159:
160: return (int) (pf.getImageableWidth() / scale);
161: }
162:
163: /**
164: * Return the width of the page
165: *
166: *@return Description of the Returned Value
167: */
168: public static int getPageHeight() {
169: if (pf == null) {
170: return -1;
171: }
172:
173: return (int) (pf.getImageableHeight() / scale);
174: }
175:
176: /**
177: * Sets the scaling
178: *
179: *@param value the scaled value
180: */
181: protected static void setScale(double value) {
182: scale = value;
183: }
184:
185: /**
186: * Returns the scaling
187: *
188: *@return The scale size
189: */
190: protected static double getScale() {
191: return scale;
192: }
193: }
|