OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.common.ui;
15
 
149 ilm 16
import org.openconcerto.erp.generationDoc.A4;
17
import org.openconcerto.erp.generationDoc.DefaultNXDocumentPrinter;
182 ilm 18
import org.openconcerto.utils.Action;
149 ilm 19
import org.openconcerto.utils.ExceptionHandler;
20
 
18 ilm 21
import java.awt.Dimension;
22
import java.awt.GraphicsEnvironment;
174 ilm 23
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionListener;
149 ilm 25
import java.awt.print.Paper;
26
import java.awt.print.PrinterJob;
18 ilm 27
import java.io.File;
182 ilm 28
import java.util.Collections;
149 ilm 29
import java.util.List;
18 ilm 30
 
149 ilm 31
import javax.print.attribute.Attribute;
32
import javax.print.attribute.HashPrintRequestAttributeSet;
33
import javax.print.attribute.Size2DSyntax;
34
import javax.print.attribute.standard.Copies;
35
import javax.print.attribute.standard.MediaPrintableArea;
36
import javax.print.attribute.standard.MediaSizeName;
174 ilm 37
import javax.swing.JButton;
18 ilm 38
import javax.swing.JFrame;
80 ilm 39
import javax.swing.SwingUtilities;
18 ilm 40
 
41
import org.jopendocument.model.OpenDocument;
42
import org.jopendocument.panel.ODSViewerPanel;
43
import org.jopendocument.print.DefaultXMLDocumentPrinter;
44
 
45
public class PreviewFrame extends JFrame {
46
 
80 ilm 47
    private PreviewFrame(OpenDocument doc, String title) {
48
        super(title);
174 ilm 49
        final ODSViewerPanel viewerPanel = new ODSViewerPanel(doc, createDocumentPrinter());
182 ilm 50
        viewerPanel.showSaveAsButton(false);
174 ilm 51
        this.setContentPane(viewerPanel);
52
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
83 ilm 53
    }
54
 
55
    public PreviewFrame(String title, String url, String odspXml) {
174 ilm 56
        final ODSViewerPanel contentPane = new ODSViewerPanel(url, odspXml, createDocumentPrinter(), true);
182 ilm 57
        contentPane.showSaveAsButton(false);
174 ilm 58
        this.setContentPane(contentPane);
83 ilm 59
        this.setTitle(title);
174 ilm 60
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
83 ilm 61
    }
62
 
182 ilm 63
    public void addTool(Action a) {
64
        JButton b = new JButton(a.getName());
174 ilm 65
        ((ODSViewerPanel) this.getContentPane()).addTool(b);
66
        b.addActionListener(new ActionListener() {
67
 
68
            @Override
69
            public void actionPerformed(ActionEvent e) {
182 ilm 70
                a.run(PreviewFrame.this);
174 ilm 71
            }
72
        });
73
    }
74
 
149 ilm 75
    public DefaultXMLDocumentPrinter createDocumentPrinter() {
76
        return new DefaultXMLDocumentPrinter() {
77
            public void print(List<OpenDocument> documents) {
78
 
79
                final double POINTS_PER_INCH = 72.0;
80
                // Printer configuration
81
                final PrinterJob printJob = PrinterJob.getPrinterJob();
82
 
83
                final HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
84
                // L'impression est forcée en A4, sur OpenSuse le format est en
85
                // Letter par défaut alors que l'imprimante est en A4 dans le système
86
                final MediaSizeName media = MediaSizeName.ISO_A4;
87
                attributes.add(media);
88
                Paper paper = new A4(0, 0);
89
 
90
                final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
91
                        (float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
92
                attributes.add(printableArea);
93
                // Print dialog
94
                boolean okToPrint = printJob.printDialog(attributes);
95
                final Attribute attribute = attributes.get(Copies.class);
96
                if (attribute != null) {
97
                    final Copies attributeCopies = (Copies) attribute;
98
                    final int value = attributeCopies.getValue();
99
                    printJob.setCopies(value);
100
                } else {
101
                    printJob.setCopies(1);
102
                }
103
                if (okToPrint) {
104
                    final Thread t = new Thread(new Runnable() {
105
                        @Override
106
                        public void run() {
107
                            try {
108
 
109
                                DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
110
                                printer.print(documents, printJob);
111
 
112
                            } catch (Exception e) {
113
                                ExceptionHandler.handle("Print error", e);
114
                            }
115
 
116
                        }
117
                    });
118
                    t.setName("PreviewFrame Print Thread");
119
                    t.setDaemon(true);
120
                    t.start();
121
                }
122
 
123
            }
124
 
125
        };
126
 
127
    }
128
 
174 ilm 129
    @Override
130
    public void pack() {
83 ilm 131
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
18 ilm 132
        this.setMaximizedBounds(ge.getMaximumWindowBounds());
83 ilm 133
        final Dimension maxD = ge.getMaximumWindowBounds().getSize();
18 ilm 134
        this.setMaximumSize(maxD);
174 ilm 135
        super.pack();
18 ilm 136
        Dimension d = this.getSize();
137
        if (d.width > maxD.width) {
138
            d.setSize(maxD.width, d.height);
139
        }
140
        if (d.height > maxD.height) {
141
            d.setSize(d.width, maxD.height);
142
        }
143
        this.setLocationRelativeTo(null);
144
    }
145
 
80 ilm 146
    public static void show(File file) {
182 ilm 147
        show(file, Collections.emptyList());
174 ilm 148
    }
149
 
182 ilm 150
    public static void show(File file, List<Action> actions) {
80 ilm 151
        final OpenDocument doc = new OpenDocument(file);
152
        final String title = file.getName();
153
        if (SwingUtilities.isEventDispatchThread()) {
174 ilm 154
            PreviewFrame previewFrame = new PreviewFrame(doc, title);
182 ilm 155
            for (Action a : actions) {
156
                previewFrame.addTool(a);
174 ilm 157
            }
158
            previewFrame.pack();
159
            previewFrame.setVisible(true);
80 ilm 160
        } else {
161
            SwingUtilities.invokeLater(new Runnable() {
162
                @Override
163
                public void run() {
174 ilm 164
                    PreviewFrame previewFrame = new PreviewFrame(doc, title);
182 ilm 165
                    for (Action a : actions) {
166
                        previewFrame.addTool(a);
174 ilm 167
                    }
168
                    previewFrame.pack();
169
                    previewFrame.setVisible(true);
80 ilm 170
                }
171
            });
172
        }
83 ilm 173
 
18 ilm 174
    }
175
}