data.core.ui.UiFileChooser.java Source code

Java tutorial

Introduction

Here is the source code for data.core.ui.UiFileChooser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data.core.ui;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import data.Constants;
import data.PackageFunctions;
import data.core.api.ApiFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Ryno Laptop
 */
public class UiFileChooser extends PackageFunctions {

    //--------------------------------------------------------------------------
    //properties
    //--------------------------------------------------------------------------
    private String filename;
    private File template;
    private String html;
    private String defaultDirectory;
    private JFileChooser saveFile;

    //--------------------------------------------------------------------------
    //constructor
    //--------------------------------------------------------------------------
    public UiFileChooser(File template) {
        this.template = template;
        this.filename = ApiFile.getRandomFilename("pdf", Constants.DIR_DOCUMENTS + "/export_");
        this.defaultDirectory = Constants.DIR_DOCUMENTS;
        this.saveFile = new JFileChooser();
    }

    //--------------------------------------------------------------------------
    public UiFileChooser(File template, String filename) {
        this.template = template;
        this.filename = filename;
        this.defaultDirectory = Constants.DIR_DOCUMENTS;
        this.saveFile = new JFileChooser();
    }

    //--------------------------------------------------------------------------
    public UiFileChooser(File template, String filename, String defaultDirectory) {
        this.template = template;
        this.filename = filename;
        this.defaultDirectory = defaultDirectory;
        this.saveFile = new JFileChooser();
    }

    //--------------------------------------------------------------------------
    public UiFileChooser(String html) {
        this.html = html;
        this.filename = ApiFile.getRandomFilename("pdf", Constants.DIR_DOCUMENTS + "/export_");
        this.defaultDirectory = Constants.DIR_DOCUMENTS;
        this.saveFile = new JFileChooser();
    }

    //--------------------------------------------------------------------------
    public UiFileChooser(String html, String filename) {
        this.html = html;
        this.filename = filename;
        this.defaultDirectory = Constants.DIR_DOCUMENTS;
        this.saveFile = new JFileChooser();
    }

    //--------------------------------------------------------------------------
    public UiFileChooser(String html, String filename, String defaultDirectory) {
        this.html = html;
        this.filename = filename;
        this.defaultDirectory = defaultDirectory;
        this.saveFile = new JFileChooser();
    }

    //--------------------------------------------------------------------------
    public void validateProperties() {
        if (this.template == null && this.html == null) {
            System.err.println("No html or file template found");
        }
    }

    //--------------------------------------------------------------------------
    public void setFileChooser(JFileChooser saveFile) {
        this.saveFile = saveFile;
    }

    //--------------------------------------------------------------------------
    public JFileChooser getFileChooser() {
        return this.saveFile;
    }

    //--------------------------------------------------------------------------
    public void renderSaveComponent() {
        this.validateProperties();
        File file = new File(this.filename);
        this.saveFile.setSelectedFile(file);
        this.saveFile.setFileFilter(new FileNameExtensionFilter("pdf file", "pdf"));
        if (this.saveFile.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            String selectedDir = this.saveFile.getCurrentDirectory().toString();
            try {
                try (OutputStream fileStream = new FileOutputStream(
                        new File(selectedDir + "/" + saveFile.getSelectedFile().getName()))) {
                    Document document = new Document();
                    PdfWriter writer = PdfWriter.getInstance(document, fileStream);
                    document.open();
                    if (this.template != null) {
                        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                                new FileInputStream(this.template));
                    } else if (this.html != null) {
                        InputStream is = new ByteArrayInputStream(this.html.getBytes());
                        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
                    } else {
                        System.err.println("No html or file template found in render component");
                    }

                    document.close();
                    this.saveFile.setSelectedFile(file);
                    this.saveFile.setCurrentDirectory(file);
                }

            } catch (IOException | DocumentException e) {
            }
        }
    }
    //--------------------------------------------------------------------------
}