1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Merge Sketch.renameFileTo() into SketchFile.renameTo()

Now that SketchFile keeps a reference to its Sketch,
`SketchFile.renameTo()` can call `Sketch.checkNewFilename()`, so there
is no need for the renaming itself to go through Sketch.

This changes the parameter for `SketchFile.renameTo()` from File to
String, to enforce that only the filename is changed, not the directory
name.
This commit is contained in:
Matthijs Kooijman 2015-12-29 16:51:49 +01:00 committed by Martino Facchin
parent 74e5228a0f
commit 85d48394a4
3 changed files with 22 additions and 26 deletions

View File

@ -186,7 +186,7 @@ public class SketchController {
} else {
// Non-primary file, rename just that file
try {
sketch.renameFileTo(current, newName);
current.renameTo(newName);
} catch (IOException e) {
// This does not pass on e, to prevent showing a backtrace for
// "normal" errors.
@ -348,7 +348,7 @@ public class SketchController {
// Do rename of all .pde files to new .ino extension
for (SketchFile file : oldFiles) {
File newName = FileUtils.replaceExtension(file.getFile(), Sketch.DEFAULT_SKETCH_EXTENSION);
file.renameTo(newName);
file.renameTo(newName.getName());
}
}
}

View File

@ -277,27 +277,9 @@ public class Sketch {
file.renamedTo(new File(newFolder, file.getFileName()));
// And finally, rename the primary file
if (!getPrimaryFile().renameTo(newPrimary))
throw new IOException(tr("Failed to rename primary sketch file"));
getPrimaryFile().renameTo(newPrimary.getName());
}
/**
* Rename the given file to get the given name.
*
* @param sketchfile
* The SketchFile to be renamed.
* @param newName
* The new name, including extension, excluding directory
* name.
* @throws IOException
* When a problem occurs, or is expected to occur. The error
* message should be already translated.
*/
public void renameFileTo(SketchFile sketchfile, String newName) throws IOException {
File newFile = new File(folder, newName);
checkNewFilename(newFile);
sketchfile.renameTo(newFile);
}
public SketchFile addFile(String newName) throws IOException {
// Check the name will not cause any conflicts

View File

@ -160,11 +160,25 @@ public class SketchFile {
return true;
}
protected boolean renameTo(File what) {
boolean success = file.renameTo(what);
if (success)
renamedTo(what);
return success;
/**
* Rename the given file to get the given name.
*
* @param newName
* The new name, including extension, excluding directory
* name.
* @throws IOException
* When a problem occurs, or is expected to occur. The error
* message should be already translated.
*/
public void renameTo(String newName) throws IOException {
File newFile = new File(file.getParentFile(), newName);
sketch.checkNewFilename(newFile);
if (file.renameTo(newFile)) {
renamedTo(newFile);
} else {
String msg = I18n.format(tr("Failed to rename \"{0}\" to \"{1}\""), file.getName(), newName);
throw new IOException(msg);
}
}
/**