|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PagePrinter.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model.print; | |
| 38 | ||
| 39 | import java.util.*; | |
| 40 | import java.awt.print.*; | |
| 41 | import java.awt.*; | |
| 42 | import java.awt.font.*; | |
| 43 | ||
| 44 | /** | |
| 45 | * Class which represents a Printable object for a given | |
| 46 | * page of the print job. | |
| 47 | * | |
| 48 | * @version $Id: PagePrinter.java 5236 2010-04-27 01:43:36Z mgricken $ | |
| 49 | */ | |
| 50 | public class PagePrinter implements Printable { | |
| 51 | ||
| 52 | // private int _page; | |
| 53 | private ArrayList<TextLayout> _textLayouts; | |
| 54 | private ArrayList<TextLayout> _lineNumbers; | |
| 55 | private String _fileName; | |
| 56 | private DrJavaBook _parent; | |
| 57 | ||
| 58 | /** Constructs a PagePrinter for a given page number (which is ignored!), a | |
| 59 | * given filename, and parent. | |
| 60 | */ | |
| 61 | 3 | public PagePrinter(int page, String fileName, DrJavaBook parent) { |
| 62 | // _page = page; | |
| 63 | 3 | _textLayouts = new ArrayList<TextLayout>(); |
| 64 | 3 | _lineNumbers = new ArrayList<TextLayout>(); |
| 65 | 3 | _fileName = fileName; |
| 66 | 3 | _parent = parent; |
| 67 | } | |
| 68 | ||
| 69 | /** Method which adds a TextLayout (and lineNumber) to this | |
| 70 | * page. This is designed to represent a physical line of | |
| 71 | * text to be printed on the document (as opposed to a | |
| 72 | * real line number. | |
| 73 | * @param text The text of the given line. | |
| 74 | * @param lineNumber The Text to write in the lineNumber location. | |
| 75 | */ | |
| 76 | 3 | public void add(TextLayout text, TextLayout lineNumber) { |
| 77 | 3 | _textLayouts.add(text); |
| 78 | 3 | _lineNumbers.add(lineNumber); |
| 79 | } | |
| 80 | ||
| 81 | /** Method to support the Printable interface. It prints the | |
| 82 | * contents of this PagePrinter onto the Graphics object. | |
| 83 | * @param graphics The Graphics object to print to. | |
| 84 | * @param format The PageFormat to use. | |
| 85 | * @param pageIndex The page number to print. | |
| 86 | */ | |
| 87 | 1 | public int print(Graphics graphics, PageFormat format, int pageIndex) { |
| 88 | // Set up graphics object | |
| 89 | 1 | Graphics2D g2d = (Graphics2D) graphics; |
| 90 | 1 | g2d.translate(format.getImageableX(), format.getImageableY()); |
| 91 | 1 | g2d.setPaint(Color.black); |
| 92 | ||
| 93 | 1 | float y = 0; |
| 94 | ||
| 95 | // loop over the TextLayouts, printing out each one and the line number | |
| 96 | 1 | for (int i = 0; i < _textLayouts.size(); i++) { |
| 97 | 1 | TextLayout layout = _textLayouts.get(i); |
| 98 | 1 | TextLayout lineNumber = _lineNumbers.get(i); |
| 99 | ||
| 100 | 1 | y += layout.getAscent(); |
| 101 | 1 | lineNumber.draw(g2d, 0, y); |
| 102 | 1 | layout.draw(g2d, _parent.LINE_NUM_WIDTH, y); |
| 103 | 1 | y += layout.getLeading(); |
| 104 | } | |
| 105 | ||
| 106 | // print the footer | |
| 107 | 1 | printFooter(g2d, format, pageIndex + 1); |
| 108 | ||
| 109 | 1 | return PAGE_EXISTS; |
| 110 | } | |
| 111 | ||
| 112 | /** Method which prints the footer onto the document | |
| 113 | * @param g2d The Graphics2D object to print the footer to. | |
| 114 | * @param format The PageFormat to use. | |
| 115 | * @param page The page number to print. | |
| 116 | */ | |
| 117 | 1 | private void printFooter(Graphics2D g2d, PageFormat format, int page) { |
| 118 | 1 | TextLayout footerFile = new TextLayout(_fileName, DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext()); |
| 119 | 1 | float footerPlace = (float) (format.getImageableWidth() - footerFile.getAdvance()) / 2; |
| 120 | ||
| 121 | 1 | footerFile.draw(g2d, footerPlace, (float) format.getImageableHeight() - footerFile.getDescent()); |
| 122 | ||
| 123 | 1 | TextLayout footerPageNo = new TextLayout(page + "", DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext()); |
| 124 | 1 | footerPageNo.draw(g2d, |
| 125 | (float) format.getImageableWidth() - footerPageNo.getAdvance(), | |
| 126 | (float) format.getImageableHeight() - footerPageNo.getDescent()); | |
| 127 | } | |
| 128 | ||
| 129 | } |
|
||||||||||