OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 174
Line 19... Line 19...
19
import org.openconcerto.erp.generationDoc.ODTPrinterNX;
19
import org.openconcerto.erp.generationDoc.ODTPrinterNX;
20
import org.openconcerto.utils.ExceptionHandler;
20
import org.openconcerto.utils.ExceptionHandler;
21
 
21
 
22
import java.awt.Dimension;
22
import java.awt.Dimension;
23
import java.awt.GraphicsEnvironment;
23
import java.awt.GraphicsEnvironment;
-
 
24
import java.awt.event.ActionEvent;
-
 
25
import java.awt.event.ActionListener;
24
import java.awt.print.Paper;
26
import java.awt.print.Paper;
25
import java.awt.print.PrinterException;
27
import java.awt.print.PrinterException;
26
import java.awt.print.PrinterJob;
28
import java.awt.print.PrinterJob;
27
import java.io.File;
29
import java.io.File;
28
import java.util.Arrays;
30
import java.util.Arrays;
Line 33... Line 35...
33
import javax.print.attribute.HashPrintRequestAttributeSet;
35
import javax.print.attribute.HashPrintRequestAttributeSet;
34
import javax.print.attribute.Size2DSyntax;
36
import javax.print.attribute.Size2DSyntax;
35
import javax.print.attribute.standard.Copies;
37
import javax.print.attribute.standard.Copies;
36
import javax.print.attribute.standard.MediaPrintableArea;
38
import javax.print.attribute.standard.MediaPrintableArea;
37
import javax.print.attribute.standard.MediaSizeName;
39
import javax.print.attribute.standard.MediaSizeName;
-
 
40
import javax.swing.JButton;
-
 
41
import javax.swing.JComponent;
38
import javax.swing.JFrame;
42
import javax.swing.JFrame;
39
import javax.swing.JOptionPane;
43
import javax.swing.JOptionPane;
40
import javax.swing.SwingUtilities;
44
import javax.swing.SwingUtilities;
41
 
45
 
42
import org.jopendocument.model.OpenDocument;
46
import org.jopendocument.model.OpenDocument;
Line 46... Line 50...
46
 
50
 
