Skip to content

Commit f9824f4

Browse files
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 4fa8dbf commit f9824f4

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

Diff for: app/src/processing/app/Sketch.java

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

6662
/**
@@ -93,7 +89,6 @@ private void load() throws IOException {
9389
}
9490

9591
protected void load(boolean forceUpdate) throws IOException {
96-
current = null;
9792
data.load();
9893

9994
for (SketchCode code : data.getCodes()) {
@@ -103,8 +98,11 @@ protected void load(boolean forceUpdate) throws IOException {
10398

10499
// set the main file to be the current tab
105100
if (editor != null) {
101+
int current = editor.getCurrentTabIndex();
102+
if (current < 0)
103+
current = 0;
106104
editor.sketchLoaded(this);
107-
setCurrentCode(currentIndex, forceUpdate);
105+
setCurrentCode(current, forceUpdate);
108106
}
109107
}
110108

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

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

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

318323
// first get the contents of the editor text area
319-
if (current.getCode().isModified()) {
324+
if (current.isModified()) {
320325
try {
321326
// save this new SketchCode
322-
current.getCode().save();
327+
current.save();
323328
} catch (Exception e) {
324329
Base.showWarning(tr("Error"), tr("Could not rename the sketch. (0)"), e);
325330
return;
326331
}
327332
}
328333

329-
if (!current.getCode().renameTo(newFile)) {
334+
if (!current.renameTo(newFile)) {
330335
Base.showWarning(tr("Error"),
331336
I18n.format(
332337
tr("Could not rename \"{0}\" to \"{1}\""),
333-
current.getCode().getFileName(),
338+
current.getFileName(),
334339
newFile.getName()
335340
), null);
336341
return;
@@ -370,11 +375,11 @@ protected void nameCode(String newName) {
370375
editor.base.rebuildSketchbookMenus();
371376

372377
} else { // else if something besides code[0]
373-
if (!current.getCode().renameTo(newFile)) {
378+
if (!current.renameTo(newFile)) {
374379
Base.showWarning(tr("Error"),
375380
I18n.format(
376381
tr("Could not rename \"{0}\" to \"{1}\""),
377-
current.getCode().getFileName(),
382+
current.getFileName(),
378383
newFile.getName()
379384
), null);
380385
return;
@@ -425,6 +430,8 @@ protected void nameCode(String newName) {
425430
* Remove a piece of code from the sketch and from the disk.
426431
*/
427432
public void handleDeleteCode() throws IOException {
433+
SketchCode current = editor.getCurrentTab().getSketchCode();
434+
int currentIndex = editor.getCurrentTabIndex();
428435
editor.status.clearState();
429436
// make sure the user didn't hide the sketch folder
430437
ensureExistence();
@@ -443,7 +450,8 @@ public void handleDeleteCode() throws IOException {
443450
Object[] options = { tr("OK"), tr("Cancel") };
444451
String prompt = (currentIndex == 0) ?
445452
tr("Are you sure you want to delete this sketch?") :
446-
I18n.format(tr("Are you sure you want to delete \"{0}\"?"), current.getCode().getFileNameWithExtensionIfNotIno());
453+
I18n.format(tr("Are you sure you want to delete \"{0}\"?"),
454+
current.getFileNameWithExtensionIfNotIno());
447455
int result = JOptionPane.showOptionDialog(editor,
448456
prompt,
449457
tr("Delete"),
@@ -470,14 +478,14 @@ public void handleDeleteCode() throws IOException {
470478

471479
} else {
472480
// delete the file
473-
if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath())) {
481+
if (!current.deleteFile(BaseNoGui.getBuildFolder(data).toPath())) {
474482
Base.showMessage(tr("Couldn't do it"),
475-
I18n.format(tr("Could not delete \"{0}\"."), current.getCode().getFileName()));
483+
I18n.format(tr("Could not delete \"{0}\"."), current.getFileName()));
476484
return;
477485
}
478486

479487
// remove code from the list
480-
data.removeCode(current.getCode());
488+
data.removeCode(current);
481489

482490
// just set current tab to the main tab
483491
setCurrentCode(0);
@@ -493,7 +501,7 @@ public void handleDeleteCode() throws IOException {
493501
* Move to the previous tab.
494502
*/
495503
public void handlePrevCode() {
496-
int prev = currentIndex - 1;
504+
int prev = editor.getCurrentTabIndex() - 1;
497505
if (prev < 0) prev = data.getCodeCount()-1;
498506
setCurrentCode(prev);
499507
}
@@ -503,7 +511,7 @@ public void handlePrevCode() {
503511
* Move to the next tab.
504512
*/
505513
public void handleNextCode() {
506-
setCurrentCode((currentIndex + 1) % data.getCodeCount());
514+
setCurrentCode((editor.getCurrentTabIndex() + 1) % data.getCodeCount());
507515
}
508516

509517
/**
@@ -716,7 +724,7 @@ protected boolean saveAs() throws IOException {
716724
data.getCode(0).saveAs(newFile);
717725

718726
editor.handleOpenUnchecked(newFile,
719-
currentIndex,
727+
editor.getCurrentTabIndex(),
720728
editor.getCurrentTab().getSelectionStart(),
721729
editor.getCurrentTab().getSelectionStop(),
722730
editor.getCurrentTab().getScrollPosition());
@@ -908,7 +916,7 @@ private void importLibrary(File jarPath) throws IOException {
908916
// import statements into the main sketch file (code[0])
909917
// if the current code is a .java file, insert into current
910918
//if (current.flavor == PDE) {
911-
if (hasDefaultExtension(current.getCode())) {
919+
if (hasDefaultExtension(editor.getCurrentTab().getSketchCode())) {
912920
setCurrentCode(0);
913921
}
914922
// could also scan the text in the file to see if each import
@@ -940,14 +948,11 @@ public void setCurrentCode(int which) {
940948
}
941949

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

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

@@ -1329,11 +1334,6 @@ public int getCodeIndex(SketchCode who) {
13291334
}
13301335

13311336

1332-
public SketchCode getCurrentCode() {
1333-
return current.getCode();
1334-
}
1335-
1336-
13371337
private void setUntitled(boolean u) {
13381338
editor.untitled = u;
13391339
}

0 commit comments

Comments
 (0)