/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.pdfbox.debugger.ui;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import java.awt.Component;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;

/**
 * @author Khyrul Bashar
 *
 * A Customized class that helps to open and save file. It uses JFileChooser to operate.
 */
public class FileOpenSaveDialog
{
    private static final JFileChooser fileChooser = new JFileChooser()
    {
        @Override
        public void approveSelection()
        {
            File selectedFile = getSelectedFile();
            if (selectedFile.exists() && getDialogType() == JFileChooser.SAVE_DIALOG)
            {
                int result = JOptionPane.showConfirmDialog(this,
                    "Do you want to overwrite?",
                    "File already exists",
                    JOptionPane.YES_NO_OPTION);
                if (result != JOptionPane.YES_OPTION)
                {
                    cancelSelection();
                    return;
                }
            }
            super.approveSelection();
        }
    };
    private final Component mainUI;

    /**
     * Constructor.
     * @param parentUI the main UI (JFrame) on top of which File open/save dialog should open.
     * @param fileFilter file Filter, null is allowed when no filter is applicable.
     */
    public FileOpenSaveDialog(Component parentUI, FileFilter fileFilter)
    {
        mainUI = parentUI;
        fileChooser.resetChoosableFileFilters();
        fileChooser.setFileFilter(fileFilter);
    }

    /**
     * Saves data into a file after the user is prompted to choose the destination.
     *
     * @param bytes byte array to be saved in a file.
     * @param extension file extension.
     * @return true if the file is saved successfully or false if failed.
     * @throws IOException if there is an error in creation of the file.
     */
    public boolean saveFile(byte[] bytes, String extension) throws IOException
    {
        int result = fileChooser.showSaveDialog(mainUI);
        if (result == JFileChooser.APPROVE_OPTION)
        {
            String filename = fileChooser.getSelectedFile().getAbsolutePath();
            if (extension != null && !filename.endsWith(extension))
            {
                filename += "." + extension;
            }

            try (FileOutputStream outputStream = new FileOutputStream(filename))
            {
                outputStream.write(bytes);
            }
            return true;
        }
        return false;
    }

    /**
     * Saves document into a .pdf file after the user is prompted to choose the destination.
     *
     * @param document document to be saved in a .pdf file.
     * @param extension file extension.
     * @return true if the file is saved successfully or false if failed.
     * @throws IOException if there is an error in creation of the file.
     */
    public boolean saveDocument(PDDocument document, String extension) throws IOException
    {
        int result = fileChooser.showSaveDialog(mainUI);
        if (result == JFileChooser.APPROVE_OPTION)
        {
            String filename = fileChooser.getSelectedFile().getAbsolutePath();
            if (!filename.endsWith(extension))
            {
                filename += "." + extension;
            }
            document.setAllSecurityToBeRemoved(true);
            document.save(filename);
            return true;
        }
        return false;
    }

    /**
     * open a file prompting user to select the file.
     * @return the file opened.
     * @throws IOException if there is error in opening the file.
     */
    public File openFile() throws IOException
    {
        int result = fileChooser.showOpenDialog(mainUI);
        if (result == JFileChooser.APPROVE_OPTION)
        {
            return fileChooser.getSelectedFile();
        }
        return null;
    }
}

@@ -1008,32 +1008,27 @@
 java.io.
-FileOutputStream
+IOException
 ;%0Aimport
@@ -1029,38 +1029,66 @@
 import java.
+n
 io.
-IOException
+file.Files;%0Aimport java.nio.file.Paths
 ;%0Aimport org
@@ -1282,16 +1282,53 @@
 ialog%0A%7B%0A
+    private final Component mainUI;%0A%0A
     priv
@@ -1357,27 +1357,28 @@
 Chooser 
-fileChooser
+FILE_CHOOSER
  = new J
@@ -2049,44 +2049,8 @@
   %7D;
-%0A    private final Component mainUI;
 %0A%0A  
@@ -2376,27 +2376,28 @@
         
-fileChooser
+FILE_CHOOSER
 .resetCh
@@ -2426,27 +2426,28 @@
         
-fileChooser
+FILE_CHOOSER
 .setFile
@@ -2923,35 +2923,36 @@
 nt result = 
-fileChooser
+FILE_CHOOSER
 .showSaveDia
@@ -3051,35 +3051,36 @@
  filename = 
-fileChooser
+FILE_CHOOSER
 .getSelected
@@ -3242,25 +3242,24 @@
           %7D%0A
-%0A
             
@@ -3262,147 +3262,48 @@
     
-try (FileOutputStream outputStream = new FileOutputStream(filename))%0A            %7B%0A                outputStream.write(bytes);%0A            %7D
+Files.write(Paths.get(filename), bytes);
 %0A   
@@ -3839,35 +3839,36 @@
 nt result = 
-fileChooser
+FILE_CHOOSER
 .showSaveDia
@@ -3971,27 +3971,28 @@
 ename = 
-fileChooser
+FILE_CHOOSER
 .getSele
@@ -4537,27 +4537,28 @@
 esult = 
-fileChooser
+FILE_CHOOSER
 .showOpe
@@ -4658,19 +4658,20 @@
 urn 
-fileChooser
+FILE_CHOOSER
 .get

use java conventions; use jdk7 method