Dépôt officiel du code source de l'ERP OpenConcerto
Rev 174 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.common.ui;
import org.openconcerto.erp.generationDoc.A4;
import org.openconcerto.erp.generationDoc.DefaultNXDocumentPrinter;
import org.openconcerto.utils.Action;
import org.openconcerto.utils.ExceptionHandler;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.io.File;
import java.util.Collections;
import java.util.List;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.Size2DSyntax;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSizeName;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jopendocument.model.OpenDocument;
import org.jopendocument.panel.ODSViewerPanel;
import org.jopendocument.print.DefaultXMLDocumentPrinter;
public class PreviewFrame extends JFrame {
private PreviewFrame(OpenDocument doc, String title) {
super(title);
final ODSViewerPanel viewerPanel = new ODSViewerPanel(doc, createDocumentPrinter());
viewerPanel.showSaveAsButton(false);
this.setContentPane(viewerPanel);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public PreviewFrame(String title, String url, String odspXml) {
final ODSViewerPanel contentPane = new ODSViewerPanel(url, odspXml, createDocumentPrinter(), true);
contentPane.showSaveAsButton(false);
this.setContentPane(contentPane);
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void addTool(Action a) {
JButton b = new JButton(a.getName());
((ODSViewerPanel) this.getContentPane()).addTool(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
a.run(PreviewFrame.this);
}
});
}
public DefaultXMLDocumentPrinter createDocumentPrinter() {
return new DefaultXMLDocumentPrinter() {
public void print(List<OpenDocument> documents) {
final double POINTS_PER_INCH = 72.0;
// Printer configuration
final PrinterJob printJob = PrinterJob.getPrinterJob();
final HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
// L'impression est forcée en A4, sur OpenSuse le format est en
// Letter par défaut alors que l'imprimante est en A4 dans le système
final MediaSizeName media = MediaSizeName.ISO_A4;
attributes.add(media);
Paper paper = new A4(0, 0);
final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
(float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
attributes.add(printableArea);
// Print dialog
boolean okToPrint = printJob.printDialog(attributes);
final Attribute attribute = attributes.get(Copies.class);
if (attribute != null) {
final Copies attributeCopies = (Copies) attribute;
final int value = attributeCopies.getValue();
printJob.setCopies(value);
} else {
printJob.setCopies(1);
}
if (okToPrint) {
final Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
printer.print(documents, printJob);
} catch (Exception e) {
ExceptionHandler.handle("Print error", e);
}
}
});
t.setName("PreviewFrame Print Thread");
t.setDaemon(true);
t.start();
}
}
};
}
@Override
public void pack() {
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
this.setMaximizedBounds(ge.getMaximumWindowBounds());
final Dimension maxD = ge.getMaximumWindowBounds().getSize();
this.setMaximumSize(maxD);
super.pack();
Dimension d = this.getSize();
if (d.width > maxD.width) {
d.setSize(maxD.width, d.height);
}
if (d.height > maxD.height) {
d.setSize(d.width, maxD.height);
}
this.setLocationRelativeTo(null);
}
public static void show(File file) {
show(file, Collections.emptyList());
}
public static void show(File file, List<Action> actions) {
final OpenDocument doc = new OpenDocument(file);
final String title = file.getName();
if (SwingUtilities.isEventDispatchThread()) {
PreviewFrame previewFrame = new PreviewFrame(doc, title);
for (Action a : actions) {
previewFrame.addTool(a);
}
previewFrame.pack();
previewFrame.setVisible(true);
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
PreviewFrame previewFrame = new PreviewFrame(doc, title);
for (Action a : actions) {
previewFrame.addTool(a);
}
previewFrame.pack();
previewFrame.setVisible(true);
}
});
}
}
}