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:
016: /**
017: * State pattern that is used to print the XML file
018: *
019: *@author Chris Seguin
020: */
021: public abstract class State {
022: /**
023: * the font
024: */
025: protected Font font;
026: protected Color color;
027: private Graphics g;
028: private int x;
029: private int y;
030: private int fontSize;
031:
032: /**
033: * Constructor for the State object
034: */
035: public State() {
036: font = null;
037: fontSize = -1;
038: color = Color.black;
039: }
040:
041: /**
042: * Sets the FontSize attribute of the State object
043: *
044: *@param value The new FontSize value
045: */
046: public void setFontSize(int value) {
047: if (value != fontSize) {
048: fontSize = value;
049: font = null;
050: }
051: }
052:
053: /**
054: * Sets the Graphics attribute of the State object
055: *
056: *@param value The new Graphics value
057: */
058: public void setGraphics(Graphics value) {
059: g = value;
060: }
061:
062: /**
063: * Sets the X attribute of the State object
064: *
065: *@param value The new X value
066: */
067: public void setX(int value) {
068: x = value;
069: }
070:
071: /**
072: * Sets the Y attribute of the State object
073: *
074: *@param value The new Y value
075: */
076: public void setY(int value) {
077: y = value;
078: }
079:
080: /**
081: * Gets the FontSize attribute of the State object
082: *
083: *@return The FontSize value
084: */
085: public int getFontSize() {
086: return fontSize;
087: }
088:
089: /**
090: * Gets the Font attribute of the State object
091: *
092: *@return The Font value
093: */
094: public abstract Font getFont();
095:
096: /**
097: * Gets the Graphics attribute of the State object
098: *
099: *@return The Graphics value
100: */
101: public Graphics getGraphics() {
102: return g;
103: }
104:
105: /**
106: * Gets the X attribute of the State object
107: *
108: *@return The X value
109: */
110: public int getX() {
111: return x;
112: }
113:
114: /**
115: * Gets the Y attribute of the State object
116: *
117: *@return The Y value
118: */
119: public int getY() {
120: return y;
121: }
122:
123: /**
124: * Processes a single line and returns the state that is in effect at the
125: * end of the line.
126: *
127: *@param line the line
128: *@return the state
129: */
130: public State processLine(String line) {
131: return processLine(line, 0, new StringBuffer());
132: }
133:
134: /**
135: * The actual worker method that processes the line. This is what is defined
136: * by the various states
137: *
138: *@param line the line
139: *@param index the index of the character
140: *@param buf the buffer
141: *@return the state at the end of the line
142: */
143: protected abstract State processLine(String line, int index,
144: StringBuffer buf);
145:
146: /**
147: * Set the state for the next state
148: *
149: *@param next the next state
150: */
151: protected void initState(State next) {
152: next.setGraphics(getGraphics());
153: next.setX(getX());
154: next.setY(getY());
155: next.setFontSize(getFontSize());
156: }
157:
158: /**
159: * Prints the buffer
160: *
161: *@param buf the buffer
162: */
163: protected void print(StringBuffer buf) {
164: String out = buf.toString();
165: if (out.length() == 0) {
166: return;
167: }
168:
169: //System.out.println("Printing: [" + out + "] (" + getClass().getName() + ") " + fontSize + "(" + x + ", " + y +")");
170: //font = new Font("Monospaced", Font.PLAIN, fontSize);
171: g.setFont(getFont());
172: g.setColor(color);
173: g.drawString(out, x, y);
174:
175: FontMetrics fm = g.getFontMetrics();
176: x = x + fm.stringWidth(out);
177: }
178: }
|