KickJava   Java API By Example, From Geeks To Geeks.

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


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-2005 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 import java.text.*;
41
42 /**
43  * The DrJavaBook class is DrJava's implementation of a Pageable object. It
44  * serves as the control class for printing, and is responsible for
45  * preparing the print job of previewing or printing given the String
46  * representation of the document.
47  *
48  * @version $Id: DrJavaBook.java 4055 2007-01-15 22:25:53Z c45207 $
49  */

50 public class DrJavaBook implements Pageable {
51
52   private ArrayList<PagePrinter> _pagePrinters;
53   private PageFormat _format;
54   private String JavaDoc _fileName;
55
56   public static final Font PRINT_FONT = new Font("Monospaced", Font.PLAIN, 9);
57   public static final Font FOOTER_FONT = new Font("Monospaced", Font.PLAIN, 8);
58   public static final Font LINE_FONT = new Font("Monospaced", Font.ITALIC, 8);
59   public float LINE_NUM_WIDTH;
60
61   private static FontRenderContext DEFAULT_FRC = new FontRenderContext(null, false, true);
62
63   /** Constructs a DrJavaBook which a given content text, filename, and pageformat. */
64   public DrJavaBook(String JavaDoc text, String JavaDoc fileName, PageFormat format) {
65     _pagePrinters = new ArrayList<PagePrinter>();
66     _format = format;
67     _fileName = fileName;
68
69     TextLayout textl = new TextLayout("XXX ", LINE_FONT, DEFAULT_FRC);
70     LINE_NUM_WIDTH = textl.getAdvance();
71
72     setUpPagePrinters(text);
73   }
74
75   /**
76    * Method which creates all of the individual Printable objects
77    * given a String text.
78    * @param text The text of the document.
79    */

80   private void setUpPagePrinters(String JavaDoc text) {
81     int linenum = 0;
82     int reallinenum = 1;
83     String JavaDoc thisText;
84     FontRenderContext frc = new FontRenderContext(null, false, true);
85
86     // determine the number of lines per page
87
TextLayout textl = new TextLayout("X", PRINT_FONT, frc);
88     float lineHeight = textl.getLeading() + textl.getAscent();
89     int linesPerPage = (int) (_format.getImageableHeight() / lineHeight) - 1;
90
91     HashMap<TextAttribute,Object JavaDoc> map = new HashMap<TextAttribute,Object JavaDoc>(); // Added parameterization <TextAttribute, Object>.
92
map.put(TextAttribute.FONT, PRINT_FONT);
93
94     char[] carriageReturn = {(char) 10};
95     String JavaDoc lineSeparator = new String JavaDoc(carriageReturn);
96
97     try {
98       thisText = text.substring(0, text.indexOf(lineSeparator));
99       text = text.substring(text.indexOf(lineSeparator) + 1);
100     }
101     catch (StringIndexOutOfBoundsException JavaDoc e) {
102       thisText = text;
103       text = "";
104     }
105
106     int page = 0;
107     PagePrinter thisPagePrinter = new PagePrinter(page, _fileName, this);
108     _pagePrinters.add(thisPagePrinter);
109
110     // loop over each of the *real* lines in the document
111
while (! (thisText.equals("") && (text.equals("")))) {
112       if (thisText.equals("")) thisText = " ";
113
114       AttributedCharacterIterator charIterator = (new AttributedString(thisText, map)).getIterator();
115       LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, frc);
116
117       boolean isCarryLine = false;
118
119       // loop over each of the broken lines in the real line
120
while (measurer.getPosition() < charIterator.getEndIndex()) {
121         TextLayout pageNumber = new TextLayout(" ", LINE_FONT, DEFAULT_FRC);
122
123         if (! isCarryLine)
124           pageNumber = new TextLayout("" + reallinenum, LINE_FONT, DEFAULT_FRC);
125
126         // add this TextLayout to the PagePrinter
127
thisPagePrinter.add(measurer.nextLayout((float) _format.getImageableWidth() - LINE_NUM_WIDTH), pageNumber);
128
129         linenum++;
130         // Create a new PagePrinter, if necessary
131
if (linenum == (linesPerPage * (page+1)))
132         {
133           page++;
134           thisPagePrinter = new PagePrinter(page, _fileName, this);
135           _pagePrinters.add(thisPagePrinter);
136         }
137
138         isCarryLine = true;
139       }
140
141       reallinenum++;
142
143       // Get next *real* line
144
try {
145         thisText = text.substring(0, text.indexOf(lineSeparator));
146         text = text.substring(text.indexOf(lineSeparator) + 1);
147       } catch (StringIndexOutOfBoundsException JavaDoc e) {
148         thisText = text;
149         text = "";
150       }
151     }
152   }
153
154   /** @return The number of pages in this print job. */
155   public int getNumberOfPages() { return _pagePrinters.size(); }
156
157   /** Returns the PageFormat for this print job.
158    * @param pageIndex The page number
159    * @return the PageFormat of this print job.
160    */

161   public PageFormat getPageFormat(int pageIndex) { return _format; }
162
163   /** Returns the Printable object for a given page.
164    * @param pageIndex The page number.
165    * @return The Printable object for the given page.
166    */

167   public Printable getPrintable(int pageIndex) { return _pagePrinters.get(pageIndex); }
168
169 }
170
Popular Tags