Skip to content

Commit 6715f41

Browse files
matthijskooijmanfacchinm
authored andcommitted
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.
1 parent ab14c63 commit 6715f41

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

app/src/processing/app/Sketch.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,12 @@ public void handleNewCode() {
131131
*/
132132
public void handleRenameCode() {
133133
SketchCode current = editor.getCurrentTab().getSketchCode();
134-
int currentIndex = editor.getCurrentTabIndex();
135134

136135
editor.status.clearState();
137136
// make sure the user didn't hide the sketch folder
138137
ensureExistence();
139138

140-
if (currentIndex == 0 && editor.untitled) {
139+
if (current.isPrimary() && editor.untitled) {
141140
Base.showMessage(tr("Sketch is Untitled"),
142141
tr("How about saving the sketch first \n" +
143142
"before trying to rename it?"));
@@ -157,7 +156,7 @@ public void handleRenameCode() {
157156
// ask for new name of file (internal to window)
158157
// TODO maybe just popup a text area?
159158
renamingCode = true;
160-
String prompt = (currentIndex == 0) ?
159+
String prompt = current.isPrimary() ?
161160
"New name for sketch:" : "New name for file:";
162161
String oldName = (current.isExtension("ino")) ? current.getPrettyName()
163162
: current.getFileName();
@@ -263,7 +262,7 @@ protected void nameCode(String newName) {
263262
return;
264263
}
265264

266-
if (renamingCode && currentIndex == 0) {
265+
if (renamingCode && current.isPrimary()) {
267266
for (SketchCode code : data.getCodes()) {
268267
if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) &&
269268
code.isExtension("cpp")) {
@@ -296,7 +295,7 @@ protected void nameCode(String newName) {
296295
// }
297296

298297
if (renamingCode) {
299-
if (currentIndex == 0) {
298+
if (current.isPrimary()) {
300299
// get the new folder name/location
301300
String folderName = newName.substring(0, newName.indexOf('.'));
302301
File newFolder = new File(data.getFolder().getParentFile(), folderName);
@@ -396,7 +395,7 @@ protected void nameCode(String newName) {
396395
return;
397396
}
398397
ensureExistence();
399-
SketchCode code = new SketchCode(newFile);
398+
SketchCode code = new SketchCode(newFile, false);
400399
try {
401400
editor.addTab(code, "");
402401
} catch (IOException e) {
@@ -425,7 +424,6 @@ protected void nameCode(String newName) {
425424
*/
426425
public void handleDeleteCode() throws IOException {
427426
SketchCode current = editor.getCurrentTab().getSketchCode();
428-
int currentIndex = editor.getCurrentTabIndex();
429427
editor.status.clearState();
430428
// make sure the user didn't hide the sketch folder
431429
ensureExistence();
@@ -442,7 +440,7 @@ public void handleDeleteCode() throws IOException {
442440

443441
// confirm deletion with user, yes/no
444442
Object[] options = { tr("OK"), tr("Cancel") };
445-
String prompt = (currentIndex == 0) ?
443+
String prompt = current.isPrimary() ?
446444
tr("Are you sure you want to delete this sketch?") :
447445
I18n.format(tr("Are you sure you want to delete \"{0}\"?"),
448446
current.getFileNameWithExtensionIfNotIno());
@@ -455,7 +453,7 @@ public void handleDeleteCode() throws IOException {
455453
options,
456454
options[0]);
457455
if (result == JOptionPane.YES_OPTION) {
458-
if (currentIndex == 0) {
456+
if (current.isPrimary()) {
459457
// need to unset all the modified flags, otherwise tries
460458
// to do a save on the handleNew()
461459

@@ -856,7 +854,7 @@ public boolean addFile(File sourceFile) {
856854
}
857855

858856
if (codeExtension != null) {
859-
SketchCode newCode = new SketchCode(destFile);
857+
SketchCode newCode = new SketchCode(destFile, false);
860858

861859
if (replacement) {
862860
data.replaceCode(newCode);

arduino-core/src/processing/app/SketchCode.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class SketchCode {
4545
*/
4646
private File file;
4747

48+
/**
49+
* Is this the primary file in the sketch?
50+
*/
51+
private boolean primary;
52+
4853
/**
4954
* Interface for an in-memory storage of text file contents. This is
5055
* intended to allow a GUI to keep modified text in memory, and allow
@@ -71,8 +76,17 @@ public static interface TextStorage {
7176
*/
7277
private TextStorage storage;
7378

74-
public SketchCode(File file) {
79+
/**
80+
* Create a new SketchCode
81+
*
82+
* @param file
83+
* The file this SketchCode represents
84+
* @param primary
85+
* Whether this file is the primary file of the sketch
86+
*/
87+
public SketchCode(File file, boolean primary) {
7588
this.file = file;
89+
this.primary = primary;
7690
}
7791

7892
/**
@@ -88,6 +102,12 @@ public File getFile() {
88102
return file;
89103
}
90104

105+
/**
106+
* Is this the primary file in the sketch?
107+
*/
108+
public boolean isPrimary() {
109+
return primary;
110+
}
91111

92112
protected boolean fileExists() {
93113
return file.exists();

arduino-core/src/processing/app/SketchData.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ protected void load() throws IOException {
134134
// Don't allow people to use files with invalid names, since on load,
135135
// it would be otherwise possible to sneak in nasty filenames. [0116]
136136
if (BaseNoGui.isSanitaryName(base)) {
137-
addCode(new SketchCode(new File(folder, filename)));
137+
File file = new File(folder, filename);
138+
addCode(new SketchCode(file, file.equals(primaryFile)));
138139
} else {
139140
System.err.println(I18n.format(tr("File name {0} is invalid: ignored"), filename));
140141
}

0 commit comments

Comments
 (0)