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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
public class PrintPreview extends JFrame implements Runnable
{
protected JScrollPane displayArea;
protected int m_wPage;
protected int m_hPage;
protected int width;
protected int height;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
protected PageFormat pp_pf = null;
protected JButton formatButton;
protected JButton shrinkButton;
protected JButton zoomin = new JButton ();
protected JButton zoomout = new JButton();
protected String tooltipText;
protected JCheckBox headerBox;
protected JTextField headerField = new JTextField("Gestion des équipements");
protected JButton printThisPage = new JButton();
@SuppressWarnings("unused")
private static final int INCH = 72;
private static boolean bScallToFitOnePage = false;
private String msg;
JTable table;
protected void getThePreviewPages()
{
m_target = table.getPrintable(JTable.PrintMode.FIT_WIDTH, new MessageFormat(headerField.getText()), new MessageFormat("{0}"));
m_wPage = (int)(pp_pf.getWidth());
m_hPage = (int)(pp_pf.getHeight());
int scale = getDisplayScale();
width = (int)Math.ceil(m_wPage*scale/100);
height = (int)Math.ceil(m_hPage*scale/100);
int pageIndex = 0;
try {
while (true) {
BufferedImage img = new BufferedImage(m_wPage, m_hPage,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, m_wPage, m_hPage);
if (bScallToFitOnePage) {
m_target.print(g, pp_pf, -1);
PagePreview pp = new PagePreview(width, height, img);
m_preview.add(pp);
break;
} else
if (m_target.print(g, pp_pf, pageIndex) != Printable.PAGE_EXISTS)
break;
PagePreview pp = new PagePreview(width, height, img);
m_preview.add(pp);
pageIndex++;
}
} catch (OutOfMemoryError om) {
JOptionPane.showMessageDialog(this,
"image is too big that run out of memory.", "Print Preview",
JOptionPane.INFORMATION_MESSAGE);
}
catch (PrinterException e) {
e.printStackTrace();
System.err.println("Printing error: "+e.toString());
}
}
protected void previewThePages(int orientation)
{
if (displayArea != null) displayArea.setVisible(false);
m_preview =null;
m_preview = new PreviewContainer();
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("../icones/Imprimer.png")));
getThePreviewPages();
JPanel panel = new JPanel();
panel.add(m_preview);
JPanel panel1 = new JPanel();
panel1.add(panel);
displayArea = new JScrollPane(panel1);
getContentPane().add(displayArea, BorderLayout.CENTER);
setLocationRelativeTo(null);
setResizable(true);
setExtendedState(this.MAXIMIZED_BOTH);
setVisible(true);
System.gc();
}
protected void createButtons(JToolBar tb, boolean shrink) {
tooltipText = "Ajouter en-tête";
headerBox = new JCheckBox("En-tête:", true);
headerBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
headerField.setEnabled(headerBox.isSelected());
}
});
tb.add(headerBox);
headerBox.setToolTipText(tooltipText);
tooltipText = " Champ en-tête";
headerField = new JTextField("Gestion des équipements");
headerField.setToolTipText(tooltipText);
tb.add(headerField);
JButton print = new JButton();
print.setContentAreaFilled(false);
print.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
print.setNextFocusableComponent(print);
tooltipText = "Imprimer";
print.setToolTipText(tooltipText);
print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/printico.png")));
tooltipText = "Imprimer page courante";
printThisPage.setToolTipText(tooltipText);
printThisPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/printcurrent.png")));
printThisPage.setPreferredSize(new Dimension(24, 21));
printThisPage.setContentAreaFilled(false);
printThisPage.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
printThisPage.setNextFocusableComponent(print);
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
MessageFormat header = null;
MessageFormat footer = null;
footer = new MessageFormat("{0}");
boolean fitWidth = true;
boolean showPrintDialog = true;
boolean interactive = true;
JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH
: JTable.PrintMode.NORMAL;
@SuppressWarnings("unused")
boolean complete=false;
header = new MessageFormat(headerField.getText());
complete = table.print(mode, header, footer,
showPrintDialog, null,
interactive, null);
if(complete)
JOptionPane.showMessageDialog(null, "Impression complète");
}
catch (PrinterException ex) {
ex.printStackTrace();
System.err.println("Printing error: "+ex.toString());
}
}
};
print.addActionListener(lst);
print.setAlignmentY(0.5f);
print.setMargin(new Insets(4,6,4,6));
tb.add(print);
tb.add(printThisPage);
if (pp_pf.getOrientation() == PageFormat.PORTRAIT)
{
formatButton = new JButton();
formatButton.setContentAreaFilled(false);
formatButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
formatButton.setNextFocusableComponent(print);
tooltipText = "Paysage";
formatButton.setToolTipText(tooltipText);
formatButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/paysage.png")));
}
else
{
formatButton = new JButton();
formatButton.setContentAreaFilled(false);
formatButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
formatButton.setNextFocusableComponent(print);
tooltipText = "Portrait";
formatButton.setToolTipText(tooltipText);
formatButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/portrait.png")));
}
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (pp_pf.getOrientation() == PageFormat.PORTRAIT) {
pp_pf.setOrientation(PageFormat.LANDSCAPE);
previewThePages(PageFormat.LANDSCAPE);
tooltipText = "Portrait";
formatButton.setToolTipText(tooltipText);
formatButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/portrait.png")));
} else {
pp_pf.setOrientation(PageFormat.PORTRAIT);
previewThePages(PageFormat.PORTRAIT);
tooltipText = "Paysage";
formatButton.setToolTipText(tooltipText);
formatButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/paysage.png")));
}
}
};
formatButton.addActionListener(lst);
formatButton.setAlignmentY(0.5f);
formatButton.setMargin(new Insets(4,6,4,6));
tb.add(formatButton);
if (shrink) {
shrinkButton = new JButton("Shrink to fit");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
bScallToFitOnePage = !bScallToFitOnePage;
previewThePages(pp_pf.getOrientation());
}
};
shrinkButton.addActionListener(lst);
shrinkButton.setAlignmentY(0.5f);
shrinkButton.setMargin(new Insets(4,6,4,6));
tb.add(shrinkButton);
}
zoomin.setContentAreaFilled(false);
zoomin.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
zoomin.setNextFocusableComponent(print);
tooltipText = "agrandir";
zoomin.setToolTipText(tooltipText);
zoomin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/zoomin.png")));
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = m_cbScale.getSelectedIndex();
i++;
if(i<=9)
m_cbScale.setSelectedIndex(i);
}
};
zoomin.addActionListener(lst);
zoomout.setContentAreaFilled(false);
zoomout.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
zoomout.setNextFocusableComponent(print);
tooltipText = "réduire";
zoomout.setToolTipText(tooltipText);
zoomout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/zoomout.png")));
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = m_cbScale.getSelectedIndex();
i--;
if(i>=0)
m_cbScale.setSelectedIndex(i);
}
};
zoomout.addActionListener(lst);
}
public int getDisplayScale() {
String str = m_cbScale.getSelectedItem().toString();
if (str.endsWith("%")) str = str.substring(0, str.length()-1);
str = str.trim();
int scale = 0;
try { scale = Integer.parseInt(str); }
catch (NumberFormatException ex) { return 25; }
return scale ;
}
public PrintPreview(Printable target,JTable table2) {
this(target, "Aperçu avant impression", false, table2);
}
public PrintPreview(Printable target, String title,JTable table2) {
this(target, title, false, table2);
}
public PrintPreview(Printable target, String title, boolean shrink,JTable table2) {
setTitle(title);
table=table2;
bScallToFitOnePage = false; // reset to default
PrinterJob prnJob = PrinterJob.getPrinterJob();
pp_pf = prnJob.defaultPage();
if (pp_pf.getHeight()==0 || pp_pf.getWidth()==0) {
System.err.println("Unable to determine default page size");
return;
}
setSize(670, 750);
m_target = target;
displayArea = null;
m_preview = null;
JToolBar tb = new JToolBar();
createButtons(tb, shrink);
String[] scales = { "10 %", "25 %", "50 %", "100 %", "125 %", "150 %", "175 %","200 %","225 %","250 %"};
m_cbScale = new JComboBox(scales);
m_cbScale.setSelectedIndex(3);
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread runner = new Thread(PrintPreview.this);
runner.start();
}
};
m_cbScale.addActionListener(lst);
m_cbScale.setMaximumSize(m_cbScale.getPreferredSize());
m_cbScale.setEditable(true);
tb.addSeparator();
tb.add(zoomout);
tb.add(m_cbScale);
tb.add(zoomin);
getContentPane().add(tb, BorderLayout.NORTH);
previewThePages(pp_pf.getOrientation());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(true);
setExtendedState(this.MAXIMIZED_BOTH);
setVisible(true);
}
public void run() {
int scale = getDisplayScale();
width = (int)(m_wPage*scale/100);
height = (int)(m_hPage*scale/100);
Component[] comps = m_preview.getComponents();
for (int k=0; k<comps.length; k++) {
if (!(comps[k] instanceof PagePreview))
continue;
PagePreview pp = (PagePreview)comps[k];
pp.setScaledSize(width, height);
}
m_preview.doLayout();
m_preview.getParent().getParent().validate();
}
class PreviewContainer extends JPanel
{
protected int H_GAP = 16;
protected int V_GAP = 10;
public Dimension getPreferredSize() {
int n = getComponentCount();
if (n == 0)
return new Dimension(H_GAP, V_GAP);
Component comp = getComponent(0);
Dimension dc = comp.getPreferredSize();
int w = dc.width;
int h = dc.height;
Dimension dp = getParent().getSize();
int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
int nRow = n/nCol;
if (nRow*nCol < n)
nRow++;
int ww = nCol*(w+H_GAP) + H_GAP;
int hh = nRow*(h+V_GAP) + V_GAP;
Insets ins = getInsets();
return new Dimension(ww+ins.left+ins.right,
hh+ins.top+ins.bottom);
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public void doLayout() {
Insets ins = getInsets();
int x = ins.left + H_GAP;
int y = ins.top + V_GAP;
int n = getComponentCount();
if (n == 0)
return;
Component comp = getComponent(0);
Dimension dc = comp.getPreferredSize();
int w = dc.width;
int h = dc.height;
Dimension dp = getParent().getSize();
int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
int nRow = n/nCol;
if (nRow*nCol < n)
nRow++;
int index = 0;
for (int k = 0; k<nRow; k++) {
for (int m = 0; m<nCol; m++) {
if (index >= n)
return;
comp = getComponent(index++);
comp.setBounds(x, y, w, h);
x += w+H_GAP;
}
y += h+V_GAP;
x = ins.left + H_GAP;
}
}
}
class PagePreview extends JPanel
{
protected int m_w;
protected int m_h;
protected Image m_source;
protected Image m_img;
public PagePreview(int w, int h, Image source) {
m_w = w;
m_h = h;
m_source= source;
m_img = m_source.getScaledInstance(m_w, m_h, Image.SCALE_SMOOTH);
m_img.flush();
setBackground(Color.white);
setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
}
public void setScaledSize(int w, int h) {
m_w = w;
m_h = h;
m_img = m_source.getScaledInstance(m_w, m_h, Image.SCALE_SMOOTH);
repaint();
}
public Dimension getPreferredSize() {
Insets ins = getInsets();
return new Dimension(m_w+ins.left+ins.right, m_h+ins.top+ins.bottom);
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public void paint(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(m_img, 0, 0, this);
paintBorder(g);
}
}
} |
Partager