001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.print;
017:
018: import java.awt.Graphics2D;
019: import java.lang.reflect.Array;
020: import java.lang.reflect.Method;
021: import java.text.DateFormat;
022: import java.util.Date;
023:
024: /**
025: * cPrintVariable class
026: *
027: * @author unknown
028: *
029: */
030: public class cPrintVariable extends cParagraph {
031: String codeString;
032: CodeStringParser[] parser;
033: int parserCount = -1;
034: String[] keys = { "PAGE_NR", "PAGE_COUNT", "DATE_TODAY" };
035: String[] methods = { "getPageNr", "getPageCount", "getDateToday" };
036:
037: /**
038: * Default Constructor for cPrintVariable
039: */
040: public cPrintVariable() {
041: Class c = this .getClass();
042: parserCount = Array.getLength(keys);
043: parser = new CodeStringParser[parserCount];
044:
045: for (int i = 0; i < parserCount; i++) {
046: try {
047: parser[i] = new CodeStringParser(this , keys[i], c
048: .getMethod(methods[i], new Class[0]));
049: } catch (NoSuchMethodException e) {
050: e.printStackTrace();
051: }
052: }
053: }
054:
055: /*
056: * Methods called by the Variable parsers
057: */
058:
059: /**
060: * getPageNr method
061: *
062: * @return PageNr
063: */
064: public String getPageNr() {
065: return Integer.toString(page.getDocument().getPageNr(page));
066: }
067:
068: /**
069: * getPageCount method
070: *
071: * @return PageCount
072: */
073: public String getPageCount() {
074: return Integer.toString(page.getDocument().getPageCount());
075: }
076:
077: /**
078: * getDateToday method
079: *
080: * @return df Today's Date in Java Format
081: */
082: public String getDateToday() {
083: DateFormat df = DateFormat.getDateInstance();
084:
085: return df.format(new Date());
086: }
087:
088: /**
089: * setCodeString method
090: *
091: * @param n
092: */
093: public void setCodeString(String n) {
094: codeString = n;
095: setText(n); // For getHeight() to return the right Value
096: }
097:
098: /* (non-Javadoc)
099: * @see org.columba.core.print.cParagraph#print(java.awt.Graphics2D)
100: */
101: public void print(Graphics2D g) {
102: setText(processCodeString());
103: super .print(g);
104: }
105:
106: /**
107: * processCodeString method
108: * @return result
109: */
110: public String processCodeString() {
111: StringBuffer result = new StringBuffer();
112: boolean isDecoding = false;
113: char c;
114:
115: for (int i = 0; i < codeString.length(); i++) {
116: c = codeString.charAt(i);
117:
118: if (c == '%') {
119: isDecoding = !isDecoding;
120:
121: if (!isDecoding) {
122: for (int j = 0; j < parserCount; j++) {
123: parser[j].reset();
124: }
125: }
126: } else {
127: if (isDecoding) {
128: for (int j = 0; j < parserCount; j++) {
129: if (parser[j].clock(c)) {
130: result.append(parser[j].getValue());
131: }
132: }
133: } else {
134: result.append(c);
135: }
136: }
137: }
138: return result.toString();
139: }
140: }
141:
142: /**
143: * CodeStringParser method
144: *
145: * @author unknown
146: *
147: */
148: class CodeStringParser {
149: Method hit;
150: char[] match;
151: int length;
152: Object parent;
153: int pos;
154:
155: public CodeStringParser(Object p, String m, Method h) {
156: parent = p;
157: match = m.toCharArray();
158: length = m.length();
159: hit = h;
160: pos = 0;
161: }
162:
163: //TODO: Should this be renamed to cLoc or something more appropriate than clock?
164: /**
165: * clock method
166: *
167: * @param in
168: * @return boolean
169: */
170: public boolean clock(char in) {
171: if (in == match[pos]) {
172: pos++;
173:
174: if (pos == length) {
175: pos = 0;
176: return true;
177: }
178: } else {
179: pos = 0;
180: }
181: return false;
182: }
183:
184: /**
185: * getValue method
186: *
187: * @return value
188: */
189: public String getValue() {
190: try {
191: return (String) hit.invoke((Object) parent, (Object) null);
192: } catch (Exception e) {
193: return new String("<Unhandled Exception occcured>");
194: }
195: }
196:
197: /**
198: * reset method - reset (int) pos to 0
199: */
200: public void reset() {
201: pos = 0;
202: }
203: }
|