Skip to content

Commit b7152b2

Browse files
committed
Implement "Recently Used Boards" Menu
1 parent 7a9234c commit b7152b2

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

Diff for: app/src/processing/app/Base.java

+71-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ public class Base {
122122
private List<JMenu> boardsCustomMenus;
123123
private List<JMenuItem> programmerMenus;
124124

125+
// these variables help rebuild the "recently used boards"
126+
// menu on board selection
127+
private HashMap<String, JMenuItem> recentBoardItems;
128+
private List<JMenuItem> recentBoardsToClear = new LinkedList<>();;
129+
private JMenu boardMenu;
130+
private int recentBoardsJMenuIndex;
131+
125132
private PdeKeywords pdeKeywords;
126133
private final List<JMenuItem> recentSketchesMenuItems = new LinkedList<>();
127134

@@ -1360,6 +1367,26 @@ public void onBoardOrPortChange() {
13601367
}
13611368
}
13621369

1370+
// Update recent boards list in preferences
1371+
List<String> newRecentBoardIds = new ArrayList<String>();
1372+
String currentBoard = PreferencesData.get("board");
1373+
for (String recentBoard : PreferencesData.getCollection("recent.boards")){
1374+
if (!recentBoard.equals(currentBoard)) {
1375+
newRecentBoardIds.add(recentBoard);
1376+
}
1377+
}
1378+
newRecentBoardIds.add(0, currentBoard);
1379+
if (newRecentBoardIds.size() == 6) {
1380+
newRecentBoardIds.remove(5);
1381+
}
1382+
PreferencesData.setCollection("recent.boards", newRecentBoardIds);
1383+
try {
1384+
rebuildRecentBoardsList();
1385+
} catch (Exception e) {
1386+
//TODO show error
1387+
e.printStackTrace();
1388+
}
1389+
13631390
// Update editors status bar
13641391
for (Editor editor : editors) {
13651392
editor.onBoardOrPortChange();
@@ -1433,9 +1460,10 @@ protected void onIndexesUpdated() throws Exception {
14331460

14341461
public void rebuildBoardsMenu() throws Exception {
14351462
boardsCustomMenus = new LinkedList<>();
1463+
recentBoardItems = new HashMap<String, JMenuItem>();
14361464

14371465
// The first custom menu is the "Board" selection submenu
1438-
JMenu boardMenu = new JMenu(tr("Board"));
1466+
boardMenu = new JMenu(tr("Board"));
14391467
boardMenu.putClientProperty("removeOnWindowDeactivation", true);
14401468
MenuScroller.setScrollerFor(boardMenu).setTopFixedCount(1);
14411469

@@ -1458,6 +1486,16 @@ public void actionPerformed(ActionEvent actionevent) {
14581486
}));
14591487
boardsCustomMenus.add(boardMenu);
14601488

1489+
// Insert recently used boards menu and remember index for insertion later
1490+
if (PreferencesData.has("recent.boards")) {
1491+
// Insert menu label
1492+
boardMenu.add(new JSeparator());
1493+
JMenuItem label = new JMenuItem(tr("Recently Used Boards"));
1494+
label.setEnabled(false);
1495+
boardMenu.add(label);
1496+
recentBoardsJMenuIndex = boardMenu.getItemCount();
1497+
}
1498+
14611499
// If there are no platforms installed we are done
14621500
if (BaseNoGui.packages.size() == 0)
14631501
return;
@@ -1555,6 +1593,23 @@ private String getPlatformUniqueId(TargetPlatform platform) {
15551593
return platform.getId() + "_" + platform.getFolder();
15561594
}
15571595

1596+
// clear the previous menu items from the "recently used boards"
1597+
// menu and repopulate with updated items
1598+
private void rebuildRecentBoardsList() throws Exception {
1599+
Collection<String> recentBoardIds = new LinkedList<>();
1600+
recentBoardIds = PreferencesData.getCollection("recent.boards");
1601+
int idxAdv = 0;
1602+
for (JMenuItem itemToClear : recentBoardsToClear) {
1603+
boardMenu.remove(itemToClear);
1604+
}
1605+
recentBoardsToClear.clear();
1606+
for (String boardId : recentBoardIds) {
1607+
boardMenu.add(recentBoardItems.get(boardId), recentBoardsJMenuIndex+idxAdv);
1608+
recentBoardsToClear.add(recentBoardItems.get(boardId));
1609+
idxAdv++;
1610+
}
1611+
}
1612+
15581613
private JRadioButtonMenuItem createBoardMenusAndCustomMenus(
15591614
final List<JMenu> boardsCustomMenus, List<JMenuItem> menuItemsToClickAfterStartup,
15601615
Map<String, ButtonGroup> buttonGroupsMap,
@@ -1585,6 +1640,21 @@ public void actionPerformed(ActionEvent actionevent) {
15851640

15861641
JRadioButtonMenuItem item = new JRadioButtonMenuItem(action);
15871642

1643+
// create an action for the "recent boards" copy of this menu item
1644+
// which clicks the original menu item
1645+
Action actionClone = new AbstractAction(board.getName()) {
1646+
public void actionPerformed(ActionEvent actionevent) {
1647+
item.setSelected(true);
1648+
item.getAction().actionPerformed(new ActionEvent(this, -1, ""));
1649+
}
1650+
};
1651+
1652+
// create a menu item for the "recent boards" menu
1653+
JMenuItem itemClone = new JMenuItem(actionClone);
1654+
1655+
// populate list of menuitem copies
1656+
recentBoardItems.put(boardId, itemClone);
1657+
15881658
if (selBoard.equals(boardId) && selPackage.equals(packageName)
15891659
&& selPlatform.equals(platformName)) {
15901660
menuItemsToClickAfterStartup.add(item);

0 commit comments

Comments
 (0)