56
56
*/
57
57
public class Sketch {
58
58
private final Editor editor ;
59
-
60
- private SketchCodeDocument current ;
61
- private int currentIndex ;
62
-
63
59
private final SketchData data ;
64
60
65
61
/**
@@ -92,7 +88,6 @@ private void load() throws IOException {
92
88
}
93
89
94
90
protected void load (boolean forceUpdate ) throws IOException {
95
- current = null ;
96
91
data .load ();
97
92
98
93
for (SketchCode code : data .getCodes ()) {
@@ -102,8 +97,11 @@ protected void load(boolean forceUpdate) throws IOException {
102
97
103
98
// set the main file to be the current tab
104
99
if (editor != null ) {
100
+ int current = editor .getCurrentTabIndex ();
101
+ if (current < 0 )
102
+ current = 0 ;
105
103
editor .sketchLoaded (this );
106
- setCurrentCode (currentIndex , forceUpdate );
104
+ setCurrentCode (current , forceUpdate );
107
105
}
108
106
}
109
107
@@ -137,6 +135,9 @@ public void handleNewCode() {
137
135
* Handler for the Rename Code menu option.
138
136
*/
139
137
public void handleRenameCode () {
138
+ SketchCode current = editor .getCurrentTab ().getSketchCode ();
139
+ int currentIndex = editor .getCurrentTabIndex ();
140
+
140
141
editor .status .clearState ();
141
142
// make sure the user didn't hide the sketch folder
142
143
ensureExistence ();
@@ -163,8 +164,8 @@ public void handleRenameCode() {
163
164
renamingCode = true ;
164
165
String prompt = (currentIndex == 0 ) ?
165
166
"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 ();
168
169
editor .status .edit (prompt , oldName );
169
170
}
170
171
@@ -177,6 +178,9 @@ public void handleRenameCode() {
177
178
* where they diverge.
178
179
*/
179
180
protected void nameCode (String newName ) {
181
+ SketchCode current = editor .getCurrentTab ().getSketchCode ();
182
+ int currentIndex = editor .getCurrentTabIndex ();
183
+
180
184
// make sure the user didn't hide the sketch folder
181
185
ensureExistence ();
182
186
@@ -191,7 +195,8 @@ protected void nameCode(String newName) {
191
195
// (osx is case insensitive but preserving, windows insensitive,
192
196
// *nix is sensitive and preserving.. argh)
193
197
if (renamingCode ) {
194
- if (newName .equalsIgnoreCase (current .getCode ().getFileName ()) && OSUtils .isWindows ()) {
198
+ if (newName .equalsIgnoreCase (current .getFileName ())
199
+ && OSUtils .isWindows ()) {
195
200
// exit quietly for the 'rename' case.
196
201
// if it's a 'new' then an error will occur down below
197
202
return ;
@@ -220,7 +225,7 @@ protected void nameCode(String newName) {
220
225
// Don't let the user create the main tab as a .java file instead of .pde
221
226
if (!isDefaultExtension (newExtension )) {
222
227
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
224
229
Base .showWarning (tr ("Problem with rename" ),
225
230
tr ("The main file can't use an extension.\n " +
226
231
"(It may be time for your to graduate to a\n " +
@@ -315,21 +320,21 @@ protected void nameCode(String newName) {
315
320
// however this *will* first save the sketch, then rename
316
321
317
322
// first get the contents of the editor text area
318
- if (current .getCode (). isModified ()) {
323
+ if (current .isModified ()) {
319
324
try {
320
325
// save this new SketchCode
321
- current .getCode (). save ();
326
+ current .save ();
322
327
} catch (Exception e ) {
323
328
Base .showWarning (tr ("Error" ), tr ("Could not rename the sketch. (0)" ), e );
324
329
return ;
325
330
}
326
331
}
327
332
328
- if (!current .getCode (). renameTo (newFile )) {
333
+ if (!current .renameTo (newFile )) {
329
334
Base .showWarning (tr ("Error" ),
330
335
I18n .format (
331
336
tr ("Could not rename \" {0}\" to \" {1}\" " ),
332
- current .getCode (). getFileName (),
337
+ current .getFileName (),
333
338
newFile .getName ()
334
339
), null );
335
340
return ;
@@ -369,11 +374,11 @@ protected void nameCode(String newName) {
369
374
editor .base .rebuildSketchbookMenus ();
370
375
371
376
} else { // else if something besides code[0]
372
- if (!current .getCode (). renameTo (newFile )) {
377
+ if (!current .renameTo (newFile )) {
373
378
Base .showWarning (tr ("Error" ),
374
379
I18n .format (
375
380
tr ("Could not rename \" {0}\" to \" {1}\" " ),
376
- current . getCode () .getFileName (),
381
+ current .getFileName (),
377
382
newFile .getName ()
378
383
), null );
379
384
return ;
@@ -424,6 +429,8 @@ protected void nameCode(String newName) {
424
429
* Remove a piece of code from the sketch and from the disk.
425
430
*/
426
431
public void handleDeleteCode () throws IOException {
432
+ SketchCode current = editor .getCurrentTab ().getSketchCode ();
433
+ int currentIndex = editor .getCurrentTabIndex ();
427
434
editor .status .clearState ();
428
435
// make sure the user didn't hide the sketch folder
429
436
ensureExistence ();
@@ -442,7 +449,8 @@ public void handleDeleteCode() throws IOException {
442
449
Object [] options = { tr ("OK" ), tr ("Cancel" ) };
443
450
String prompt = (currentIndex == 0 ) ?
444
451
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 ());
446
454
int result = JOptionPane .showOptionDialog (editor ,
447
455
prompt ,
448
456
tr ("Delete" ),
@@ -469,14 +477,14 @@ public void handleDeleteCode() throws IOException {
469
477
470
478
} else {
471
479
// delete the file
472
- if (!current .getCode (). deleteFile (BaseNoGui .getBuildFolder (data ).toPath ())) {
480
+ if (!current .deleteFile (BaseNoGui .getBuildFolder (data ).toPath ())) {
473
481
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 ()));
475
483
return ;
476
484
}
477
485
478
486
// remove code from the list
479
- data .removeCode (current . getCode () );
487
+ data .removeCode (current );
480
488
481
489
// just set current tab to the main tab
482
490
setCurrentCode (0 );
@@ -492,7 +500,7 @@ public void handleDeleteCode() throws IOException {
492
500
* Move to the previous tab.
493
501
*/
494
502
public void handlePrevCode () {
495
- int prev = currentIndex - 1 ;
503
+ int prev = editor . getCurrentTabIndex () - 1 ;
496
504
if (prev < 0 ) prev = data .getCodeCount ()-1 ;
497
505
setCurrentCode (prev );
498
506
}
@@ -502,7 +510,7 @@ public void handlePrevCode() {
502
510
* Move to the next tab.
503
511
*/
504
512
public void handleNextCode () {
505
- setCurrentCode ((currentIndex + 1 ) % data .getCodeCount ());
513
+ setCurrentCode ((editor . getCurrentTabIndex () + 1 ) % data .getCodeCount ());
506
514
}
507
515
508
516
/**
@@ -715,7 +723,7 @@ protected boolean saveAs() throws IOException {
715
723
data .getCode (0 ).saveAs (newFile );
716
724
717
725
editor .handleOpenUnchecked (newFile ,
718
- currentIndex ,
726
+ editor . getCurrentTabIndex () ,
719
727
editor .getCurrentTab ().getSelectionStart (),
720
728
editor .getCurrentTab ().getSelectionStop (),
721
729
editor .getCurrentTab ().getScrollPosition ());
@@ -907,7 +915,7 @@ public void importLibrary(UserLibrary lib) throws IOException {
907
915
// import statements into the main sketch file (code[0])
908
916
// if the current code is a .java file, insert into current
909
917
//if (current.flavor == PDE) {
910
- if (hasDefaultExtension (current . getCode ())) {
918
+ if (hasDefaultExtension (editor . getCurrentTab (). getSketchCode ())) {
911
919
setCurrentCode (0 );
912
920
}
913
921
// could also scan the text in the file to see if each import
@@ -939,14 +947,11 @@ public void setCurrentCode(int which) {
939
947
}
940
948
941
949
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 )) {
944
951
return ;
945
952
}
946
953
947
- current = (SketchCodeDocument ) data .getCode (which ).getMetadata ();
948
- currentIndex = which ;
949
- editor .setCode (current );
954
+ editor .setCode ((SketchCodeDocument )editor .getTabs ().get (which ).getSketchCode ().getMetadata ());
950
955
editor .header .rebuild ();
951
956
}
952
957
@@ -1337,11 +1342,6 @@ public int getCodeIndex(SketchCode who) {
1337
1342
}
1338
1343
1339
1344
1340
- public SketchCode getCurrentCode () {
1341
- return current .getCode ();
1342
- }
1343
-
1344
-
1345
1345
private void setUntitled (boolean u ) {
1346
1346
editor .untitled = u ;
1347
1347
}
0 commit comments