@@ -532,8 +532,11 @@ protected boolean restoreSketches() throws Exception {
532
532
// Save the sketch path and window placement for each open sketch
533
533
int count = PreferencesData .getInteger ("last.sketch.count" );
534
534
int opened = 0 ;
535
- for (int i = 0 ; i < count ; i ++ ) {
535
+ for (int i = count - 1 ; i >= 0 ; i -- ) {
536
536
String path = PreferencesData .get ("last.sketch" + i + ".path" );
537
+ if (path == null ) {
538
+ continue ;
539
+ }
537
540
if (BaseNoGui .getPortableFolder () != null ) {
538
541
File absolute = new File (BaseNoGui .getPortableFolder (), path );
539
542
try {
@@ -550,7 +553,7 @@ protected boolean restoreSketches() throws Exception {
550
553
location = nextEditorLocation ();
551
554
}
552
555
// If file did not exist, null will be returned for the Editor
553
- if (handleOpen (new File (path ), location , true ) != null ) {
556
+ if (handleOpen (new File (path ), location , true , false ) != null ) {
554
557
opened ++;
555
558
}
556
559
}
@@ -571,19 +574,21 @@ protected void storeSketches() {
571
574
String untitledPath = untitledFolder .getAbsolutePath ();
572
575
573
576
// Save the sketch path and window placement for each open sketch
577
+ LinkedList <Editor > reverseEditors = new LinkedList <Editor >(editors );
578
+ Collections .reverse (reverseEditors );
574
579
int index = 0 ;
575
- for (Editor editor : editors ) {
580
+ for (Editor editor : reverseEditors ) {
576
581
String path = editor .getSketch ().getMainFilePath ();
577
582
// In case of a crash, save untitled sketches if they contain changes.
578
583
// (Added this for release 0158, may not be a good idea.)
579
- if (path .startsWith (untitledPath ) &&
580
- !editor .getSketch ().isModified ()) {
584
+ if (path .startsWith (untitledPath ) && !editor .getSketch ().isModified ()) {
581
585
continue ;
582
586
}
583
587
if (BaseNoGui .getPortableFolder () != null ) {
584
588
path = FileUtils .relativePath (BaseNoGui .getPortableFolder ().toString (), path );
585
- if (path == null )
589
+ if (path == null ) {
586
590
continue ;
591
+ }
587
592
}
588
593
PreferencesData .set ("last.sketch" + index + ".path" , path );
589
594
@@ -595,45 +600,6 @@ protected void storeSketches() {
595
600
PreferencesData .setInteger ("last.sketch.count" , index );
596
601
}
597
602
598
-
599
- // If a sketch is untitled on quit, may need to store the new name
600
- // rather than the location from the temp folder.
601
- protected void storeSketchPath (Editor editor , int index ) {
602
- String path = editor .getSketch ().getMainFilePath ();
603
- String untitledPath = untitledFolder .getAbsolutePath ();
604
- if (path .startsWith (untitledPath )) {
605
- path = "" ;
606
- } else if (BaseNoGui .getPortableFolder () != null ) {
607
- path = FileUtils .relativePath (BaseNoGui .getPortableFolder ().toString (), path );
608
- if (path == null )
609
- path = "" ;
610
- }
611
- PreferencesData .set ("last.sketch" + index + ".path" , path );
612
- }
613
-
614
-
615
- /*
616
- public void storeSketch(Editor editor) {
617
- int index = -1;
618
- for (int i = 0; i < editorCount; i++) {
619
- if (editors[i] == editor) {
620
- index = i;
621
- break;
622
- }
623
- }
624
- if (index == -1) {
625
- System.err.println("Problem storing sketch " + editor.sketch.name);
626
- } else {
627
- String path = editor.sketch.getMainFilePath();
628
- Preferences.set("last.sketch" + index + ".path", path);
629
- }
630
- }
631
- */
632
-
633
-
634
- // .................................................................
635
-
636
-
637
603
// Because of variations in native windowing systems, no guarantees about
638
604
// changes to the focused and active Windows can be made. Developers must
639
605
// never assume that this Window is the focused or active Window until this
@@ -880,79 +846,48 @@ public Editor handleOpen(File file) throws Exception {
880
846
return handleOpen (file , nextEditorLocation (), true );
881
847
}
882
848
883
-
884
849
protected Editor handleOpen (File file , int [] location , boolean showEditor ) throws Exception {
885
- // System.err.println("entering handleOpen " + path);
850
+ return handleOpen (file , location , showEditor , true );
851
+ }
886
852
853
+ protected Editor handleOpen (File file , int [] location , boolean showEditor , boolean storeOpenedSketches ) throws Exception {
887
854
if (!file .exists ()) return null ;
888
855
889
- // System.err.println(" editors: " + editors);
890
856
// Cycle through open windows to make sure that it's not already open.
891
857
String path = file .getAbsolutePath ();
892
858
for (Editor editor : editors ) {
893
859
if (editor .getSketch ().getMainFilePath ().equals (path )) {
894
860
editor .toFront ();
895
- // System.err.println(" handleOpen: already opened");
896
861
return editor ;
897
862
}
898
863
}
899
864
900
- // If the active editor window is an untitled, and un-modified document,
901
- // just replace it with the file that's being opened.
902
- // if (activeEditor != null) {
903
- // Sketch activeSketch = activeEditor.sketch;
904
- // if (activeSketch.isUntitled() && !activeSketch.isModified()) {
905
- // // if it's an untitled, unmodified document, it can be replaced.
906
- // // except in cases where a second blank window is being opened.
907
- // if (!path.startsWith(untitledFolder.getAbsolutePath())) {
908
- // activeEditor.handleOpenUnchecked(path, 0, 0, 0, 0);
909
- // return activeEditor;
910
- // }
911
- // }
912
- // }
913
-
914
- // System.err.println(" creating new editor");
915
- Editor editor = new Editor (this , file , location , BaseNoGui .getPlatform ());
916
- // Editor editor = null;
917
- // try {
918
- // editor = new Editor(this, path, location);
919
- // } catch (Exception e) {
920
- // e.printStackTrace();
921
- // System.err.flush();
922
- // System.out.flush();
923
- // System.exit(1);
924
- // }
925
- // System.err.println(" done creating new editor");
926
- // EditorConsole.systemErr.println(" done creating new editor");
865
+ final Editor editor = new Editor (this , file , location , BaseNoGui .getPlatform ());
927
866
928
867
// Make sure that the sketch actually loaded
929
868
if (editor .getSketch () == null ) {
930
- // System.err.println("sketch was null, getting out of handleOpen");
931
869
return null ; // Just walk away quietly
932
870
}
933
871
934
- // if (editors == null) {
935
- // editors = new Editor[5];
936
- // }
937
- // if (editorCount == editors.length) {
938
- // editors = (Editor[]) PApplet.expand(editors);
939
- // }
940
- // editors[editorCount++] = editor;
941
872
editors .add (editor );
942
873
943
- // if (markedForClose != null ) {
944
- // Point p = markedForClose.getLocation();
945
- // handleClose(markedForClose, false);
946
- // // open the new window in
947
- // editor.setLocation(p );
948
- // }
874
+ if (storeOpenedSketches ) {
875
+ // Store information on who's open and running
876
+ // (in case there's a crash or something that can't be recovered)
877
+ storeSketches ();
878
+ PreferencesData . save ( );
879
+ }
949
880
950
881
// now that we're ready, show the window
951
882
// (don't do earlier, cuz we might move it based on a window being closed)
952
- if (showEditor )
953
- editor .setVisible (true );
954
-
955
- // System.err.println("exiting handleOpen");
883
+ if (showEditor ) {
884
+ SwingUtilities .invokeLater (new Runnable () {
885
+ @ Override
886
+ public void run () {
887
+ editor .setVisible (true );
888
+ }
889
+ });
890
+ }
956
891
957
892
return editor ;
958
893
}
@@ -1080,14 +1015,8 @@ public boolean handleQuit() {
1080
1015
* @return false if canceled along the way
1081
1016
*/
1082
1017
protected boolean handleQuitEach () {
1083
- int index = 0 ;
1084
1018
for (Editor editor : editors ) {
1085
- if (editor .checkModified ()) {
1086
- // Update to the new/final sketch path for this fella
1087
- storeSketchPath (editor , index );
1088
- index ++;
1089
-
1090
- } else {
1019
+ if (!editor .checkModified ()) {
1091
1020
return false ;
1092
1021
}
1093
1022
}
0 commit comments