Java JFrame writeOrAbort(String path, JFrame frame)

Here you can find the source of writeOrAbort(String path, JFrame frame)

Description

Return if yes or not a file should be written.

License

Open Source License

Parameter

Parameter Description
path Absolute path
frame Frame source of the dialog box

Return

True if the user decided to erase an existing file.

Declaration

public static boolean writeOrAbort(String path, JFrame frame) 

Method Source Code


//package com.java2s;
/*//from w ww. ja  v  a  2  s  . c  om
Copyright (c) 2008-2010 Daniel Marbach & Thomas Schaffter
    
We release this software open source under an MIT license (see below). If this
software was useful for your scientific work, please cite our paper(s) listed
on http://gnw.sourceforge.net.
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Main {
    /**
     * Return if yes or not a file should be written.
     * @param path Absolute path
     * @param frame Frame source of the dialog box
     * @return True if the user decided to erase an existing file.
     */
    public static boolean writeOrAbort(String path, JFrame frame) {
        // Check if the file already exists
        if (fileAlreadyExist(path)) {
            int n = JOptionPane.showConfirmDialog(
                    //               GnwGuiSettings.getInstance().getGnwGui().getFrame(),
                    frame,
                    path + "\n\n" + "The selected filename already exists. If you\n"
                            + "continue, the contents of the existing file will\n" + "be replaced.\n" + "\n"
                            + "Do you want to continue?",
                    "Replace file", JOptionPane.YES_NO_OPTION);

            if (n == JOptionPane.YES_OPTION)
                return true; // If the user selected YES
            else
                return false;
        } else
            return true;
    }

    /**
     * Return true if the given path points on an already existing file.
     * @param path
     * @return Return true if there is already an existing file at the location defined by
     * the input path.
     */
    public static boolean fileAlreadyExist(String path) {
        File file = new File(path);
        return file.exists();
    }
}

Related

  1. toggleOsXFullScreen(JFrame frame)
  2. unpackArchive(URL url, File targetDir, JFrame frame, ProgressMonitor progressMonitor)
  3. waitFor(JFrame popup)
  4. wrapPanelInFrame(JFrame frame, JPanel panel, String caption, int width, int height)
  5. write(JFrame frame, String ext, String desc, String text)