OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 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.AbstractSheetXml;
import org.openconcerto.erp.generationDoc.DefaultNXDocumentPrinter;
import org.openconcerto.erp.generationDoc.ODTPrinterNX;
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.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.util.Arrays;
import java.util.List;

import javax.print.PrintService;
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.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.jopendocument.model.OpenDocument;
import org.jopendocument.panel.ODSViewerPanel;
import org.jopendocument.print.DefaultXMLDocumentPrinter;
import org.jopendocument.print.ODTPrinterXML;

public class PreviewFrame extends JFrame {

    private PreviewFrame(OpenDocument doc, String title) {
        super(title);
        final ODSViewerPanel viewerPanel = new ODSViewerPanel(doc, createDocumentPrinter());
        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);

        this.setContentPane(contentPane);
        this.setTitle(title);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void addTool(String n, Runnable r) {
        JButton b = new JButton(n);
        ((ODSViewerPanel) this.getContentPane()).addTool(b);
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                r.run();
            }
        });
    }

    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, null, null);
    }

    public static void show(File file, String actionName, Runnable r) {
        final OpenDocument doc = new OpenDocument(file);
        final String title = file.getName();
        if (SwingUtilities.isEventDispatchThread()) {
            PreviewFrame previewFrame = new PreviewFrame(doc, title);
            if (r != null) {
                previewFrame.addTool(actionName, r);
            }
            previewFrame.pack();
            previewFrame.setVisible(true);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    PreviewFrame previewFrame = new PreviewFrame(doc, title);
                    if (r != null) {
                        previewFrame.addTool(actionName, r);
                    }
                    previewFrame.pack();
                    previewFrame.setVisible(true);
                }
            });
        }

    }
}