KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > print > PagePrinter


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.print;
35
36 import java.util.*;
37 import java.awt.print.*;
38 import java.awt.*;
39 import java.awt.font.*;
40
41 /**
42  * Class which represents a Printable object for a given
43  * page of the print job.
44  *
45  * @version $Id: PagePrinter.java 3901 2006-06-30 05:28:11Z rcartwright $
46  */

47 public class PagePrinter implements Printable {
48
49   // private int _page;
50
private ArrayList<TextLayout> _textLayouts;
51   private ArrayList<TextLayout> _lineNumbers;
52   private String JavaDoc _fileName;
53   private DrJavaBook _parent;
54
55   /**
56    * Constructs a PagePrinter for a given page number (which is ignored!), a
57    * given filename, and parent.
58    */

59   public PagePrinter(int page, String JavaDoc fileName, DrJavaBook parent) {
60     // _page = page;
61
_textLayouts = new ArrayList<TextLayout>();
62     _lineNumbers = new ArrayList<TextLayout>();
63     _fileName = fileName;
64     _parent = parent;
65   }
66
67   /**
68    * Method which adds a TextLayout (and lineNumber) to this
69    * page. This is designed to represent a physical line of
70    * text to be printed on the document (as opposed to a
71    * real line number.
72    * @param text The text of the given line.
73    * @param lineNumber The Text to write in the lineNumber location.
74    */

75   public void add(TextLayout text, TextLayout lineNumber) {
76     _textLayouts.add(text);
77     _lineNumbers.add(lineNumber);
78   }
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   public int print(Graphics graphics, PageFormat format, int pageIndex) {
88     // Set up graphics object
89
Graphics2D g2d = (Graphics2D) graphics;
90     g2d.translate(format.getImageableX(), format.getImageableY());
91     g2d.setPaint(Color.black);
92
93     float y = 0;
94
95     // loop over the TextLayouts, printing out each one and the line number
96
for (int i=0; i<_textLayouts.size(); i++) {
97       TextLayout layout = _textLayouts.get(i);
98       TextLayout lineNumber = _lineNumbers.get(i);
99
100       y += layout.getAscent();
101       lineNumber.draw(g2d, 0, y);
102       layout.draw(g2d, _parent.LINE_NUM_WIDTH, y);
103       y += layout.getLeading();
104     }
105
106     // print the footer
107
printFooter(g2d, format, pageIndex + 1);
108
109     return PAGE_EXISTS;
110   }
111
112   /**
113    * Method which prints the footer onto the document
114    * @param g2d The Graphics2D object to print the footer to.
115    * @param format The PageFormat to use.
116    * @param page The page number to print.
117    */

118   private void printFooter(Graphics2D g2d, PageFormat format, int page) {
119     TextLayout footerFile = new TextLayout(_fileName, DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext());
120     float footerPlace = (float) (format.getImageableWidth() - footerFile.getAdvance()) / 2;
121
122     footerFile.draw(g2d, footerPlace, (float) format.getImageableHeight() - footerFile.getDescent());
123
124     TextLayout footerPageNo = new TextLayout(page + "", DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext());
125     footerPageNo.draw(g2d,
126                       (float) format.getImageableWidth() - footerPageNo.getAdvance(),
127                       (float) format.getImageableHeight() - footerPageNo.getDescent());
128   }
129
130 }
131
Popular Tags