OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | Rev 174 | Go to most recent revision | 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
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.AbstractSheetXml;
18
import org.openconcerto.erp.generationDoc.DefaultNXDocumentPrinter;
19
import org.openconcerto.erp.generationDoc.ODTPrinterNX;
20
import org.openconcerto.utils.ExceptionHandler;
21
 
18 ilm 22
import java.awt.Dimension;
23
import java.awt.GraphicsEnvironment;
149 ilm 24
import java.awt.print.Paper;
25
import java.awt.print.PrinterException;
26
import java.awt.print.PrinterJob;
18 ilm 27
import java.io.File;
149 ilm 28
import java.util.Arrays;
29
import java.util.List;
18 ilm 30
 
149 ilm 31
import javax.print.PrintService;
32
import javax.print.attribute.Attribute;
33
import javax.print.attribute.HashPrintRequestAttributeSet;
34
import javax.print.attribute.Size2DSyntax;
35
import javax.print.attribute.standard.Copies;
36
import javax.print.attribute.standard.MediaPrintableArea;
37
import javax.print.attribute.standard.MediaSizeName;
18 ilm 38
import javax.swing.JFrame;
149 ilm 39
import javax.swing.JOptionPane;
80 ilm 40
import javax.swing.SwingUtilities;
18 ilm 41
 
42
import org.jopendocument.model.OpenDocument;
43
import org.jopendocument.panel.ODSViewerPanel;
44
import org.jopendocument.print.DefaultXMLDocumentPrinter;
149 ilm 45
import org.jopendocument.print.ODTPrinterXML;
18 ilm 46
 
47
public class PreviewFrame extends JFrame {
48
 
80 ilm 49
    private PreviewFrame(OpenDocument doc, String title) {
50
        super(title);
149 ilm 51
        this.setContentPane(new ODSViewerPanel(doc, createDocumentPrinter()));
83 ilm 52
        init();
53
    }
54
 
55
    public PreviewFrame(String title, String url, String odspXml) {
149 ilm 56
        this.setContentPane(new ODSViewerPanel(url, odspXml, createDocumentPrinter(), true));
83 ilm 57
        this.setTitle(title);
58
        init();
59
    }
60
 
149 ilm 61
    public DefaultXMLDocumentPrinter createDocumentPrinter() {
62
        return new DefaultXMLDocumentPrinter() {
63
            public void print(List<OpenDocument> documents) {
64
 
65
                final double POINTS_PER_INCH = 72.0;
66
                // Printer configuration
67
                final PrinterJob printJob = PrinterJob.getPrinterJob();
68
 
69
                final HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
70
                // 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
72
                final MediaSizeName media = MediaSizeName.ISO_A4;
73
                attributes.add(media);
74
                Paper paper = new A4(0, 0);
75
 
76
                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);
78
                attributes.add(printableArea);
79
                attributes.add(new Copies(2));
80
                // Print dialog
81
                boolean okToPrint = printJob.printDialog(attributes);
82
                final Attribute attribute = attributes.get(Copies.class);
83
                if (attribute != null) {
84
                    final Copies attributeCopies = (Copies) attribute;
85
                    final int value = attributeCopies.getValue();
86
                    printJob.setCopies(value);
87
                } else {
88
                    printJob.setCopies(1);
89
                }
90
                if (okToPrint) {
91
                    final Thread t = new Thread(new Runnable() {
92
                        @Override
93
                        public void run() {
94
                            try {
95
 
96
                                DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
97
                                printer.print(documents, printJob);
98
 
99
                            } catch (Exception e) {
100
                                ExceptionHandler.handle("Print error", e);
101
                            }
102
 
103
                        }
104
                    });
105
                    t.setName("PreviewFrame Print Thread");
106
                    t.setDaemon(true);
107
                    t.start();
108
                }
109
 
110
            }
111
 
112
        };
113
 
114
    }
115
 
83 ilm 116
    private void init() {
117
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
18 ilm 118
        this.setMaximizedBounds(ge.getMaximumWindowBounds());
83 ilm 119
        final Dimension maxD = ge.getMaximumWindowBounds().getSize();
18 ilm 120
        this.setMaximumSize(maxD);
121
        this.pack();
122
        Dimension d = this.getSize();
123
        if (d.width > maxD.width) {
124
            d.setSize(maxD.width, d.height);
125
        }
126
        if (d.height > maxD.height) {
127
            d.setSize(d.width, maxD.height);
128
        }
129
        this.setSize(d);
130
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
131
 
132
        this.setLocationRelativeTo(null);
133
    }
134
 
80 ilm 135
    public static void show(File file) {
136
        final OpenDocument doc = new OpenDocument(file);
137
        final String title = file.getName();
138
        if (SwingUtilities.isEventDispatchThread()) {
139
            new PreviewFrame(doc, title).setVisible(true);
140
        } else {
141
            SwingUtilities.invokeLater(new Runnable() {
142
                @Override
143
                public void run() {
144
                    new PreviewFrame(doc, title).setVisible(true);
145
                }
146
            });
147
        }
83 ilm 148
 
18 ilm 149
    }
150
}