@@ -86,6 +86,8 @@ public boolean apply(UserLibrary library) {
8686 }
8787 };
8888
89+ private static final int RECENT_SKETCHES_MAX_SIZE = 5 ;
90+
8991 private static boolean commandLine ;
9092 public static volatile Base INSTANCE ;
9193
@@ -117,6 +119,7 @@ public boolean apply(UserLibrary library) {
117119 private volatile Action openBoardsManager ;
118120
119121 private final PdeKeywords pdeKeywords ;
122+ private final List <JMenuItem > recentSketchesMenuItems ;
120123
121124 static public void main (String args []) throws Exception {
122125 System .setProperty ("awt.useSystemAAFontSettings" , "on" );
@@ -269,6 +272,7 @@ static public File absoluteFile(String path) {
269272
270273 public Base (String [] args ) throws Exception {
271274 BaseNoGui .notifier = new GUIUserNotifier (this );
275+ this .recentSketchesMenuItems = new LinkedList <JMenuItem >();
272276
273277 String sketchbookPath = BaseNoGui .getSketchbookPath ();
274278
@@ -316,7 +320,7 @@ public Base(String[] args) throws Exception {
316320 boolean showEditor = parser .isGuiMode ();
317321 if (!parser .isForceSavePrefs ())
318322 PreferencesData .setDoSave (showEditor );
319- if (handleOpen (file , nextEditorLocation (), showEditor ) == null ) {
323+ if (handleOpen (file , nextEditorLocation (), showEditor , false ) == null ) {
320324 String mess = I18n .format (_ ("Failed to open sketch: \" {0}\" " ), path );
321325 // Open failure is fatal in upload/verify mode
322326 if (parser .isVerifyOrUploadMode ())
@@ -547,7 +551,7 @@ protected boolean restoreSketches() throws Exception {
547551 location = nextEditorLocation ();
548552 }
549553 // If file did not exist, null will be returned for the Editor
550- if (handleOpen (new File (path ), location , true , false ) != null ) {
554+ if (handleOpen (new File (path ), location , true , false , false ) != null ) {
551555 opened ++;
552556 }
553557 }
@@ -594,12 +598,25 @@ protected void storeSketches() {
594598 PreferencesData .setInteger ("last.sketch.count" , index );
595599 }
596600
601+ protected void storeRecentSketches (Sketch sketch ) {
602+ if (sketch .isUntitled ()) {
603+ return ;
604+ }
605+
606+ Set <String > sketches = new LinkedHashSet <String >();
607+ sketches .add (sketch .getMainFilePath ());
608+ sketches .addAll (PreferencesData .getCollection ("recent.sketches" ));
609+
610+ PreferencesData .setCollection ("recent.sketches" , sketches );
611+ }
612+
597613 // Because of variations in native windowing systems, no guarantees about
598614 // changes to the focused and active Windows can be made. Developers must
599615 // never assume that this Window is the focused or active Window until this
600616 // Window receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED event.
601617 protected void handleActivated (Editor whichEditor ) {
602618 activeEditor = whichEditor ;
619+ activeEditor .rebuildRecentSketchesMenu ();
603620
604621 // set the current window to be the console that's getting output
605622 EditorConsoleStream .setCurrent (activeEditor .console );
@@ -728,8 +745,7 @@ public void handleNew() throws Exception {
728745 try {
729746 File file = createNewUntitled ();
730747 if (file != null ) {
731- Editor editor = handleOpen (file );
732- editor .untitled = true ;
748+ Editor editor = handleOpen (file , true );
733749 }
734750
735751 } catch (IOException e ) {
@@ -837,14 +853,18 @@ public boolean accept(File dir, String name) {
837853 * @throws Exception
838854 */
839855 public Editor handleOpen (File file ) throws Exception {
840- return handleOpen (file , nextEditorLocation (), true );
856+ return handleOpen (file , false );
841857 }
842858
843- protected Editor handleOpen (File file , int [] location , boolean showEditor ) throws Exception {
844- return handleOpen (file , location , showEditor , true );
859+ public Editor handleOpen (File file , boolean untitled ) throws Exception {
860+ return handleOpen (file , nextEditorLocation (), true , untitled );
845861 }
846862
847- protected Editor handleOpen (File file , int [] location , boolean showEditor , boolean storeOpenedSketches ) throws Exception {
863+ protected Editor handleOpen (File file , int [] location , boolean showEditor , boolean untitled ) throws Exception {
864+ return handleOpen (file , location , showEditor , true , untitled );
865+ }
866+
867+ protected Editor handleOpen (File file , int [] location , boolean showEditor , boolean storeOpenedSketches , boolean untitled ) throws Exception {
848868 if (!file .exists ()) return null ;
849869
850870 // Cycle through open windows to make sure that it's not already open.
@@ -863,12 +883,16 @@ protected Editor handleOpen(File file, int[] location, boolean showEditor, boole
863883 return null ; // Just walk away quietly
864884 }
865885
886+ editor .untitled = untitled ;
887+
866888 editors .add (editor );
867889
868890 if (storeOpenedSketches ) {
869891 // Store information on who's open and running
870892 // (in case there's a crash or something that can't be recovered)
871893 storeSketches ();
894+ storeRecentSketches (editor .getSketch ());
895+ rebuildRecentSketchesMenuItems ();
872896 PreferencesData .save ();
873897 }
874898
@@ -886,6 +910,42 @@ public void run() {
886910 return editor ;
887911 }
888912
913+ protected void rebuildRecentSketchesMenuItems () {
914+ Set <File > recentSketches = new LinkedHashSet <File >() {
915+
916+ @ Override
917+ public boolean add (File file ) {
918+ if (size () >= RECENT_SKETCHES_MAX_SIZE ) {
919+ return false ;
920+ }
921+ return super .add (file );
922+ }
923+ };
924+
925+ for (String path : PreferencesData .getCollection ("recent.sketches" )) {
926+ File file = new File (path );
927+ if (file .exists ()) {
928+ recentSketches .add (file );
929+ }
930+ }
931+
932+ recentSketchesMenuItems .clear ();
933+ for (final File recentSketch : recentSketches ) {
934+ JMenuItem recentSketchMenuItem = new JMenuItem (recentSketch .getParentFile ().getName ());
935+ recentSketchMenuItem .addActionListener (new ActionListener () {
936+ @ Override
937+ public void actionPerformed (ActionEvent actionEvent ) {
938+ try {
939+ handleOpen (recentSketch );
940+ } catch (Exception e ) {
941+ e .printStackTrace ();
942+ }
943+ }
944+ });
945+ recentSketchesMenuItems .add (recentSketchMenuItem );
946+ }
947+ }
948+
889949
890950 /**
891951 * Close a sketch as specified by its editor window.
@@ -912,6 +972,7 @@ public boolean handleClose(Editor editor) {
912972 //ignore
913973 }
914974 storeSketches ();
975+ rebuildRecentSketchesMenuItems ();
915976
916977 // Save out the current prefs state
917978 PreferencesData .save ();
@@ -2463,4 +2524,8 @@ public Action getOpenBoardsManager() {
24632524 public PdeKeywords getPdeKeywords () {
24642525 return pdeKeywords ;
24652526 }
2527+
2528+ public List <JMenuItem > getRecentSketchesMenuItems () {
2529+ return recentSketchesMenuItems ;
2530+ }
24662531}
0 commit comments