Skip to content

Commit 0764eb7

Browse files
matthijskooijmanfacchinm
authored andcommitted
Remove current and currentIndex variables from Sketch
Instead of letting Sketch (also) keep track of the currently selected tab, this moves the responsibility to Editor instead. When Sketch need to know the current tab and file, it now asks Editor. Switching between tabs is still handled through Sketch methods, but that will be cleaned up later.
1 parent 6b31cff commit 0764eb7

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

app/src/processing/app/Sketch.java

+34-34
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@
5656
*/
5757
public class Sketch {
5858
private final Editor editor;
59-
60-
private SketchCodeDocument current;
61-
private int currentIndex;
62-
6359
private final SketchData data;
6460

6561
/**
@@ -92,7 +88,6 @@ private void load() throws IOException {
9288
}
9389

9490
protected void load(boolean forceUpdate) throws IOException {
95-
current = null;
9691
data.load();
9792

9893
for (SketchCode code : data.getCodes()) {
@@ -102,8 +97,11 @@ protected void load(boolean forceUpdate) throws IOException {
10297

10398
// set the main file to be the current tab
10499
if (editor != null) {
100+
int current = editor.getCurrentTabIndex();
101+
if (current < 0)
102+
current = 0;
105103
editor.sketchLoaded(this);
106-
setCurrentCode(currentIndex, forceUpdate);
104+
setCurrentCode(current, forceUpdate);
107105
}
108106
}
109107

@@ -137,6 +135,9 @@ public void handleNewCode() {
137135
* Handler for the Rename Code menu option.
138136
*/
139137
public void handleRenameCode() {
138+
SketchCode current = editor.getCurrentTab().getSketchCode();
139+
int currentIndex = editor.getCurrentTabIndex();
140+
140141
editor.status.clearState();
141142
// make sure the user didn't hide the sketch folder
142143
ensureExistence();
@@ -163,8 +164,8 @@ public void handleRenameCode() {
163164
renamingCode = true;
164165
String prompt = (currentIndex == 0) ?
165166
"New name for sketch:" : "New name for file:";
166-
String oldName = (current.getCode().isExtension("ino")) ?
167-
current.getCode().getPrettyName() : current.getCode().getFileName();
167+
String oldName = (current.isExtension("ino")) ? current.getPrettyName()
168+
: current.getFileName();
168169
editor.status.edit(prompt, oldName);
169170
}
170171

@@ -177,6 +178,9 @@ public void handleRenameCode() {
177178
* where they diverge.
178179
*/
179180
protected void nameCode(String newName) {
181+
SketchCode current = editor.getCurrentTab().getSketchCode();
182+
int currentIndex = editor.getCurrentTabIndex();
183+
180184
// make sure the user didn't hide the sketch folder
181185
ensureExistence();
182186

@@ -191,7 +195,8 @@ protected void nameCode(String newName) {
191195
// (osx is case insensitive but preserving, windows insensitive,
192196
// *nix is sensitive and preserving.. argh)
193197
if (renamingCode) {
194-
if (newName.equalsIgnoreCase(current.getCode().getFileName()) && OSUtils.isWindows()) {
198+
if (newName.equalsIgnoreCase(current.getFileName())
199+
&& OSUtils.isWindows()) {
195200
// exit quietly for the 'rename' case.
196201
// if it's a 'new' then an error will occur down below
197202
return;
@@ -220,7 +225,7 @@ protected void nameCode(String newName) {
220225
// Don't let the user create the main tab as a .java file instead of .pde
221226
if (!isDefaultExtension(newExtension)) {
222227
if (renamingCode) { // If creating a new tab, don't show this error
223-
if (current.getCode() == data.getCode(0)) { // If this is the main tab, disallow
228+
if (current == data.getCode(0)) { // If this is the main tab, disallow
224229
Base.showWarning(tr("Problem with rename"),
225230
tr("The main file can't use an extension.\n" +
226231
"(It may be time for your to graduate to a\n" +
@@ -315,21 +320,21 @@ protected void nameCode(String newName) {
315320
// however this *will* first save the sketch, then rename
316321

317322
// first get the contents of the editor text area
318-
if (current.getCode().isModified()) {
323+
if (current.isModified()) {
319324
try {
320325
// save this new SketchCode
321-
current.getCode().save();
326+
current.save();
322327
} catch (Exception e) {
323328
Base.showWarning(tr("Error"), tr("Could not rename the sketch. (0)"), e);
324329
return;
325330
}
326331
}
327332

328-
if (!current.getCode().renameTo(newFile)) {
333+
if (!current.renameTo(newFile)) {
329334
Base.showWarning(tr("Error"),
330335
I18n.format(
331336
tr("Could not rename \"{0}\" to \"{1}\""),
332-
current.getCode().getFileName(),
337+
current.getFileName(),
333338
newFile.getName()
334339
), null);
335340
return;
@@ -369,11 +374,11 @@ protected void nameCode(String newName) {
369374
editor.base.rebuildSketchbookMenus();
370375

371376
} else { // else if something besides code[0]
372-
if (!current.getCode().renameTo(newFile)) {
377+
if (!current.renameTo(newFile)) {
373378
Base.showWarning(tr("Error"),
374379
I18n.format(
375380
tr("Could not rename \"{0}\" to \"{1}\""),
376-
current.getCode().getFileName(),
381+
current.getFileName(),
377382
newFile.getName()
378383
), null);
379384
return;
@@ -424,6 +429,8 @@ protected void nameCode(String newName) {
424429
* Remove a piece of code from the sketch and from the disk.
425430
*/
426431
public void handleDeleteCode() throws IOException {
432+
SketchCode current = editor.getCurrentTab().getSketchCode();
433+
int currentIndex = editor.getCurrentTabIndex();
427434
editor.status.clearState();
428435
// make sure the user didn't hide the sketch folder
429436
ensureExistence();
@@ -442,7 +449,8 @@ public void handleDeleteCode() throws IOException {
442449
Object[] options = { tr("OK"), tr("Cancel") };
443450
String prompt = (currentIndex == 0) ?
444451
tr("Are you sure you want to delete this sketch?") :
445-
I18n.format(tr("Are you sure you want to delete \"{0}\"?"), current.getCode().getFileNameWithExtensionIfNotIno());
452+
I18n.format(tr("Are you sure you want to delete \"{0}\"?"),
453+
current.getFileNameWithExtensionIfNotIno());
446454
int result = JOptionPane.showOptionDialog(editor,
447455
prompt,
448456
tr("Delete"),
@@ -469,14 +477,14 @@ public void handleDeleteCode() throws IOException {
469477

470478
} else {
471479
// delete the file
472-
if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath())) {
480+
if (!current.deleteFile(BaseNoGui.getBuildFolder(data).toPath())) {
473481
Base.showMessage(tr("Couldn't do it"),
474-
I18n.format(tr("Could not delete \"{0}\"."), current.getCode().getFileName()));
482+
I18n.format(tr("Could not delete \"{0}\"."), current.getFileName()));
475483
return;
476484
}
477485

478486
// remove code from the list
479-
data.removeCode(current.getCode());
487+
data.removeCode(current);
480488

481489
// just set current tab to the main tab
482490
setCurrentCode(0);
@@ -492,7 +500,7 @@ public void handleDeleteCode() throws IOException {
492500
* Move to the previous tab.
493501
*/
494502
public void handlePrevCode() {
495-
int prev = currentIndex - 1;
503+
int prev = editor.getCurrentTabIndex() - 1;
496504
if (prev < 0) prev = data.getCodeCount()-1;
497505
setCurrentCode(prev);
498506
}
@@ -502,7 +510,7 @@ public void handlePrevCode() {
502510
* Move to the next tab.
503511
*/
504512
public void handleNextCode() {
505-
setCurrentCode((currentIndex + 1) % data.getCodeCount());
513+
setCurrentCode((editor.getCurrentTabIndex() + 1) % data.getCodeCount());
506514
}
507515

508516
/**
@@ -715,7 +723,7 @@ protected boolean saveAs() throws IOException {
715723
data.getCode(0).saveAs(newFile);
716724

717725
editor.handleOpenUnchecked(newFile,
718-
currentIndex,
726+
editor.getCurrentTabIndex(),
719727
editor.getCurrentTab().getSelectionStart(),
720728
editor.getCurrentTab().getSelectionStop(),
721729
editor.getCurrentTab().getScrollPosition());
@@ -907,7 +915,7 @@ public void importLibrary(UserLibrary lib) throws IOException {
907915
// import statements into the main sketch file (code[0])
908916
// if the current code is a .java file, insert into current
909917
//if (current.flavor == PDE) {
910-
if (hasDefaultExtension(current.getCode())) {
918+
if (hasDefaultExtension(editor.getCurrentTab().getSketchCode())) {
911919
setCurrentCode(0);
912920
}
913921
// could also scan the text in the file to see if each import
@@ -939,14 +947,11 @@ public void setCurrentCode(int which) {
939947
}
940948

941949
private void setCurrentCode(int which, boolean forceUpdate) {
942-
// if current is null, then this is the first setCurrent(0)
943-
if (!forceUpdate && (currentIndex == which) && (current != null)) {
950+
if (!forceUpdate && (editor.getCurrentTabIndex() == which)) {
944951
return;
945952
}
946953

947-
current = (SketchCodeDocument) data.getCode(which).getMetadata();
948-
currentIndex = which;
949-
editor.setCode(current);
954+
editor.setCode((SketchCodeDocument)editor.getTabs().get(which).getSketchCode().getMetadata());
950955
editor.header.rebuild();
951956
}
952957

@@ -1337,11 +1342,6 @@ public int getCodeIndex(SketchCode who) {
13371342
}
13381343

13391344

1340-
public SketchCode getCurrentCode() {
1341-
return current.getCode();
1342-
}
1343-
1344-
13451345
private void setUntitled(boolean u) {
13461346
editor.untitled = u;
13471347
}

0 commit comments

Comments
 (0)