OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
137 ilm 1
package org.jopencalendar.ui;
2
 
3
import java.awt.event.ActionEvent;
4
import java.awt.print.PageFormat;
5
import java.awt.print.Paper;
6
import java.awt.print.PrinterException;
7
import java.awt.print.PrinterJob;
8
 
9
import javax.print.attribute.HashPrintRequestAttributeSet;
10
import javax.print.attribute.PrintRequestAttributeSet;
11
import javax.print.attribute.standard.OrientationRequested;
12
import javax.print.attribute.standard.PrintQuality;
13
import javax.swing.AbstractAction;
14
import javax.swing.Action;
15
import javax.swing.JComponent;
16
import javax.swing.JOptionPane;
17
 
18
public class PrintJComponentAction extends AbstractAction {
19
    private JComponent component;
20
 
21
    private final int pageFormat;
22
    private final String title;
23
 
24
    public PrintJComponentAction(JComponent view) {
25
        this(view, PageFormat.PORTRAIT, null);
26
    }
27
 
28
    public PrintJComponentAction(JComponent view, int pageFormat, String title) {
29
        this.component = view;
30
        putValue(Action.NAME, "Print");
31
        this.pageFormat = pageFormat;
32
        this.title = title;
33
    }
34
 
35
    @Override
36
    public void actionPerformed(ActionEvent e) {
37
        PrinterJob pj = PrinterJob.getPrinterJob();
38
 
39
        if (pj.printDialog()) {
40
            try {
41
 
42
                if (this.pageFormat == PageFormat.LANDSCAPE) {
43
 
44
                    final PageFormat pageFormat = getMinimumMarginPageFormat(pj);
45
                    pageFormat.setOrientation(PageFormat.LANDSCAPE);
46
 
47
                    pj.setPrintable(new JComponentPrintable(component, title), pageFormat);
48
                    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
49
                    printAttributes.add(PrintQuality.HIGH);
50
                    printAttributes.add(OrientationRequested.LANDSCAPE);
51
 
52
                    pj.print(printAttributes);
53
                } else {
54
                    pj.setPrintable(new JComponentPrintable(component, title));
55
                    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
56
                    printAttributes.add(PrintQuality.HIGH);
57
                    pj.print(printAttributes);
58
                }
59
            } catch (PrinterException exc) {
60
                JOptionPane.showMessageDialog(component, exc.getMessage(), "Printing error", JOptionPane.ERROR_MESSAGE);
61
                System.err.println(exc);
62
            }
63
        }
64
 
65
    }
66
 
67
    private PageFormat getMinimumMarginPageFormat(PrinterJob printJob) {
68
        PageFormat pf0 = printJob.defaultPage();
69
        PageFormat pf1 = (PageFormat) pf0.clone();
70
        Paper p = pf0.getPaper();
71
        p.setImageableArea(0, 0, pf0.getWidth(), pf0.getHeight());
72
        pf1.setPaper(p);
73
        PageFormat pf2 = printJob.validatePage(pf1);
74
        return pf2;
75
    }
76
}