001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.print.xml;
010:
011: import java.awt.Color;
012: import java.awt.Font;
013: import java.awt.FontMetrics;
014: import java.awt.Graphics;
015: import java.util.ArrayList;
016: import org.acm.seguin.print.text.LineSet;
017: import org.acm.seguin.print.text.LinePrinter;
018: import org.acm.seguin.util.TextFormatter;
019:
020: /**
021: * Prints a single line
022: *
023: *@author Chris Seguin
024: */
025: public class XMLLinePrinter extends LinePrinter {
026: private ArrayList list;
027: private int fontSize;
028: private Font lineNo;
029:
030: /**
031: * Constructor for the JavaLinePrinter object
032: */
033: public XMLLinePrinter() {
034: list = new ArrayList();
035: fontSize = -1;
036: }
037:
038: /**
039: * Sets the FontSize attribute of the LinePrinter object
040: *
041: *@param value The new FontSize value
042: */
043: public void setFontSize(int value) {
044: if (value != fontSize) {
045: fontSize = value;
046: lineNo = new Font("Monospaced", Font.PLAIN, fontSize);
047: }
048: }
049:
050: /**
051: * Gets the LineHeight attribute of the LinePrinter object
052: *
053: *@param g Description of Parameter
054: *@return The LineHeight value
055: */
056: public int getLineHeight(Graphics g) {
057: init(g);
058:
059: g.setFont(lineNo);
060: FontMetrics fm = g.getFontMetrics();
061: return fm.getHeight();
062: }
063:
064: /**
065: * Prints the line
066: *
067: *@param g The graphics device
068: *@param line The string to print
069: *@param x The x location on the graphics device
070: *@param y The y location on the graphics device
071: *@param set The set of lines
072: *@param index The line we are printing
073: */
074: public void print(Graphics g, String line, int x, int y,
075: LineSet set, int index) {
076: State state;
077:
078: if (index == 0) {
079: list.add(0, TextState.getState());
080: }
081:
082: state = (State) list.get(index);
083:
084: if (line.length() == 0) {
085: list.add(index + 1, state);
086: return;
087: }
088:
089: if (state instanceof TextState) {
090: if (line.charAt(0) == '<')
091: state = TagState.getState();
092: }
093:
094: String output = TextFormatter.rightJustifyNumber(index + 1, 5)
095: + ": ";
096: g.setFont(lineNo);
097: FontMetrics fm = g.getFontMetrics();
098: g.drawString(output, x, y);
099:
100: state.setGraphics(g);
101: state.setX(x + fm.stringWidth(output));
102: state.setY(y);
103: state.setFontSize(fontSize);
104:
105: list.add(index + 1, state.processLine(line));
106: }
107: }
|