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