@@ -495,61 +495,64 @@ public boolean addFile(File sourceFile) {
495
495
isData = true ;
496
496
}
497
497
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
516
501
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
+ }
527
519
}
528
- }
529
520
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
+ }
538
532
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
542
534
try {
543
535
Base .copyFile (sourceFile , destFile );
544
536
545
537
} catch (IOException e ) {
546
538
Base .showWarning (tr ("Error adding file" ),
547
539
I18n .format (tr ("Could not add ''{0}'' to the sketch." ), filename ),
548
- e );
540
+ e );
549
541
return false ;
550
542
}
551
543
}
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
+ }
552
554
555
+ // open/refresh the tab
553
556
if (!isData ) {
554
557
int tabIndex ;
555
558
if (replacement ) {
0 commit comments