Skip to content

Commit b0cb2c4

Browse files
author
Federico Fissore
committed
Added File > Recent menu: shows last 5 opened sketches, sorted in reverse chronological order
1 parent e375571 commit b0cb2c4

File tree

3 files changed

+117
-29
lines changed

3 files changed

+117
-29
lines changed

app/src/processing/app/Base.java

+73-8
Original file line numberDiff line numberDiff line change
@@ -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
}

app/src/processing/app/Editor.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
public class Editor extends JFrame implements RunnerListener {
7272

7373
private final Platform platform;
74+
private JMenu recentSketchesMenu;
7475

7576
private static class ShouldSaveIfModified implements Predicate<Sketch> {
7677

@@ -523,10 +524,10 @@ protected void buildMenuBar() throws Exception {
523524
public void menuSelected(MenuEvent e) {
524525
List<Component> components = Arrays.asList(fileMenu.getComponents());
525526
if (!components.contains(sketchbookMenu)) {
526-
fileMenu.insert(sketchbookMenu, 2);
527+
fileMenu.insert(sketchbookMenu, 3);
527528
}
528529
if (!components.contains(sketchbookMenu)) {
529-
fileMenu.insert(examplesMenu, 3);
530+
fileMenu.insert(examplesMenu, 4);
530531
}
531532
fileMenu.revalidate();
532533
validate();
@@ -603,6 +604,16 @@ public void actionPerformed(ActionEvent e) {
603604
});
604605
fileMenu.add(item);
605606

607+
base.rebuildRecentSketchesMenuItems();
608+
recentSketchesMenu = new JMenu(_("Recent"));
609+
SwingUtilities.invokeLater(new Runnable() {
610+
@Override
611+
public void run() {
612+
rebuildRecentSketchesMenu();
613+
}
614+
});
615+
fileMenu.add(recentSketchesMenu);
616+
606617
if (sketchbookMenu == null) {
607618
sketchbookMenu = new JMenu(_("Sketchbook"));
608619
MenuScroller.setScrollerFor(sketchbookMenu);
@@ -684,6 +695,12 @@ public void actionPerformed(ActionEvent e) {
684695
return fileMenu;
685696
}
686697

698+
public void rebuildRecentSketchesMenu() {
699+
recentSketchesMenu.removeAll();
700+
for (JMenuItem recentSketchMenuItem : base.getRecentSketchesMenuItems()) {
701+
recentSketchesMenu.add(recentSketchMenuItem);
702+
}
703+
}
687704

688705
protected void buildSketchMenu(JMenu sketchMenu) {
689706
sketchMenu.removeAll();

arduino-core/src/processing/app/PreferencesData.java

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package processing.app;
22

3-
import static processing.app.I18n._;
3+
import com.google.common.base.Joiner;
4+
import org.apache.commons.compress.utils.IOUtils;
5+
import processing.app.helpers.PreferencesHelper;
6+
import processing.app.helpers.PreferencesMap;
7+
import processing.app.legacy.PApplet;
8+
import processing.app.legacy.PConstants;
49

510
import java.awt.*;
6-
import java.io.BufferedReader;
711
import java.io.File;
812
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.io.InputStreamReader;
1113
import java.io.PrintWriter;
1214
import java.util.Arrays;
15+
import java.util.Collection;
1316
import java.util.Iterator;
1417
import java.util.MissingResourceException;
1518

16-
import org.apache.commons.compress.utils.IOUtils;
17-
import processing.app.helpers.PreferencesHelper;
18-
import processing.app.helpers.PreferencesMap;
19-
import processing.app.legacy.PApplet;
20-
import processing.app.legacy.PConstants;
19+
import static processing.app.I18n._;
2120

2221

2322
public class PreferencesData {
@@ -51,14 +50,14 @@ static public void init(File file) {
5150
prefs.load(new File(BaseNoGui.getContentFile("lib"), PREFS_FILE));
5251
} catch (IOException e) {
5352
BaseNoGui.showError(null, _("Could not read default settings.\n" +
54-
"You'll need to reinstall Arduino."), e);
53+
"You'll need to reinstall Arduino."), e);
5554
}
5655

5756
// set some runtime constants (not saved on preferences file)
5857
File hardwareFolder = BaseNoGui.getHardwareFolder();
5958
prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath());
6059
prefs.put("runtime.ide.version", "" + BaseNoGui.REVISION);
61-
60+
6261
// clone the hash table
6362
defaults = new PreferencesMap(prefs);
6463

@@ -68,10 +67,10 @@ static public void init(File file) {
6867
prefs.load(preferencesFile);
6968
} catch (IOException ex) {
7069
BaseNoGui.showError(_("Error reading preferences"),
71-
I18n.format(_("Error reading the preferences file. "
72-
+ "Please delete (or move)\n"
73-
+ "{0} and restart Arduino."),
74-
preferencesFile.getAbsolutePath()), ex);
70+
I18n.format(_("Error reading the preferences file. "
71+
+ "Please delete (or move)\n"
72+
+ "{0} and restart Arduino."),
73+
preferencesFile.getAbsolutePath()), ex);
7574
}
7675
}
7776

@@ -197,8 +196,7 @@ static public void setInteger(String key, int value) {
197196
}
198197

199198
// get a copy of the Preferences
200-
static public PreferencesMap getMap()
201-
{
199+
static public PreferencesMap getMap() {
202200
return new PreferencesMap(prefs);
203201
}
204202

@@ -211,8 +209,7 @@ static public void removeAllKeysWithPrefix(String prefix) {
211209

212210
// Decide wether changed preferences will be saved. When value is
213211
// false, Preferences.save becomes a no-op.
214-
static public void setDoSave(boolean value)
215-
{
212+
static public void setDoSave(boolean value) {
216213
doSave = value;
217214
}
218215

@@ -225,4 +222,13 @@ static public Font getFont(String attr) {
225222
}
226223
return font;
227224
}
225+
226+
public static Collection<String> getCollection(String key) {
227+
return Arrays.asList(get(key, "").split(","));
228+
}
229+
230+
public static void setCollection(String key, Collection<String> values) {
231+
String value = Joiner.on(',').join(values);
232+
set(key, value);
233+
}
228234
}

0 commit comments

Comments
 (0)