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: import java.text.*;
044:
045: /**
046: * The DrJavaBook class is DrJava's implementation of a Pageable object. It
047: * serves as the control class for printing, and is responsible for
048: * preparing the print job of previewing or printing given the String
049: * representation of the document.
050: *
051: * @version $Id: DrJavaBook.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public class DrJavaBook implements Pageable {
054:
055: private ArrayList<PagePrinter> _pagePrinters;
056: private PageFormat _format;
057: private String _fileName;
058:
059: public static final Font PRINT_FONT = new Font("Monospaced",
060: Font.PLAIN, 9);
061: public static final Font FOOTER_FONT = new Font("Monospaced",
062: Font.PLAIN, 8);
063: public static final Font LINE_FONT = new Font("Monospaced",
064: Font.ITALIC, 8);
065: public float LINE_NUM_WIDTH;
066:
067: private static FontRenderContext DEFAULT_FRC = new FontRenderContext(
068: null, false, true);
069:
070: /** Constructs a DrJavaBook which a given content text, filename, and pageformat. */
071: public DrJavaBook(String text, String fileName, PageFormat format) {
072: _pagePrinters = new ArrayList<PagePrinter>();
073: _format = format;
074: _fileName = fileName;
075:
076: TextLayout textl = new TextLayout("XXX ", LINE_FONT,
077: DEFAULT_FRC);
078: LINE_NUM_WIDTH = textl.getAdvance();
079:
080: setUpPagePrinters(text);
081: }
082:
083: /**
084: * Method which creates all of the individual Printable objects
085: * given a String text.
086: * @param text The text of the document.
087: */
088: private void setUpPagePrinters(String text) {
089: int linenum = 0;
090: int reallinenum = 1;
091: String this Text;
092: FontRenderContext frc = new FontRenderContext(null, false, true);
093:
094: // determine the number of lines per page
095: TextLayout textl = new TextLayout("X", PRINT_FONT, frc);
096: float lineHeight = textl.getLeading() + textl.getAscent();
097: int linesPerPage = (int) (_format.getImageableHeight() / lineHeight) - 1;
098:
099: HashMap<TextAttribute, Object> map = new HashMap<TextAttribute, Object>(); // Added parameterization <TextAttribute, Object>.
100: map.put(TextAttribute.FONT, PRINT_FONT);
101:
102: char[] carriageReturn = { (char) 10 };
103: String lineSeparator = new String(carriageReturn);
104:
105: try {
106: this Text = text.substring(0, text.indexOf(lineSeparator));
107: text = text.substring(text.indexOf(lineSeparator) + 1);
108: } catch (StringIndexOutOfBoundsException e) {
109: this Text = text;
110: text = "";
111: }
112:
113: int page = 0;
114: PagePrinter this PagePrinter = new PagePrinter(page, _fileName,
115: this );
116: _pagePrinters.add(this PagePrinter);
117:
118: // loop over each of the *real* lines in the document
119: while (!(this Text.equals("") && (text.equals("")))) {
120: if (this Text.equals(""))
121: this Text = " ";
122:
123: AttributedCharacterIterator charIterator = (new AttributedString(
124: this Text, map)).getIterator();
125: LineBreakMeasurer measurer = new LineBreakMeasurer(
126: charIterator, frc);
127:
128: boolean isCarryLine = false;
129:
130: // loop over each of the broken lines in the real line
131: while (measurer.getPosition() < charIterator.getEndIndex()) {
132: TextLayout pageNumber = new TextLayout(" ", LINE_FONT,
133: DEFAULT_FRC);
134:
135: if (!isCarryLine)
136: pageNumber = new TextLayout("" + reallinenum,
137: LINE_FONT, DEFAULT_FRC);
138:
139: // add this TextLayout to the PagePrinter
140: this PagePrinter.add(measurer.nextLayout((float) _format
141: .getImageableWidth()
142: - LINE_NUM_WIDTH), pageNumber);
143:
144: linenum++;
145: // Create a new PagePrinter, if necessary
146: if (linenum == (linesPerPage * (page + 1))) {
147: page++;
148: this PagePrinter = new PagePrinter(page, _fileName,
149: this );
150: _pagePrinters.add(this PagePrinter);
151: }
152:
153: isCarryLine = true;
154: }
155:
156: reallinenum++;
157:
158: // Get next *real* line
159: try {
160: this Text = text.substring(0, text
161: .indexOf(lineSeparator));
162: text = text.substring(text.indexOf(lineSeparator) + 1);
163: } catch (StringIndexOutOfBoundsException e) {
164: this Text = text;
165: text = "";
166: }
167: }
168: }
169:
170: /** @return The number of pages in this print job. */
171: public int getNumberOfPages() {
172: return _pagePrinters.size();
173: }
174:
175: /** Returns the PageFormat for this print job.
176: * @param pageIndex The page number
177: * @return the PageFormat of this print job.
178: */
179: public PageFormat getPageFormat(int pageIndex) {
180: return _format;
181: }
182:
183: /** Returns the Printable object for a given page.
184: * @param pageIndex The page number.
185: * @return The Printable object for the given page.
186: */
187: public Printable getPrintable(int pageIndex) {
188: return _pagePrinters.get(pageIndex);
189: }
190:
191: }
|