47
public class PreviewFrame extends JFrame {
51
public class PreviewFrame extends JFrame {
48
 
52
 
49
    private PreviewFrame(OpenDocument doc, String title) {
53
    private PreviewFrame(OpenDocument doc, String title) {
50
        super(title);
54
        super(title);
51
        this.setContentPane(new ODSViewerPanel(doc, createDocumentPrinter()));
55
        final ODSViewerPanel viewerPanel = new ODSViewerPanel(doc, createDocumentPrinter());
52
        init();
56
        this.setContentPane(viewerPanel);
-
 
57
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
53
    }
58
    }
54
 
59
 
55
    public PreviewFrame(String title, String url, String odspXml) {
60
    public PreviewFrame(String title, String url, String odspXml) {
56
        this.setContentPane(new ODSViewerPanel(url, odspXml, createDocumentPrinter(), true));
61
        final ODSViewerPanel contentPane = new ODSViewerPanel(url, odspXml, createDocumentPrinter(), true);
-
 
62
 
-
 
63
        this.setContentPane(contentPane);
57
        this.setTitle(title);
64
        this.setTitle(title);
-
 
65
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-
 
66
    }
-
 
67
 
-
 
68
    public void addTool(String n, Runnable r) {
-
 
69
        JButton b = new JButton(n);
-
 
70
        ((ODSViewerPanel) this.getContentPane()).addTool(b);
-
 
71
        b.addActionListener(new ActionListener() {
-
 
72
 
-
 
73
            @Override
-
 
74
            public void actionPerformed(ActionEvent e) {
-
 
75
                r.run();
-
 
76
            }
58
        init();
77
        });
59
    }
78
    }
60
 
79
 
61
    public DefaultXMLDocumentPrinter createDocumentPrinter() {
80
    public DefaultXMLDocumentPrinter createDocumentPrinter() {
62
        return new DefaultXMLDocumentPrinter() {
81
        return new DefaultXMLDocumentPrinter() {
63
            public void print(List<OpenDocument> documents) {
82
            public void print(List<OpenDocument> documents) {
Line 74... Line 93...
74
                Paper paper = new A4(0, 0);
93
                Paper paper = new A4(0, 0);
75
 
94
 
76
                final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
95
                final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
77
                        (float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
96
                        (float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
78
                attributes.add(printableArea);
97
                attributes.add(printableArea);
79
                attributes.add(new Copies(2));
-
 
80
                // Print dialog
98
                // Print dialog
81
                boolean okToPrint = printJob.printDialog(attributes);
99
                boolean okToPrint = printJob.printDialog(attributes);
82
                final Attribute attribute = attributes.get(Copies.class);
100
                final Attribute attribute = attributes.get(Copies.class);
83
                if (attribute != null) {
101
                if (attribute != null) {
84
                    final Copies attributeCopies = (Copies) attribute;
102
                    final Copies attributeCopies = (Copies) attribute;
Line 111... Line 129...
111
 
129
 
112
        };
130
        };
113
 
131
 
114
    }
132
    }
115
 
133
 
-
 
134
    @Override
116
    private void init() {
135
    public void pack() {
117
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
136
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
118
        this.setMaximizedBounds(ge.getMaximumWindowBounds());
137
        this.setMaximizedBounds(ge.getMaximumWindowBounds());
119
        final Dimension maxD = ge.getMaximumWindowBounds().getSize();
138
        final Dimension maxD = ge.getMaximumWindowBounds().getSize();
120
        this.setMaximumSize(maxD);
139
        this.setMaximumSize(maxD);
121
        this.pack();
140
        super.pack();
122
        Dimension d = this.getSize();
141
        Dimension d = this.getSize();
123
        if (d.width > maxD.width) {
142
        if (d.width > maxD.width) {
124
            d.setSize(maxD.width, d.height);
143
            d.setSize(maxD.width, d.height);
125
        }
144
        }
126
        if (d.height > maxD.height) {
145
        if (d.height > maxD.height) {
127
            d.setSize(d.width, maxD.height);
146
            d.setSize(d.width, maxD.height);
128
        }
147
        }
129
        this.setSize(d);
-
 
130
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-
 
131
 
-
 
132
        this.setLocationRelativeTo(null);
148
        this.setLocationRelativeTo(null);
133
    }
149
    }
134
 
150
 
135
    public static void show(File file) {
151
    public static void show(File file) {
-
 
152
        show(file, null, null);
-
 
153
    }
-
 
154
 
-
 
155
    public static void show(File file, String actionName, Runnable r) {
136
        final OpenDocument doc = new OpenDocument(file);
156
        final OpenDocument doc = new OpenDocument(file);
137
        final String title = file.getName();
157
        final String title = file.getName();
138
        if (SwingUtilities.isEventDispatchThread()) {
158
        if (SwingUtilities.isEventDispatchThread()) {
-
 
159
            PreviewFrame previewFrame = new PreviewFrame(doc, title);
-
 
160
            if (r != null) {
-
 
161
                previewFrame.addTool(actionName, r);
-
 
162
            }
-
 
163
            previewFrame.pack();
139
            new PreviewFrame(doc, title).setVisible(true);
164
            previewFrame.setVisible(true);
140
        } else {
165
        } else {
141
            SwingUtilities.invokeLater(new Runnable() {
166
            SwingUtilities.invokeLater(new Runnable() {
142
                @Override
167
                @Override
143
                public void run() {
168
                public void run() {
-
 
169
                    PreviewFrame previewFrame = new PreviewFrame(doc, title);
-
 
170
                    if (r != null) {
-
 
171
                        previewFrame.addTool(actionName, r);
-
 
172
                    }
-
 
173
                    previewFrame.pack();
144
                    new PreviewFrame(doc, title).setVisible(true);
174
                    previewFrame.setVisible(true);
145
                }
175
                }
146
            });
176
            });
147
        }
177
        }
148
 
178
 
149
    }
179
    }