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 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 174
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.common.ui;
14
 package org.openconcerto.erp.core.common.ui;
15
 
15
 
16
import org.openconcerto.erp.generationDoc.A4;
16
import org.openconcerto.erp.generationDoc.A4;
17
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
17
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
18
import org.openconcerto.erp.generationDoc.DefaultNXDocumentPrinter;
18
import org.openconcerto.erp.generationDoc.DefaultNXDocumentPrinter;
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;
29
import java.util.List;
31
import java.util.List;
30
 
32
 
31
import javax.print.PrintService;
33
import javax.print.PrintService;
32
import javax.print.attribute.Attribute;
34
import javax.print.attribute.Attribute;
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;
43
import org.jopendocument.panel.ODSViewerPanel;
47
import org.jopendocument.panel.ODSViewerPanel;
44
import org.jopendocument.print.DefaultXMLDocumentPrinter;
48
import org.jopendocument.print.DefaultXMLDocumentPrinter;
45
import org.jopendocument.print.ODTPrinterXML;
49
import org.jopendocument.print.ODTPrinterXML;
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) {
64
 
83
 
65
                final double POINTS_PER_INCH = 72.0;
84
                final double POINTS_PER_INCH = 72.0;
66
                // Printer configuration
85
                // Printer configuration
67
                final PrinterJob printJob = PrinterJob.getPrinterJob();
86
                final PrinterJob printJob = PrinterJob.getPrinterJob();
68
 
87
 
69
                final HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
88
                final HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
70
                // L'impression est forcée en A4, sur OpenSuse le format est en
89
                // L'impression est forcée en A4, sur OpenSuse le format est en
71
                // Letter par défaut alors que l'imprimante est en A4 dans le système
90
                // Letter par défaut alors que l'imprimante est en A4 dans le système
72
                final MediaSizeName media = MediaSizeName.ISO_A4;
91
                final MediaSizeName media = MediaSizeName.ISO_A4;
73
                attributes.add(media);
92
                attributes.add(media);
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;
85
                    final int value = attributeCopies.getValue();
103
                    final int value = attributeCopies.getValue();
86
                    printJob.setCopies(value);
104
                    printJob.setCopies(value);
87
                } else {
105
                } else {
88
                    printJob.setCopies(1);
106
                    printJob.setCopies(1);
89
                }
107
                }
90
                if (okToPrint) {
108
                if (okToPrint) {
91
                    final Thread t = new Thread(new Runnable() {
109
                    final Thread t = new Thread(new Runnable() {
92
                        @Override
110
                        @Override
93
                        public void run() {
111
                        public void run() {
94
                            try {
112
                            try {
95
 
113
 
96
                                DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
114
                                DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
97
                                printer.print(documents, printJob);
115
                                printer.print(documents, printJob);
98
 
116
 
99
                            } catch (Exception e) {
117
                            } catch (Exception e) {
100
                                ExceptionHandler.handle("Print error", e);
118
                                ExceptionHandler.handle("Print error", e);
101
                            }
119
                            }
102
 
120
 
103
                        }
121
                        }
104
                    });
122
                    });
105
                    t.setName("PreviewFrame Print Thread");
123
                    t.setName("PreviewFrame Print Thread");
106
                    t.setDaemon(true);
124
                    t.setDaemon(true);
107
                    t.start();
125
                    t.start();
108
                }
126
                }
109
 
127
 
110
            }
128
            }
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
    }
150
}
180
}