001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.print;
038:
039: import java.util.*;
040: import java.awt.print.*;
041: import java.awt.*;
042: import java.awt.font.*;
043:
044: /**
045: * Class which represents a Printable object for a given
046: * page of the print job.
047: *
048: * @version $Id: PagePrinter.java 4255 2007-08-28 19:17:37Z mgricken $
049: */
050: public class PagePrinter implements Printable {
051:
052: // private int _page;
053: private ArrayList<TextLayout> _textLayouts;
054: private ArrayList<TextLayout> _lineNumbers;
055: private String _fileName;
056: private DrJavaBook _parent;
057:
058: /**
059: * Constructs a PagePrinter for a given page number (which is ignored!), a
060: * given filename, and parent.
061: */
062: public PagePrinter(int page, String fileName, DrJavaBook parent) {
063: // _page = page;
064: _textLayouts = new ArrayList<TextLayout>();
065: _lineNumbers = new ArrayList<TextLayout>();
066: _fileName = fileName;
067: _parent = parent;
068: }
069:
070: /**
071: * Method which adds a TextLayout (and lineNumber) to this
072: * page. This is designed to represent a physical line of
073: * text to be printed on the document (as opposed to a
074: * real line number.
075: * @param text The text of the given line.
076: * @param lineNumber The Text to write in the lineNumber location.
077: */
078: public void add(TextLayout text, TextLayout lineNumber) {
079: _textLayouts.add(text);
080: _lineNumbers.add(lineNumber);
081: }
082:
083: /**
084: * Method to support the Printable interface. It prints the
085: * contents of this PagePrinter onto the Graphics object.
086: * @param graphics The Graphics object to print to.
087: * @param format The PageFormat to use.
088: * @param pageIndex The page number to print.
089: */
090: public int print(Graphics graphics, PageFormat format, int pageIndex) {
091: // Set up graphics object
092: Graphics2D g2d = (Graphics2D) graphics;
093: g2d.translate(format.getImageableX(), format.getImageableY());
094: g2d.setPaint(Color.black);
095:
096: float y = 0;
097:
098: // loop over the TextLayouts, printing out each one and the line number
099: for (int i = 0; i < _textLayouts.size(); i++) {
100: TextLayout layout = _textLayouts.get(i);
101: TextLayout lineNumber = _lineNumbers.get(i);
102:
103: y += layout.getAscent();
104: lineNumber.draw(g2d, 0, y);
105: layout.draw(g2d, _parent.LINE_NUM_WIDTH, y);
106: y += layout.getLeading();
107: }
108:
109: // print the footer
110: printFooter(g2d, format, pageIndex + 1);
111:
112: return PAGE_EXISTS;
113: }
114:
115: /**
116: * Method which prints the footer onto the document
117: * @param g2d The Graphics2D object to print the footer to.
118: * @param format The PageFormat to use.
119: * @param page The page number to print.
120: */
121: private void printFooter(Graphics2D g2d, PageFormat format, int page) {
122: TextLayout footerFile = new TextLayout(_fileName,
123: DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext());
124: float footerPlace = (float) (format.getImageableWidth() - footerFile
125: .getAdvance()) / 2;
126:
127: footerFile.draw(g2d, footerPlace, (float) format
128: .getImageableHeight()
129: - footerFile.getDescent());
130:
131: TextLayout footerPageNo = new TextLayout(page + "",
132: DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext());
133: footerPageNo.draw(g2d, (float) format.getImageableWidth()
134: - footerPageNo.getAdvance(), (float) format
135: .getImageableHeight()
136: - footerPageNo.getDescent());
137: }
138:
139: }
|