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;
|
174 |
ilm |
24 |
import java.awt.event.ActionEvent;
|
|
|
25 |
import java.awt.event.ActionListener;
|
149 |
ilm |
26 |
import java.awt.print.Paper;
|
|
|
27 |
import java.awt.print.PrinterException;
|
|
|
28 |
import java.awt.print.PrinterJob;
|
18 |
ilm |
29 |
import java.io.File;
|
149 |
ilm |
30 |
import java.util.Arrays;
|
|
|
31 |
import java.util.List;
|
18 |
ilm |
32 |
|
149 |
ilm |
33 |
import javax.print.PrintService;
|
|
|
34 |
import javax.print.attribute.Attribute;
|
|
|
35 |
import javax.print.attribute.HashPrintRequestAttributeSet;
|
|
|
36 |
import javax.print.attribute.Size2DSyntax;
|
|
|
37 |
import javax.print.attribute.standard.Copies;
|
|
|
38 |
import javax.print.attribute.standard.MediaPrintableArea;
|
|
|
39 |
import javax.print.attribute.standard.MediaSizeName;
|
174 |
ilm |
40 |
import javax.swing.JButton;
|
|
|
41 |
import javax.swing.JComponent;
|
18 |
ilm |
42 |
import javax.swing.JFrame;
|
149 |
ilm |
43 |
import javax.swing.JOptionPane;
|
80 |
ilm |
44 |
import javax.swing.SwingUtilities;
|
18 |
ilm |
45 |
|
|
|
46 |
import org.jopendocument.model.OpenDocument;
|
|
|
47 |
import org.jopendocument.panel.ODSViewerPanel;
|
|
|
48 |
import org.jopendocument.print.DefaultXMLDocumentPrinter;
|
149 |
ilm |
49 |
import org.jopendocument.print.ODTPrinterXML;
|
18 |
ilm |
50 |
|
|
|
51 |
public class PreviewFrame extends JFrame {
|
|
|
52 |
|
80 |
ilm |
53 |
private PreviewFrame(OpenDocument doc, String title) {
|
|
|
54 |
super(title);
|
174 |
ilm |
55 |
final ODSViewerPanel viewerPanel = new ODSViewerPanel(doc, createDocumentPrinter());
|
|
|
56 |
this.setContentPane(viewerPanel);
|
|
|
57 |
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
83 |
ilm |
58 |
}
|
|
|
59 |
|
|
|
60 |
public PreviewFrame(String title, String url, String odspXml) {
|
174 |
ilm |
61 |
final ODSViewerPanel contentPane = new ODSViewerPanel(url, odspXml, createDocumentPrinter(), true);
|
|
|
62 |
|
|
|
63 |
this.setContentPane(contentPane);
|
83 |
ilm |
64 |
this.setTitle(title);
|
174 |
ilm |
65 |
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
83 |
ilm |
66 |
}
|
|
|
67 |
|
174 |
ilm |
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 |
}
|
|
|
77 |
});
|
|
|
78 |
}
|
|
|
79 |
|
149 |
ilm |
80 |
public DefaultXMLDocumentPrinter createDocumentPrinter() {
|
|
|
81 |
return new DefaultXMLDocumentPrinter() {
|
|
|
82 |
public void print(List<OpenDocument> documents) {
|
|
|
83 |
|
|
|
84 |
final double POINTS_PER_INCH = 72.0;
|
|
|
85 |
// Printer configuration
|
|
|
86 |
final PrinterJob printJob = PrinterJob.getPrinterJob();
|
|
|
87 |
|
|
|
88 |
final HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
|
|
|
89 |
// L'impression est forcée en A4, sur OpenSuse le format est en
|
|
|
90 |
// Letter par défaut alors que l'imprimante est en A4 dans le système
|
|
|
91 |
final MediaSizeName media = MediaSizeName.ISO_A4;
|
|
|
92 |
attributes.add(media);
|
|
|
93 |
Paper paper = new A4(0, 0);
|
|
|
94 |
|
|
|
95 |
final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
|
|
|
96 |
(float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
|
|
|
97 |
attributes.add(printableArea);
|
|
|
98 |
// Print dialog
|
|
|
99 |
boolean okToPrint = printJob.printDialog(attributes);
|
|
|
100 |
final Attribute attribute = attributes.get(Copies.class);
|
|
|
101 |
if (attribute != null) {
|
|
|
102 |
final Copies attributeCopies = (Copies) attribute;
|
|
|
103 |
final int value = attributeCopies.getValue();
|
|
|
104 |
printJob.setCopies(value);
|
|
|
105 |
} else {
|
|
|
106 |
printJob.setCopies(1);
|
|
|
107 |
}
|
|
|
108 |
if (okToPrint) {
|
|
|
109 |
final Thread t = new Thread(new Runnable() {
|
|
|
110 |
@Override
|
|
|
111 |
public void run() {
|
|
|
112 |
try {
|
|
|
113 |
|
|
|
114 |
DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
|
|
|
115 |
printer.print(documents, printJob);
|
|
|
116 |
|
|
|
117 |
} catch (Exception e) {
|
|
|
118 |
ExceptionHandler.handle("Print error", e);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
}
|
|
|
122 |
});
|
|
|
123 |
t.setName("PreviewFrame Print Thread");
|
|
|
124 |
t.setDaemon(true);
|
|
|
125 |
t.start();
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
};
|
|
|
131 |
|
|
|
132 |
}
|
|
|
133 |
|
174 |
ilm |
134 |
@Override
|
|
|
135 |
public void pack() {
|
83 |
ilm |
136 |
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
18 |
ilm |
137 |
this.setMaximizedBounds(ge.getMaximumWindowBounds());
|
83 |
ilm |
138 |
final Dimension maxD = ge.getMaximumWindowBounds().getSize();
|
18 |
ilm |
139 |
this.setMaximumSize(maxD);
|
174 |
ilm |
140 |
super.pack();
|
18 |
ilm |
141 |
Dimension d = this.getSize();
|
|
|
142 |
if (d.width > maxD.width) {
|
|
|
143 |
d.setSize(maxD.width, d.height);
|
|
|
144 |
}
|
|
|
145 |
if (d.height > maxD.height) {
|
|
|
146 |
d.setSize(d.width, maxD.height);
|
|
|
147 |
}
|
|
|
148 |
this.setLocationRelativeTo(null);
|
|
|
149 |
}
|
|
|
150 |
|
80 |
ilm |
151 |
public static void show(File file) {
|
174 |
ilm |
152 |
show(file, null, null);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
public static void show(File file, String actionName, Runnable r) {
|
80 |
ilm |
156 |
final OpenDocument doc = new OpenDocument(file);
|
|
|
157 |
final String title = file.getName();
|
|
|
158 |
if (SwingUtilities.isEventDispatchThread()) {
|
174 |
ilm |
159 |
PreviewFrame previewFrame = new PreviewFrame(doc, title);
|
|
|
160 |
if (r != null) {
|
|
|
161 |
previewFrame.addTool(actionName, r);
|
|
|
162 |
}
|
|
|
163 |
previewFrame.pack();
|
|
|
164 |
previewFrame.setVisible(true);
|
80 |
ilm |
165 |
} else {
|
|
|
166 |
SwingUtilities.invokeLater(new Runnable() {
|
|
|
167 |
@Override
|
|
|
168 |
public void run() {
|
174 |
ilm |
169 |
PreviewFrame previewFrame = new PreviewFrame(doc, title);
|
|
|
170 |
if (r != null) {
|
|
|
171 |
previewFrame.addTool(actionName, r);
|
|
|
172 |
}
|
|
|
173 |
previewFrame.pack();
|
|
|
174 |
previewFrame.setVisible(true);
|
80 |
ilm |
175 |
}
|
|
|
176 |
});
|
|
|
177 |
}
|
83 |
ilm |
178 |
|
18 |
ilm |
179 |
}
|
|
|
180 |
}
|