-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormattedPrint.java
More file actions
executable file
·196 lines (157 loc) · 7.93 KB
/
FormattedPrint.java
File metadata and controls
executable file
·196 lines (157 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright (c) 2005, Terrance Gene Davis
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Gene Davis Software nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
//package com.genedavissoftware.printing;
import java.net.*;
//import com.genedavissoftware.printing.backend.PrintPreview;
/**
* Build a formatted document and print it. types of input possible are
* plain text, bold text, italicized text, section titles, document titles
* tables of text and images.<br>
* <br>
* The image and table options still need some work, the rest of this class should
* be functional.<br>
* <br>
* There is the issue of what to do with multiple spaces. The internal HTML makes
* extra spaces go away. What should be done? have an addSpace() method? I haven't
* decided and will gladly take input.<br>
*/
public class FormattedPrint extends GDSPrinting {
String doc = "";
/**Testing/example for using FormattedPrint*/
public static void main(String args[]) {
FormattedPrint formatted = new FormattedPrint();
//Making a sample document
formatted.addDocumentTitle("Our Lovely Title");
formatted.addImage( ClassLoader.getSystemResource("images/woman.gif") );
formatted.addSectionTitle("Our First Section");
formatted.addPlainText("Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text Just sample text ");
formatted.addLineBreak();
formatted.addPlainText("Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text ");
formatted.addItalicsText("Just some more italicized sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text ");
formatted.addBoldText("Just some more italicized sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text ");
formatted.addPlainText("Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text Just some more sample text ");
formatted.addLineBreak();
formatted.addLineBreak();
formatted.addSectionTitle("Our First Section");
String[] testA = {"this","is","a","ringer","of","a","test","!!!!!"};
formatted.addTable(2,4,testA);
formatted.addLineBreak();
formatted.addLineBreak();
formatted.addPlainText("Thus concludes our nifty test.");
try { formatted.print(); } catch (Exception e) {}
}
/**
* Print the currently maintained internal document.
*/
public void print() throws GDSPrintException {
PrintPreview.print(doc);
}
/**
* Empty the internal document in preparation for filling with
* new text.
*/
public void clear() {
doc = "";
}
/**
* Add a line break to the document. Only titles (section and Document)
* do this automatically.
*/
public void addLineBreak() {
doc += "<br>";
}
/**
* Add plain text to the document.
*/
public void addPlainText(String text) {
doc += "<font size=\"+0\">" + text + "</font>";
}
/**
* Adds italicized text. Some JVMs have a bug that will prevent
* the italicized text from being italicized.
*/
public void addItalicsText(String text) {
doc += "<font size=\"+0\"><i>" + text + "</i></font>";
}
/**
* Add bold text to the document.
*/
public void addBoldText(String text) {
doc += "<font size=\"+0\"><b>" + text + "</b></font>";
}
/**
* Add a section title to the document. These titles are smaller
* than the document titles.
*/
public void addSectionTitle(String text) {
doc += "<font size=\"+1\"><b>" + text + "</b></font><br>";
}
/**
* Add a Title sized String to the document.
*/
public void addDocumentTitle(String text) {
doc += "<h1>" + text + "</h1>";
}
/**
* Add in a table. The columns and rows of the table are based
* off the parameters that are passed in . The String[] array
* fills the table from left to right and top to bottom. Any left
* over Strings are silently ignored. Any extra fields are
* filled with empty strings.
*/
public void addTable(int cols, int rows, String[] data) {
doc += "<table border=\"0\">";
int count = 0;
for (int y=0;y<rows;y++){
doc += "<tr>";
for (int x=0;x<cols;x++){
doc += "<td>";
if (data != null && count < data.length) doc += "<font size=\"+0\">"+data[count]+"<font size=\"+0\">";
count++;
doc += "</td>";
}
doc += "</tr>";
}
doc += "</table></font>";
}
/**
* The URL to the image that is to be added to the document.
* The method ClassLoader.getResource(String name) provides
* a handy method for generating this URL.<br>
* <br>
* For instance:<br>
* <br>
* ClassLoader.getSystemResource("images/some.jpg");<br>
* <br>
* Would grab the URL of the the image 'some.jpg' in the
* folder 'images' in the directory that your app was started
* in.<br>
*/
public void addImage(URL image) {
doc += "<img src=\""+image.toString()+"\" border=\"0\" align=\"right\">";
}
}