KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > print > cPrintVariable


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.print;
17
18 import java.awt.Graphics2D JavaDoc;
19 import java.lang.reflect.Array JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23
24 /**
25  * cPrintVariable class
26  *
27  * @author unknown
28  *
29  */

30 public class cPrintVariable extends cParagraph {
31     String JavaDoc codeString;
32     CodeStringParser[] parser;
33     int parserCount = -1;
34     String JavaDoc[] keys = { "PAGE_NR", "PAGE_COUNT", "DATE_TODAY" };
35     String JavaDoc[] methods = { "getPageNr", "getPageCount", "getDateToday" };
36
37     /**
38      * Default Constructor for cPrintVariable
39      */

40     public cPrintVariable() {
41         Class JavaDoc c = this.getClass();
42         parserCount = Array.getLength(keys);
43         parser = new CodeStringParser[parserCount];
44
45         for (int i = 0; i < parserCount; i++) {
46             try {
47                 parser[i] = new CodeStringParser(this, keys[i], c.getMethod(
48                         methods[i], new Class JavaDoc[0]));
49             } catch (NoSuchMethodException JavaDoc e) {
50                 e.printStackTrace();
51             }
52         }
53     }
54
55     /*
56      * Methods called by the Variable parsers
57      */

58         
59     /**
60      * getPageNr method
61      *
62      * @return PageNr
63      */

64     public String JavaDoc getPageNr() {
65         return Integer.toString(page.getDocument().getPageNr(page));
66     }
67
68     /**
69      * getPageCount method
70      *
71      * @return PageCount
72      */

73     public String JavaDoc getPageCount() {
74         return Integer.toString(page.getDocument().getPageCount());
75     }
76
77     /**
78      * getDateToday method
79      *
80      * @return df Today's Date in Java Format
81      */

82     public String JavaDoc getDateToday() {
83         DateFormat JavaDoc df = DateFormat.getDateInstance();
84
85         return df.format(new Date JavaDoc());
86     }
87
88     /**
89      * setCodeString method
90      *
91      * @param n
92      */

93     public void setCodeString(String JavaDoc n) {
94         codeString = n;
95         setText(n); // For getHeight() to return the right Value
96
}
97
98     /* (non-Javadoc)
99      * @see org.columba.core.print.cParagraph#print(java.awt.Graphics2D)
100      */

101     public void print(Graphics2D JavaDoc g) {
102         setText(processCodeString());
103         super.print(g);
104     }
105
106     /**
107      * processCodeString method
108      * @return result
109      */

110     public String JavaDoc processCodeString() {
111         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc hit;
150     char[] match;
151     int length;
152     Object JavaDoc parent;
153     int pos;
154
155     public CodeStringParser(Object JavaDoc p, String JavaDoc m, Method JavaDoc 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 JavaDoc getValue() {
190         try {
191             return (String JavaDoc) hit.invoke((Object JavaDoc) parent, (Object JavaDoc) null);
192         } catch (Exception JavaDoc e) {
193             return new String JavaDoc("<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 }
204
Popular Tags