From 1fc0997f71d59ac756ae5751ee75b247316cfcef Mon Sep 17 00:00:00 2001 From: Petri Laarne Date: Sun, 1 Jan 2017 17:36:08 +0200 Subject: [PATCH] Do not delete the source if source = destination While the previous version could handle the case, it only did so after deleting the destination file, therefore causing data loss. --- app/src/processing/app/SketchController.java | 91 ++++++++++---------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/app/src/processing/app/SketchController.java b/app/src/processing/app/SketchController.java index 086d4dae9..3448ad1a8 100644 --- a/app/src/processing/app/SketchController.java +++ b/app/src/processing/app/SketchController.java @@ -495,61 +495,64 @@ public class SketchController { isData = true; } - // check whether this file already exists - if (destFile.exists()) { - Object[] options = { tr("OK"), tr("Cancel") }; - String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename); - int result = JOptionPane.showOptionDialog(editor, - prompt, - tr("Replace"), - JOptionPane.YES_NO_OPTION, - JOptionPane.QUESTION_MESSAGE, - null, - options, - options[0]); - if (result == JOptionPane.YES_OPTION) { - replacement = true; - } else { - return false; - } - } - - // If it's a replacement, delete the old file first, - // otherwise case changes will not be preserved. - // http://dev.processing.org/bugs/show_bug.cgi?id=969 - if (replacement) { - boolean muchSuccess = destFile.delete(); - if (!muchSuccess) { - Base.showWarning(tr("Error adding file"), - I18n.format(tr("Could not delete the existing ''{0}'' file."), filename), - null); - return false; - } - } - - // make sure they aren't the same file - if (isData && sourceFile.equals(destFile)) { - Base.showWarning(tr("You can't fool me"), - tr("This file has already been copied to the\n" + - "location from which where you're trying to add it.\n" + - "I ain't not doin nuthin'."), null); - return false; - } - - // in case the user is "adding" the code in an attempt - // to update the sketch's tabs if (!sourceFile.equals(destFile)) { + // The typical case here is adding a file from somewhere else. + // This however fails if the source and destination are equal + + // check whether this file already exists + if (destFile.exists()) { + Object[] options = { tr("OK"), tr("Cancel") }; + String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename); + int result = JOptionPane.showOptionDialog(editor, + prompt, + tr("Replace"), + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[0]); + if (result == JOptionPane.YES_OPTION) { + replacement = true; + } else { + return false; + } + } + + // If it's a replacement, delete the old file first, + // otherwise case changes will not be preserved. + // http://dev.processing.org/bugs/show_bug.cgi?id=969 + if (replacement) { + if (!destFile.delete()) { + Base.showWarning(tr("Error adding file"), + I18n.format(tr("Could not delete the existing ''{0}'' file."), filename), + null); + return false; + } + } + + // perform the copy try { Base.copyFile(sourceFile, destFile); } catch (IOException e) { Base.showWarning(tr("Error adding file"), I18n.format(tr("Could not add ''{0}'' to the sketch."), filename), - e); + e); return false; } } + else { + // If the source and destination are equal, a code file is handled + // - as a replacement, if there is a corresponding tab, + // (eg. user wants to update the file after modifying it outside the editor) + // - as an addition, otherwise. + // (eg. the user copied the file to the sketch folder and wants to edit it) + // For a data file, this is a no-op. + if (editor.findTabIndex(destFile) >= 0) + replacement = true; + } + // open/refresh the tab if (!isData) { int tabIndex; if (replacement) {