@@ -86,6 +86,8 @@ public boolean apply(UserLibrary library) {
86
86
}
87
87
};
88
88
89
+ private static final int RECENT_SKETCHES_MAX_SIZE = 5 ;
90
+
89
91
private static boolean commandLine ;
90
92
public static volatile Base INSTANCE ;
91
93
@@ -117,6 +119,7 @@ public boolean apply(UserLibrary library) {
117
119
private volatile Action openBoardsManager ;
118
120
119
121
private final PdeKeywords pdeKeywords ;
122
+ private final List <JMenuItem > recentSketchesMenuItems ;
120
123
121
124
static public void main (String args []) throws Exception {
122
125
System .setProperty ("awt.useSystemAAFontSettings" , "on" );
@@ -269,6 +272,7 @@ static public File absoluteFile(String path) {
269
272
270
273
public Base (String [] args ) throws Exception {
271
274
BaseNoGui .notifier = new GUIUserNotifier (this );
275
+ this .recentSketchesMenuItems = new LinkedList <JMenuItem >();
272
276
273
277
String sketchbookPath = BaseNoGui .getSketchbookPath ();
274
278
@@ -316,7 +320,7 @@ public Base(String[] args) throws Exception {
316
320
boolean showEditor = parser .isGuiMode ();
317
321
if (!parser .isForceSavePrefs ())
318
322
PreferencesData .setDoSave (showEditor );
319
- if (handleOpen (file , nextEditorLocation (), showEditor ) == null ) {
323
+ if (handleOpen (file , nextEditorLocation (), showEditor , false ) == null ) {
320
324
String mess = I18n .format (_ ("Failed to open sketch: \" {0}\" " ), path );
321
325
// Open failure is fatal in upload/verify mode
322
326
if (parser .isVerifyOrUploadMode ())
@@ -547,7 +551,7 @@ protected boolean restoreSketches() throws Exception {
547
551
location = nextEditorLocation ();
548
552
}
549
553
// 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 ) {
551
555
opened ++;
552
556
}
553
557
}
@@ -594,12 +598,25 @@ protected void storeSketches() {
594
598
PreferencesData .setInteger ("last.sketch.count" , index );
595
599
}
596
600
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
+
597
613
// Because of variations in native windowing systems, no guarantees about
598
614
// changes to the focused and active Windows can be made. Developers must
599
615
// never assume that this Window is the focused or active Window until this
600
616
// Window receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED event.
601
617
protected void handleActivated (Editor whichEditor ) {
602
618
activeEditor = whichEditor ;
619
+ activeEditor .rebuildRecentSketchesMenu ();
603
620
604
621
// set the current window to be the console that's getting output
605
622
EditorConsoleStream .setCurrent (activeEditor .console );
@@ -728,8 +745,7 @@ public void handleNew() throws Exception {
728
745
try {
729
746
File file = createNewUntitled ();
730
747
if (file != null ) {
731
- Editor editor = handleOpen (file );
732
- editor .untitled = true ;
748
+ Editor editor = handleOpen (file , true );
733
749
}
734
750
735
751
} catch (IOException e ) {
@@ -837,14 +853,18 @@ public boolean accept(File dir, String name) {
837
853
* @throws Exception
838
854
*/
839
855
public Editor handleOpen (File file ) throws Exception {
840
- return handleOpen (file , nextEditorLocation (), true );
856
+ return handleOpen (file , false );
841
857
}
842
858
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 );
845
861
}
846
862
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 {
848
868
if (!file .exists ()) return null ;
849
869
850
870
// 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
863
883
return null ; // Just walk away quietly
864
884
}
865
885
886
+ editor .untitled = untitled ;
887
+
866
888
editors .add (editor );
867
889
868
890
if (storeOpenedSketches ) {
869
891
// Store information on who's open and running
870
892
// (in case there's a crash or something that can't be recovered)
871
893
storeSketches ();
894
+ storeRecentSketches (editor .getSketch ());
895
+ rebuildRecentSketchesMenuItems ();
872
896
PreferencesData .save ();
873
897
}
874
898
@@ -886,6 +910,42 @@ public void run() {
886
910
return editor ;
887
911
}
888
912
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
+
889
949
890
950
/**
891
951
* Close a sketch as specified by its editor window.
@@ -912,6 +972,7 @@ public boolean handleClose(Editor editor) {
912
972
//ignore
913
973
}
914
974
storeSketches ();
975
+ rebuildRecentSketchesMenuItems ();
915
976
916
977
// Save out the current prefs state
917
978
PreferencesData .save ();
@@ -2463,4 +2524,8 @@ public Action getOpenBoardsManager() {
2463
2524
public PdeKeywords getPdeKeywords () {
2464
2525
return pdeKeywords ;
2465
2526
}
2527
+
2528
+ public List <JMenuItem > getRecentSketchesMenuItems () {
2529
+ return recentSketchesMenuItems ;
2530
+ }
2466
2531
}
0 commit comments