@@ -532,8 +532,11 @@ protected boolean restoreSketches() throws Exception {
532532 // Save the sketch path and window placement for each open sketch
533533 int count = PreferencesData .getInteger ("last.sketch.count" );
534534 int opened = 0 ;
535- for (int i = 0 ; i < count ; i ++ ) {
535+ for (int i = count - 1 ; i >= 0 ; i -- ) {
536536 String path = PreferencesData .get ("last.sketch" + i + ".path" );
537+ if (path == null ) {
538+ continue ;
539+ }
537540 if (BaseNoGui .getPortableFolder () != null ) {
538541 File absolute = new File (BaseNoGui .getPortableFolder (), path );
539542 try {
@@ -550,7 +553,7 @@ protected boolean restoreSketches() throws Exception {
550553 location = nextEditorLocation ();
551554 }
552555 // 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 ) {
554557 opened ++;
555558 }
556559 }
@@ -571,19 +574,21 @@ protected void storeSketches() {
571574 String untitledPath = untitledFolder .getAbsolutePath ();
572575
573576 // Save the sketch path and window placement for each open sketch
577+ LinkedList <Editor > reverseEditors = new LinkedList <Editor >(editors );
578+ Collections .reverse (reverseEditors );
574579 int index = 0 ;
575- for (Editor editor : editors ) {
580+ for (Editor editor : reverseEditors ) {
576581 String path = editor .getSketch ().getMainFilePath ();
577582 // In case of a crash, save untitled sketches if they contain changes.
578583 // (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 ()) {
581585 continue ;
582586 }
583587 if (BaseNoGui .getPortableFolder () != null ) {
584588 path = FileUtils .relativePath (BaseNoGui .getPortableFolder ().toString (), path );
585- if (path == null )
589+ if (path == null ) {
586590 continue ;
591+ }
587592 }
588593 PreferencesData .set ("last.sketch" + index + ".path" , path );
589594
@@ -595,45 +600,6 @@ protected void storeSketches() {
595600 PreferencesData .setInteger ("last.sketch.count" , index );
596601 }
597602
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-
637603 // Because of variations in native windowing systems, no guarantees about
638604 // changes to the focused and active Windows can be made. Developers must
639605 // never assume that this Window is the focused or active Window until this
@@ -880,79 +846,48 @@ public Editor handleOpen(File file) throws Exception {
880846 return handleOpen (file , nextEditorLocation (), true );
881847 }
882848
883-
884849 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+ }
886852
853+ protected Editor handleOpen (File file , int [] location , boolean showEditor , boolean storeOpenedSketches ) throws Exception {
887854 if (!file .exists ()) return null ;
888855
889- // System.err.println(" editors: " + editors);
890856 // Cycle through open windows to make sure that it's not already open.
891857 String path = file .getAbsolutePath ();
892858 for (Editor editor : editors ) {
893859 if (editor .getSketch ().getMainFilePath ().equals (path )) {
894860 editor .toFront ();
895- // System.err.println(" handleOpen: already opened");
896861 return editor ;
897862 }
898863 }
899864
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 ());
927866
928867 // Make sure that the sketch actually loaded
929868 if (editor .getSketch () == null ) {
930- // System.err.println("sketch was null, getting out of handleOpen");
931869 return null ; // Just walk away quietly
932870 }
933871
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;
941872 editors .add (editor );
942873
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+ }
949880
950881 // now that we're ready, show the window
951882 // (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+ }
956891
957892 return editor ;
958893 }
@@ -1080,14 +1015,8 @@ public boolean handleQuit() {
10801015 * @return false if canceled along the way
10811016 */
10821017 protected boolean handleQuitEach () {
1083- int index = 0 ;
10841018 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 ()) {
10911020 return false ;
10921021 }
10931022 }
0 commit comments