1
|
|
import java.awt.*;
|
2
|
|
import java.awt.event.*;
|
3
|
|
import java.awt.image.*;
|
4
|
|
import java.util.*;
|
5
|
|
import java.awt.print.*;
|
6
|
|
|
7
|
|
import javax.swing.*;
|
8
|
|
import javax.swing.border.*;
|
9
|
|
import javax.swing.event.*;
|
10
|
|
|
11
|
|
public class PrintPreview extends JFrame
|
12
|
|
{
|
13
|
|
protected int m_wPage;
|
14
|
|
protected int m_hPage;
|
15
|
|
protected Printable m_target;
|
16
|
|
protected JComboBox m_cbScale;
|
17
|
|
protected PreviewContainer m_preview;
|
18
|
|
|
19
|
0
|
public PrintPreview(Printable target) {
|
20
|
0
|
this(target, "Print Preview");
|
21
|
|
}
|
22
|
|
|
23
|
1
|
public PrintPreview(Printable target, String title) {
|
24
|
1
|
super(title);
|
25
|
1
|
setSize(600, 400);
|
26
|
1
|
m_target = target;
|
27
|
|
|
28
|
1
|
JToolBar tb = new JToolBar();
|
29
|
1
|
JButton bt = new JButton("Print", new ImageIcon("print.gif"));
|
30
|
1
|
ActionListener lst = new ActionListener() {
|
31
|
0
|
public void actionPerformed(ActionEvent e) {
|
32
|
0
|
try {
|
33
|
|
|
34
|
0
|
PrinterJob prnJob = PrinterJob.getPrinterJob();
|
35
|
0
|
prnJob.setPrintable(m_target);
|
36
|
0
|
setCursor( Cursor.getPredefinedCursor(
|
37
|
|
Cursor.WAIT_CURSOR));
|
38
|
0
|
prnJob.print();
|
39
|
0
|
setCursor( Cursor.getPredefinedCursor(
|
40
|
|
Cursor.DEFAULT_CURSOR));
|
41
|
0
|
dispose();
|
42
|
|
}
|
43
|
|
catch (PrinterException ex) {
|
44
|
0
|
ex.printStackTrace();
|
45
|
0
|
System.err.println("Printing error: "+ex.toString());
|
46
|
|
}
|
47
|
|
}
|
48
|
|
};
|
49
|
1
|
bt.addActionListener(lst);
|
50
|
1
|
bt.setAlignmentY(0.5f);
|
51
|
1
|
bt.setMargin(new Insets(4,6,4,6));
|
52
|
1
|
tb.add(bt);
|
53
|
|
|
54
|
1
|
bt = new JButton("Close");
|
55
|
1
|
lst = new ActionListener() {
|
56
|
0
|
public void actionPerformed(ActionEvent e) {
|
57
|
0
|
dispose();
|
58
|
|
}
|
59
|
|
};
|
60
|
1
|
bt.addActionListener(lst);
|
61
|
1
|
bt.setAlignmentY(0.5f);
|
62
|
1
|
bt.setMargin(new Insets(2,6,2,6));
|
63
|
1
|
tb.add(bt);
|
64
|
|
|
65
|
1
|
String[] scales = { "10 %", "25 %", "50 %", "100 %" };
|
66
|
1
|
m_cbScale = new JComboBox(scales);
|
67
|
1
|
lst = new ActionListener() {
|
68
|
0
|
public void actionPerformed(ActionEvent e) {
|
69
|
0
|
Thread runner = new Thread() {
|
70
|
0
|
public void run() {
|
71
|
0
|
String str = m_cbScale.getSelectedItem().
|
72
|
|
toString();
|
73
|
0
|
if (str.endsWith("%"))
|
74
|
0
|
str = str.substring(0, str.length()-1);
|
75
|
0
|
str = str.trim();
|
76
|
0
|
int scale = 0;
|
77
|
0
|
try { scale = Integer.parseInt(str); }
|
78
|
0
|
catch (NumberFormatException ex) { return; }
|
79
|
0
|
int w = (int)(m_wPage*scale/100);
|
80
|
0
|
int h = (int)(m_hPage*scale/100);
|
81
|
|
|
82
|
0
|
Component[] comps = m_preview.getComponents();
|
83
|
0
|
for (int k=0; k<comps.length; k++) {
|
84
|
0
|
if (!(comps[k] instanceof PagePreview))
|
85
|
0
|
continue;
|
86
|
0
|
PagePreview pp = (PagePreview)comps[k];
|
87
|
0
|
pp.setScaledSize(w, h);
|
88
|
|
}
|
89
|
0
|
m_preview.doLayout();
|
90
|
0
|
m_preview.getParent().getParent().validate();
|
91
|
|
}
|
92
|
|
};
|
93
|
0
|
runner.start();
|
94
|
|
}
|
95
|
|
};
|
96
|
1
|
m_cbScale.addActionListener(lst);
|
97
|
1
|
m_cbScale.setMaximumSize(m_cbScale.getPreferredSize());
|
98
|
1
|
m_cbScale.setEditable(true);
|
99
|
1
|
tb.addSeparator();
|
100
|
1
|
tb.add(m_cbScale);
|
101
|
1
|
getContentPane().add(tb, BorderLayout.NORTH);
|
102
|
|
|
103
|
1
|
m_preview = new PreviewContainer();
|
104
|
|
|
105
|
1
|
PrinterJob prnJob = PrinterJob.getPrinterJob();
|
106
|
1
|
PageFormat pageFormat = prnJob.defaultPage();
|
107
|
1
|
if (pageFormat.getHeight()==0 || pageFormat.getWidth()==0) {
|
108
|
0
|
System.err.println("Unable to determine default page size");
|
109
|
0
|
return;
|
110
|
|
}
|
111
|
1
|
m_wPage = (int)(pageFormat.getWidth());
|
112
|
1
|
m_hPage = (int)(pageFormat.getHeight());
|
113
|
1
|
int scale = 10;
|
114
|
1
|
int w = (int)(m_wPage*scale/100);
|
115
|
1
|
int h = (int)(m_hPage*scale/100);
|
116
|
|
|
117
|
1
|
int pageIndex = 0;
|
118
|
1
|
try {
|
119
|
1
|
while (true) {
|
120
|
2
|
BufferedImage img = new BufferedImage(m_wPage,
|
121
|
|
m_hPage, BufferedImage.TYPE_INT_RGB);
|
122
|
2
|
Graphics g = img.getGraphics();
|
123
|
2
|
g.setColor(Color.white);
|
124
|
2
|
g.fillRect(0, 0, m_wPage, m_hPage);
|
125
|
2
|
if (target.print(g, pageFormat, pageIndex) !=
|
126
|
|
Printable.PAGE_EXISTS)
|
127
|
1
|
break;
|
128
|
1
|
PagePreview pp = new PagePreview(w, h, img);
|
129
|
1
|
m_preview.add(pp);
|
130
|
1
|
pageIndex++;
|
131
|
|
}
|
132
|
|
}
|
133
|
|
catch (PrinterException e) {
|
134
|
0
|
e.printStackTrace();
|
135
|
0
|
System.err.println("Printing error: "+e.toString());
|
136
|
|
}
|
137
|
|
|
138
|
1
|
JScrollPane ps = new JScrollPane(m_preview);
|
139
|
1
|
getContentPane().add(ps, BorderLayout.CENTER);
|
140
|
|
|
141
|
1
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
142
|
1
|
setVisible(true);
|
143
|
|
}
|
144
|
|
|
145
|
|
class PreviewContainer extends JPanel
|
146
|
|
{
|
147
|
|
protected int H_GAP = 16;
|
148
|
|
protected int V_GAP = 10;
|
149
|
|
|
150
|
5
|
public Dimension getPreferredSize() {
|
151
|
5
|
int n = getComponentCount();
|
152
|
5
|
if (n == 0)
|
153
|
0
|
return new Dimension(H_GAP, V_GAP);
|
154
|
5
|
Component comp = getComponent(0);
|
155
|
5
|
Dimension dc = comp.getPreferredSize();
|
156
|
5
|
int w = dc.width;
|
157
|
5
|
int h = dc.height;
|
158
|
|
|
159
|
5
|
Dimension dp = getParent().getSize();
|
160
|
5
|
int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
|
161
|
5
|
int nRow = n/nCol;
|
162
|
5
|
if (nRow*nCol < n)
|
163
|
4
|
nRow++;
|
164
|
|
|
165
|
5
|
int ww = nCol*(w+H_GAP) + H_GAP;
|
166
|
5
|
int hh = nRow*(h+V_GAP) + V_GAP;
|
167
|
5
|
Insets ins = getInsets();
|
168
|
5
|
return new Dimension(ww+ins.left+ins.right,
|
169
|
|
hh+ins.top+ins.bottom);
|
170
|
|
}
|
171
|
|
|
172
|
0
|
public Dimension getMaximumSize() {
|
173
|
0
|
return getPreferredSize();
|
174
|
|
}
|
175
|
|
|
176
|
0
|
public Dimension getMinimumSize() {
|
177
|
0
|
return getPreferredSize();
|
178
|
|
}
|
179
|
|
|
180
|
1
|
public void doLayout() {
|
181
|
1
|
Insets ins = getInsets();
|
182
|
1
|
int x = ins.left + H_GAP;
|
183
|
1
|
int y = ins.top + V_GAP;
|
184
|
|
|
185
|
1
|
int n = getComponentCount();
|
186
|
1
|
if (n == 0)
|
187
|
0
|
return;
|
188
|
1
|
Component comp = getComponent(0);
|
189
|
1
|
Dimension dc = comp.getPreferredSize();
|
190
|
1
|
int w = dc.width;
|
191
|
1
|
int h = dc.height;
|
192
|
|
|
193
|
1
|
Dimension dp = getParent().getSize();
|
194
|
1
|
int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
|
195
|
1
|
int nRow = n/nCol;
|
196
|
1
|
if (nRow*nCol < n)
|
197
|
1
|
nRow++;
|
198
|
|
|
199
|
1
|
int index = 0;
|
200
|
1
|
for (int k = 0; k<nRow; k++) {
|
201
|
2
|
for (int m = 0; m<nCol; m++) {
|
202
|
2
|
if (index >= n)
|
203
|
1
|
return;
|
204
|
1
|
comp = getComponent(index++);
|
205
|
1
|
comp.setBounds(x, y, w, h);
|
206
|
1
|
x += w+H_GAP;
|
207
|
|
}
|
208
|
0
|
y += h+V_GAP;
|
209
|
0
|
x = ins.left + H_GAP;
|
210
|
|
}
|
211
|
|
}
|
212
|
|
}
|
213
|
|
|
214
|
|
class PagePreview extends JPanel
|
215
|
|
{
|
216
|
|
protected int m_w;
|
217
|
|
protected int m_h;
|
218
|
|
protected Image m_source;
|
219
|
|
protected Image m_img;
|
220
|
|
|
221
|
1
|
public PagePreview(int w, int h, Image source) {
|
222
|
1
|
m_w = w;
|
223
|
1
|
m_h = h;
|
224
|
1
|
m_source= source;
|
225
|
1
|
m_img = m_source.getScaledInstance(m_w, m_h,
|
226
|
|
Image.SCALE_SMOOTH);
|
227
|
1
|
m_img.flush();
|
228
|
1
|
setBackground(Color.white);
|
229
|
1
|
setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
|
230
|
|
}
|
231
|
|
|
232
|
0
|
public void setScaledSize(int w, int h) {
|
233
|
0
|
m_w = w;
|
234
|
0
|
m_h = h;
|
235
|
0
|
m_img = m_source.getScaledInstance(m_w, m_h,
|
236
|
|
Image.SCALE_SMOOTH);
|
237
|
0
|
repaint();
|
238
|
|
}
|
239
|
|
|
240
|
6
|
public Dimension getPreferredSize() {
|
241
|
6
|
Insets ins = getInsets();
|
242
|
6
|
return new Dimension(m_w+ins.left+ins.right,
|
243
|
|
m_h+ins.top+ins.bottom);
|
244
|
|
}
|
245
|
|
|
246
|
0
|
public Dimension getMaximumSize() {
|
247
|
0
|
return getPreferredSize();
|
248
|
|
}
|
249
|
|
|
250
|
0
|
public Dimension getMinimumSize() {
|
251
|
0
|
return getPreferredSize();
|
252
|
|
}
|
253
|
|
|
254
|
2
|
public void paint(Graphics g) {
|
255
|
2
|
g.setColor(getBackground());
|
256
|
2
|
g.fillRect(0, 0, getWidth(), getHeight());
|
257
|
2
|
g.drawImage(m_img, 0, 0, this);
|
258
|
2
|
paintBorder(g);
|
259
|
|
}
|
260
|
|
}
|
261
|
|
}
|
262
|
|
|