From 2af634d3576061d867542e729018f0de1b6604ca Mon Sep 17 00:00:00 2001 From: "Rafael C. Carrasco" Date: Tue, 3 Dec 2013 20:46:19 +0100 Subject: [PATCH] 1- Output file chooser now asks for confirmation in case of overwrite. 2- Preselected directory and file for output takes prefix from ocr input file with new extension "_report.html". --- src/main/java/eu/digitisation/MainGUI.java | 25 +++++--- .../digitisation/gui/OutputFileSelector.java | 63 +++++++++++++++++++ 2 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 src/main/java/eu/digitisation/gui/OutputFileSelector.java diff --git a/src/main/java/eu/digitisation/MainGUI.java b/src/main/java/eu/digitisation/MainGUI.java index c80d363..6b83a3c 100644 --- a/src/main/java/eu/digitisation/MainGUI.java +++ b/src/main/java/eu/digitisation/MainGUI.java @@ -18,6 +18,7 @@ package eu.digitisation; import eu.digitisation.gui.InputFileSelector; +import eu.digitisation.gui.OutputFileSelector; import eu.digitisation.gui.Pulldown; import eu.digitisation.ocr.Report; import java.awt.*; @@ -75,9 +76,6 @@ public MainGUI() { pane.add(trigger); repaint(); - - - } private boolean checkInputFiles() { @@ -119,13 +117,20 @@ public void actionPerformed(ActionEvent e) { ? System.getProperty("file.encoding") : encodingsMenu.choice(); boolean checked = checkInputFiles(); - if (checked) { - files[3] = choose("output.html"); - if (files[3] != null) { - Report.report(files[0], encoding, - files[1], encoding, - files[2], files[3]); - } + if (checked) { + File dir = files[1].getParentFile(); + String name = files[1].getName().replaceAll("\\.\\w+","") + + "_report.html"; + File prefile = new File(name); + OutputFileSelector selector = new OutputFileSelector(); + + files[3] = selector.choose(dir, prefile); + if (files[3] != null) { + Report.report(files[0], encoding, + files[1], encoding, + files[2], files[3]); + } + } } } diff --git a/src/main/java/eu/digitisation/gui/OutputFileSelector.java b/src/main/java/eu/digitisation/gui/OutputFileSelector.java new file mode 100644 index 0000000..877401f --- /dev/null +++ b/src/main/java/eu/digitisation/gui/OutputFileSelector.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2013 Universidad de Alicante + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +package eu.digitisation.gui; + +import java.io.File; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +/** + * File chooser with confirmation dialog to avoid accidental overwrite + * + * @author R.C.C. + */ +public class OutputFileSelector extends JFileChooser { + + private static final long serialVersionUID = 1L; + + public OutputFileSelector() { + super(); + } + + /** + * + * @param dir the default directory + * @param file the preselected file + * @return the selected file + */ + public File choose(File dir, File file) { + setCurrentDirectory(dir); + setSelectedFile(file); + int returnVal = showOpenDialog(OutputFileSelector.this); + + if (returnVal == JFileChooser.APPROVE_OPTION) { + file = getSelectedFile(); + if (file != null && file.exists()) { + int response = JOptionPane.showConfirmDialog(new JFrame().getContentPane(), + "The file " + file.getName() + + " already exists. Do you want to replace the existing file?", + "Overwrite file", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); + + return (response == JOptionPane.YES_NO_OPTION) ? file : null; + } + return file; + } + return null; + } +}