Let SketchCode track if it is the primary file

This makes checking for the primary file easier, without having to know
the index of a file in the list of tabs, or relying on the fact that the
primary file is always first (it still is, though).

This changes some places in Sketch to use the new
`SketchCode.isPrimary()` method, but there probably are a lot more
places in the code that could be start to use it as well.
This commit is contained in:
Matthijs Kooijman 2015-12-08 18:07:05 +01:00 committed by Martino Facchin
parent ab14c63f58
commit 6715f41c0e
3 changed files with 31 additions and 12 deletions

View File

@ -131,13 +131,12 @@ public class Sketch {
*/
public void handleRenameCode() {
SketchCode current = editor.getCurrentTab().getSketchCode();
int currentIndex = editor.getCurrentTabIndex();
editor.status.clearState();
// make sure the user didn't hide the sketch folder
ensureExistence();
if (currentIndex == 0 && editor.untitled) {
if (current.isPrimary() && editor.untitled) {
Base.showMessage(tr("Sketch is Untitled"),
tr("How about saving the sketch first \n" +
"before trying to rename it?"));
@ -157,7 +156,7 @@ public class Sketch {
// ask for new name of file (internal to window)
// TODO maybe just popup a text area?
renamingCode = true;
String prompt = (currentIndex == 0) ?
String prompt = current.isPrimary() ?
"New name for sketch:" : "New name for file:";
String oldName = (current.isExtension("ino")) ? current.getPrettyName()
: current.getFileName();
@ -263,7 +262,7 @@ public class Sketch {
return;
}
if (renamingCode && currentIndex == 0) {
if (renamingCode && current.isPrimary()) {
for (SketchCode code : data.getCodes()) {
if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) &&
code.isExtension("cpp")) {
@ -296,7 +295,7 @@ public class Sketch {
// }
if (renamingCode) {
if (currentIndex == 0) {
if (current.isPrimary()) {
// get the new folder name/location
String folderName = newName.substring(0, newName.indexOf('.'));
File newFolder = new File(data.getFolder().getParentFile(), folderName);
@ -396,7 +395,7 @@ public class Sketch {
return;
}
ensureExistence();
SketchCode code = new SketchCode(newFile);
SketchCode code = new SketchCode(newFile, false);
try {
editor.addTab(code, "");
} catch (IOException e) {
@ -425,7 +424,6 @@ public class Sketch {
*/
public void handleDeleteCode() throws IOException {
SketchCode current = editor.getCurrentTab().getSketchCode();
int currentIndex = editor.getCurrentTabIndex();
editor.status.clearState();
// make sure the user didn't hide the sketch folder
ensureExistence();
@ -442,7 +440,7 @@ public class Sketch {
// confirm deletion with user, yes/no
Object[] options = { tr("OK"), tr("Cancel") };
String prompt = (currentIndex == 0) ?
String prompt = current.isPrimary() ?
tr("Are you sure you want to delete this sketch?") :
I18n.format(tr("Are you sure you want to delete \"{0}\"?"),
current.getFileNameWithExtensionIfNotIno());
@ -455,7 +453,7 @@ public class Sketch {
options,
options[0]);
if (result == JOptionPane.YES_OPTION) {
if (currentIndex == 0) {
if (current.isPrimary()) {
// need to unset all the modified flags, otherwise tries
// to do a save on the handleNew()
@ -856,7 +854,7 @@ public class Sketch {
}
if (codeExtension != null) {
SketchCode newCode = new SketchCode(destFile);
SketchCode newCode = new SketchCode(destFile, false);
if (replacement) {
data.replaceCode(newCode);

View File

@ -45,6 +45,11 @@ public class SketchCode {
*/
private File file;
/**
* Is this the primary file in the sketch?
*/
private boolean primary;
/**
* Interface for an in-memory storage of text file contents. This is
* intended to allow a GUI to keep modified text in memory, and allow
@ -71,8 +76,17 @@ public class SketchCode {
*/
private TextStorage storage;
public SketchCode(File file) {
/**
* Create a new SketchCode
*
* @param file
* The file this SketchCode represents
* @param primary
* Whether this file is the primary file of the sketch
*/
public SketchCode(File file, boolean primary) {
this.file = file;
this.primary = primary;
}
/**
@ -88,6 +102,12 @@ public class SketchCode {
return file;
}
/**
* Is this the primary file in the sketch?
*/
public boolean isPrimary() {
return primary;
}
protected boolean fileExists() {
return file.exists();

View File

@ -134,7 +134,8 @@ public class SketchData {
// Don't allow people to use files with invalid names, since on load,
// it would be otherwise possible to sneak in nasty filenames. [0116]
if (BaseNoGui.isSanitaryName(base)) {
addCode(new SketchCode(new File(folder, filename)));
File file = new File(folder, filename);
addCode(new SketchCode(file, file.equals(primaryFile)));
} else {
System.err.println(I18n.format(tr("File name {0} is invalid: ignored"), filename));
}