All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.examples.viewer.commands.SaveForm Maven / Gradle / Ivy

There is a newer version: 7.15.25
Show newest version
/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2017 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
 @LICENSE@
 *
 * ---------------
 * SaveForm.java
 * ---------------
 */
package org.jpedal.examples.viewer.commands;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.jpedal.PdfDecoderInt;
import org.jpedal.examples.viewer.Values;
import org.jpedal.examples.viewer.utils.FileFilterer;
import org.jpedal.examples.viewer.utils.ItextFunctions;
import org.jpedal.external.ExternalHandlers;
import org.jpedal.gui.GUIFactory;
import org.jpedal.objects.acroforms.ReturnValues;
import org.jpedal.utils.Messages;

/**
 * Saves the form and sends data to database server
 */
@SuppressWarnings({"UnusedAssignment", "PMD"})
public class SaveForm {

    public static void execute(final Object[] args, final GUIFactory currentGUI, final PdfDecoderInt decode_pdf, final Values commonValues) {
        if (args == null) {
            saveChangedForm(currentGUI, decode_pdf, commonValues);
        }
    }

    /**
     * add listeners to forms to track changes - could also do other tasks like
     * send data to database server
     */
    public static void saveChangedForm(final GUIFactory currentGUI, final PdfDecoderInt decode_pdf, final Values commonValues) {
        final org.jpedal.objects.acroforms.AcroRenderer formRenderer = decode_pdf.getFormRenderer();

        if (formRenderer == null) {
            return;
        }

        final Object[] names = formRenderer.getFormComponents(null, ReturnValues.FORM_NAMES, -1);

        if (names == null) {
            currentGUI.showMessageDialog(Messages.getMessage("PdfViewer.NoFields"));
        } else {
            /*
             * create the file chooser to select the file
             */
            File file;
            String fileToSave = "";
            boolean finished = false;
            while (!finished) {
                final JFileChooser chooser = new JFileChooser(commonValues.getInputDir());
                chooser.setSelectedFile(new File(commonValues.getInputDir() + '/' + commonValues.getSelectedFile()));
                chooser.addChoosableFileFilter(new FileFilterer(new String[]{"pdf"}, "Pdf (*.pdf)"));
                chooser.addChoosableFileFilter(new FileFilterer(new String[]{"fdf"}, "fdf (*.fdf)"));
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                //set default name to current file name
                final int approved = chooser.showSaveDialog(null);
                if (approved == JFileChooser.APPROVE_OPTION) {
                    file = chooser.getSelectedFile();
                    fileToSave = file.getAbsolutePath();

                    if (!fileToSave.endsWith(".pdf")) {
                        fileToSave += ".pdf";
                        file = new File(fileToSave);
                    }

                    if (fileToSave.equals(commonValues.getSelectedFile())) {
                        currentGUI.showMessageDialog(Messages.getMessage("PdfViewerMessage.SaveError"));
                        continue;
                    }

                    if (file.exists()) {
                        final int n = currentGUI.showConfirmDialog(fileToSave + '\n'
                                        + Messages.getMessage("PdfViewerMessage.FileAlreadyExists") + ".\n"
                                        + Messages.getMessage("PdfViewerMessage.ConfirmResave"),
                                Messages.getMessage("PdfViewerMessage.Resave"), JOptionPane.YES_NO_OPTION);
                        if (n == 1) {
                            continue;
                        }
                    }
                    finished = true;
                } else {
                    return;
                }
            }

            if (currentGUI.getAnnotationPanel().annotationAdded()) {
                currentGUI.getAnnotationPanel().saveAnnotations(commonValues.getSelectedFile(), fileToSave);
            }

            if (ExternalHandlers.isITextPresent()) {
//                final ItextFunctions itextFunctions = new ItextFunctions(currentGUI, commonValues.getSelectedFile(), decode_pdf);
                ItextFunctions.saveFormsData(fileToSave);
            }
            
            /*
             * reset flag and graphical clue
             */
            commonValues.setFormsChanged(false);
            currentGUI.setViewerTitle(null);

        }
    }

    /**
     * warns user forms unsaved and offers save option
     */
    public static void handleUnsaveForms(final GUIFactory currentGUI, final Values commonValues, final PdfDecoderInt decode_pdf) {

        //OLD FORM CHANGE CODE
        if (commonValues.isFormsChanged()) {
            final int n = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerFormsUnsavedOptions.message"), Messages.getMessage("PdfViewerFormsUnsavedWarning.message"), JOptionPane.YES_NO_OPTION);

            if (n == JOptionPane.YES_OPTION) {
                SaveForm.saveChangedForm(currentGUI, decode_pdf, commonValues);
            }
        }

        commonValues.setFormsChanged(false);
        currentGUI.setViewerTitle(null);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy