-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintPreview.java
More file actions
executable file
·401 lines (315 loc) · 12.5 KB
/
PrintPreview.java
File metadata and controls
executable file
·401 lines (315 loc) · 12.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//package com.genedavissoftware.printing.backend;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
//import com.genedavissoftware.printing.*;
/**
* This class is intended to handle printing requests from children of
* GDSPrinting. The process is to set the text as html (setNewText())
* and then call print().
*
* Internally the html is reformated as the user chooses bigger or smaller
* buttons in the preview window.
*/
public class PrintPreview extends JFrame implements Printable {
public static PrintPreview pp = null;
static int fontSize = 0;
static boolean doubleBuffered = true;
Panel pnMainPanel;
Button btBiggerJB;
Button btSmallerJB;
Button btCancelJB;
JScrollPane editorScrollPane;
JEditorPane editorPane;
Button btOkayJB;
////////////////////////////////////////
// need generic component for printing
/**What ever component you print, it is placed here to await printing.*/
static Component comp;
//This is the raw data to print if using the internal JEditorPane for the
//component to print.
static String html = "";
//print methods...
/**
* This is one of the print methods called by the classes that the programmer is
* using to interface with the PrintPreview.<br>
* <br>
*/
public static void print(String text) throws GDSPrintException {
PrintPreview.pp = new PrintPreview();
//TODO Set the text to be printed
comp = PrintPreview.pp.createComponent();
//The text should come in as HTML, whether in a body, or
//just text intermingled with HTML tags.
fontSize = -1 ;
PrintPreview.pp.setNewText(text);
PrintPreview.pp.showPreviewWindow();
PrintPreview.pp.setCenter() ;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
PrintPreview.pp.setVisible( true );
}
});
}
public void setCenter() {
Dimension screen = getToolkit().getDefaultToolkit().getScreenSize();
double top = 0.5*(screen.getWidth()-this.getWidth());
double left = 0.5*(screen.getHeight()-this.getHeight());
int x = new Double(top).intValue();
int y = new Double(left).intValue();
PrintPreview.pp.setLocation(x, y);
}
/**
* This is one of the print methods called by the classes that the programmer is
* using to interface with the PrintPreview.<br>
* <br>
* Eventually this Component will need to implement some interface to allow
* for increasing and decreasing of text size in a manner the programmer intends.
* For now, however, I'm just going to deactivate the "Bigger" and "Smaller"
* buttons.<br>
*/
public static void print(Component comp) throws GDSPrintException {
pp = new PrintPreview();
//TODO Set the Component to be printed
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pp.setVisible( true );
}
});
}
//constructor
/**
* Default constructor.<br>
* <br>
* Self contained printable editor pane for previewing text and
* images that will be displayed for printing.
*/
public PrintPreview() {
super( "Print Preview" );
}
//populate 'comp'
/**
* Creates a default component to print. The default is a JEditorPane.<br>
*/
public JEditorPane createComponent() {
///////////////////////////////////
// Time to create the display pane
editorPane = new JEditorPane();
//editorPane.setContentType("text/plain");
editorPane.setContentType("text/html");
editorPane.setEditable(false);
return editorPane;
}
// set up the preview JFrame
/**
* Creates the window for the component to be previewed in. The 'comp'
* variable should be filled first, before the method is called.
*/
public void showPreviewWindow() {
setSize(new Dimension(700, 550));
setResizable(false);
pnMainPanel = new Panel();
GridBagLayout gbMainPanel = new GridBagLayout();
GridBagConstraints gbcMainPanel = new GridBagConstraints();
pnMainPanel.setLayout( gbMainPanel );
btSmallerJB = new Button( "Zoom Out" );
btSmallerJB.addActionListener(new SmallerAL());
gbcMainPanel.gridx = 0;
gbcMainPanel.gridy = 0;
gbcMainPanel.gridwidth = 4;
gbcMainPanel.gridheight = 2;
gbcMainPanel.fill = GridBagConstraints.NONE;
gbcMainPanel.weightx = 0;
gbcMainPanel.weighty = 1;
gbcMainPanel.anchor = GridBagConstraints.NORTHWEST;
gbcMainPanel.insets = new Insets( 5,5,5,0 );
gbMainPanel.setConstraints( btSmallerJB, gbcMainPanel );
pnMainPanel.add( btSmallerJB );
btBiggerJB = new Button( "Zoom In" );
btBiggerJB.addActionListener(new BiggerAL());
gbcMainPanel.gridx = 4;
gbcMainPanel.gridy = 0;
gbcMainPanel.gridwidth = 4;
gbcMainPanel.gridheight = 2;
gbcMainPanel.fill = GridBagConstraints.NONE;
gbcMainPanel.weightx = 0;
gbcMainPanel.weighty = 1;
gbcMainPanel.anchor = GridBagConstraints.NORTHWEST;
gbcMainPanel.insets = new Insets( 5,5,5,0 );
gbMainPanel.setConstraints( btBiggerJB, gbcMainPanel );
pnMainPanel.add( btBiggerJB );
btOkayJB = new Button( " Print ..." );
btOkayJB.addActionListener(new OkayAL());
gbcMainPanel.gridx = 27;
gbcMainPanel.gridy = 28;
gbcMainPanel.gridwidth = 7;
gbcMainPanel.gridheight = 2;
gbcMainPanel.fill = GridBagConstraints.NONE;
gbcMainPanel.weightx = 1;
gbcMainPanel.weighty = 1;
gbcMainPanel.anchor = GridBagConstraints.SOUTHEAST;
gbcMainPanel.insets = new Insets( 5,0,5,5 );
gbMainPanel.setConstraints( btOkayJB, gbcMainPanel );
pnMainPanel.add( btOkayJB );
btCancelJB = new Button( "Cancel" );
btCancelJB.addActionListener(new CancelAL());
gbcMainPanel.gridx = 34;
gbcMainPanel.gridy = 28;
gbcMainPanel.gridwidth = 4;
gbcMainPanel.gridheight = 2;
gbcMainPanel.fill = GridBagConstraints.NONE;
gbcMainPanel.weightx = 0;
gbcMainPanel.weighty = 1;
gbcMainPanel.anchor = GridBagConstraints.SOUTHEAST;
gbcMainPanel.insets = new Insets( 5,0,5,5 );
gbMainPanel.setConstraints( btCancelJB, gbcMainPanel );
pnMainPanel.add( btCancelJB );
/////////////////////////////////////////////
// Put the editor pane in a scroll pane.
// using the comp variable. This may have been supplied by
// the users of the API.
editorScrollPane = new JScrollPane(comp);
editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
editorScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
editorScrollPane.setPreferredSize(new Dimension(500, 400));
JScrollPane scpPanel1 = new JScrollPane( editorScrollPane );
gbcMainPanel.gridx = 0;
gbcMainPanel.gridy = 2;
gbcMainPanel.gridwidth = 38;
gbcMainPanel.gridheight = 26;
gbcMainPanel.fill = GridBagConstraints.BOTH;
gbcMainPanel.weightx = 2;
gbcMainPanel.weighty = 2;
gbcMainPanel.anchor = GridBagConstraints.CENTER;
gbcMainPanel.insets = new Insets( 0,5,0,5 );
gbMainPanel.setConstraints( scpPanel1, gbcMainPanel );
pnMainPanel.add( scpPanel1 );
setDefaultCloseOperation( DISPOSE_ON_CLOSE );
setContentPane( pnMainPanel );
pack();
}
////////////////////////////////////
// Utility methods
public void setNewText(String html) {
PrintPreview.html = html;
JEditorPane jep = (JEditorPane) comp;
jep.setText( resizeHtml() );
}
/*
* Update the size of the text in the html.
* WARNING: Here there be regex.
*/
public String resizeHtml() {//FIXME this needs to have more generic search and replace.
String retval = PrintPreview.html;
String header = "";
String body = "";
//System.out.println("font size = "+fontSize);
if ((fontSize+1) < 0) header = "<font size=\"" + (fontSize+1) + "\">";//'-' sign shows up by itself, don't want an extra '+' sign
else header = "<font size=\"+" + (fontSize+1) + "\">";
if (fontSize < 0) body = "<font size=\"" + (fontSize) + "\">";//'-' sign shows up by itself, don't want an extra '+' sign
else body = "<font size=\"+"+ fontSize +"\">";
//FIXME replace all headers and bodies. This is real basic and won't be good enough when raw html is supported.
Pattern pathead = Pattern.compile("<font size=\"\\+1\">");
Matcher mathead = pathead.matcher(retval);
retval = mathead.replaceAll(header);
Pattern patbod = Pattern.compile("<font size=\"\\+0\">");
Matcher matbod = patbod.matcher(retval);
retval = matbod.replaceAll( (body) );
//System.out.println("retval-->"+retval+"<--");
return retval;
}
////////////////////////////////////////
// Printable implementation
/**
* Printable's implementation
*/
public int print(Graphics g, PageFormat pf, int pageIndex) {
//assume the page exists until proven otherwise
int retval = Printable.PAGE_EXISTS;
//Decide which pages exist, and how big they are.
Dimension dim = comp.getSize();
double height = dim.getHeight();
double pageHeight = pf.getImageableHeight();
int pageCount = (int) Math.ceil(height / pageHeight);
//The first page is numbered '0'
if ((pageIndex+1) > pageCount){
retval = Printable.NO_SUCH_PAGE;
} else {
disableDoubleBuffering();//see if we can give printing a speed bump
//setting up the Graphics object for printing
g.translate((int)(pf.getImageableX()), (int)(pf.getImageableY()));
g.translate(0, (int)(-pageIndex * pageHeight));
//populate the Graphics object from HelloPrint's paint() method
editorPane.paint(g);
resetDoubleBuffering();//put things back the way we found them
}
return retval;
}
/**
* Disabling double buffering speeds printing
*/
public void disableDoubleBuffering() {
RepaintManager cm = RepaintManager.currentManager(this);
doubleBuffered = cm.isDoubleBufferingEnabled();
if (doubleBuffered) cm.setDoubleBufferingEnabled(false);
}
/**
* Turn back on double buffering if it was ever on.
*/
public void resetDoubleBuffering() {
RepaintManager cm = RepaintManager.currentManager(this);
cm.setDoubleBufferingEnabled(doubleBuffered);
}
///////////////////////////////////////
// Event Listeners for the buttons
class BiggerAL implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if (fontSize < 2) fontSize++;
setNewText(PrintPreview.html);
}
}
class SmallerAL implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if (fontSize > -3) fontSize--;
setNewText(PrintPreview.html);
}
}
class OkayAL implements ActionListener {
public void actionPerformed(ActionEvent ae) {
//invokeLater() is used as a workaround for a java
//gui bug.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
//get a PrintJob
PrinterJob pjob = PrinterJob.getPrinterJob();
//set a HelloPrint as the target to print
pjob.setPrintable(pp, pjob.defaultPage());
//get the print dialog, continue if canel
//is not clicked
if (pjob.printDialog()) {
//print the target (HelloPrint)
pjob.print();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
pp.setVisible(false);
}
}
class CancelAL implements ActionListener {
public void actionPerformed(ActionEvent ae) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pp.setVisible(false);
}
});
}
}
}