Skip to content

Commit 1fc0997

Browse files
polsyscmaglie
authored andcommitted
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.
1 parent d3b3714 commit 1fc0997

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

Diff for: app/src/processing/app/SketchController.java

+44-41
Original file line numberDiff line numberDiff line change
@@ -495,61 +495,64 @@ public boolean addFile(File sourceFile) {
495495
isData = true;
496496
}
497497

498-
// check whether this file already exists
499-
if (destFile.exists()) {
500-
Object[] options = { tr("OK"), tr("Cancel") };
501-
String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename);
502-
int result = JOptionPane.showOptionDialog(editor,
503-
prompt,
504-
tr("Replace"),
505-
JOptionPane.YES_NO_OPTION,
506-
JOptionPane.QUESTION_MESSAGE,
507-
null,
508-
options,
509-
options[0]);
510-
if (result == JOptionPane.YES_OPTION) {
511-
replacement = true;
512-
} else {
513-
return false;
514-
}
515-
}
498+
if (!sourceFile.equals(destFile)) {
499+
// The typical case here is adding a file from somewhere else.
500+
// This however fails if the source and destination are equal
516501

517-
// If it's a replacement, delete the old file first,
518-
// otherwise case changes will not be preserved.
519-
// http://dev.processing.org/bugs/show_bug.cgi?id=969
520-
if (replacement) {
521-
boolean muchSuccess = destFile.delete();
522-
if (!muchSuccess) {
523-
Base.showWarning(tr("Error adding file"),
524-
I18n.format(tr("Could not delete the existing ''{0}'' file."), filename),
525-
null);
526-
return false;
502+
// check whether this file already exists
503+
if (destFile.exists()) {
504+
Object[] options = { tr("OK"), tr("Cancel") };
505+
String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename);
506+
int result = JOptionPane.showOptionDialog(editor,
507+
prompt,
508+
tr("Replace"),
509+
JOptionPane.YES_NO_OPTION,
510+
JOptionPane.QUESTION_MESSAGE,
511+
null,
512+
options,
513+
options[0]);
514+
if (result == JOptionPane.YES_OPTION) {
515+
replacement = true;
516+
} else {
517+
return false;
518+
}
527519
}
528-
}
529520

530-
// make sure they aren't the same file
531-
if (isData && sourceFile.equals(destFile)) {
532-
Base.showWarning(tr("You can't fool me"),
533-
tr("This file has already been copied to the\n" +
534-
"location from which where you're trying to add it.\n" +
535-
"I ain't not doin nuthin'."), null);
536-
return false;
537-
}
521+
// If it's a replacement, delete the old file first,
522+
// otherwise case changes will not be preserved.
523+
// http://dev.processing.org/bugs/show_bug.cgi?id=969
524+
if (replacement) {
525+
if (!destFile.delete()) {
526+
Base.showWarning(tr("Error adding file"),
527+
I18n.format(tr("Could not delete the existing ''{0}'' file."), filename),
528+
null);
529+
return false;
530+
}
531+
}
538532

539-
// in case the user is "adding" the code in an attempt
540-
// to update the sketch's tabs
541-
if (!sourceFile.equals(destFile)) {
533+
// perform the copy
542534
try {
543535
Base.copyFile(sourceFile, destFile);
544536

545537
} catch (IOException e) {
546538
Base.showWarning(tr("Error adding file"),
547539
I18n.format(tr("Could not add ''{0}'' to the sketch."), filename),
548-
e);
540+
e);
549541
return false;
550542
}
551543
}
544+
else {
545+
// If the source and destination are equal, a code file is handled
546+
// - as a replacement, if there is a corresponding tab,
547+
// (eg. user wants to update the file after modifying it outside the editor)
548+
// - as an addition, otherwise.
549+
// (eg. the user copied the file to the sketch folder and wants to edit it)
550+
// For a data file, this is a no-op.
551+
if (editor.findTabIndex(destFile) >= 0)
552+
replacement = true;
553+
}
552554

555+
// open/refresh the tab
553556
if (!isData) {
554557
int tabIndex;
555558
if (replacement) {

0 commit comments

Comments
 (0)