@@ -118,6 +118,11 @@ public class Base {
118
118
Editor activeEditor ;
119
119
120
120
private static JMenu boardMenu ;
121
+ private static ButtonGroup boardsButtonGroup ;
122
+ private static ButtonGroup recentBoardsButtonGroup ;
123
+ private static Map <String , ButtonGroup > buttonGroupsMap ;
124
+ private static List <JMenuItem > menuItemsToClickAfterStartup ;
125
+ private static MenuScroller boardMenuScroller ;
121
126
122
127
// these menus are shared so that the board and serial port selections
123
128
// are the same for all windows (since the board and serial port that are
@@ -1335,6 +1340,41 @@ public void selectTargetBoard(TargetBoard targetBoard) {
1335
1340
onBoardOrPortChange ();
1336
1341
rebuildImportMenu (Editor .importMenu );
1337
1342
rebuildExamplesMenu (Editor .examplesMenu );
1343
+ try {
1344
+ rebuildRecentBoardsMenu ();
1345
+ } catch (Exception e ) {
1346
+ // fail silently
1347
+ }
1348
+ }
1349
+
1350
+ public void rebuildRecentBoardsMenu () throws Exception {
1351
+
1352
+ Enumeration <AbstractButton > btns = recentBoardsButtonGroup .getElements ();
1353
+ while (btns .hasMoreElements ()) {
1354
+ AbstractButton x = btns .nextElement ();
1355
+ if (x .isSelected ()) {
1356
+ return ;
1357
+ }
1358
+ }
1359
+ btns = recentBoardsButtonGroup .getElements ();
1360
+ while (btns .hasMoreElements ()) {
1361
+ AbstractButton x = btns .nextElement ();
1362
+ boardMenu .remove (x );
1363
+ }
1364
+ int index = 0 ;
1365
+ for (TargetBoard board : BaseNoGui .getRecentlyUsedBoards ()) {
1366
+ JMenuItem item = createBoardMenusAndCustomMenus (boardsCustomMenus , menuItemsToClickAfterStartup ,
1367
+ buttonGroupsMap ,
1368
+ board , board .getContainerPlatform (), board .getContainerPlatform ().getContainerPackage ());
1369
+ boardMenu .insert (item , 3 );
1370
+ item .setAccelerator (KeyStroke .getKeyStroke ('1' + index ,
1371
+ Toolkit .getDefaultToolkit ().getMenuShortcutKeyMask () |
1372
+ ActionEvent .SHIFT_MASK ));
1373
+ recentBoardsButtonGroup .add (item );
1374
+ boardsButtonGroup .add (item );
1375
+ index ++;
1376
+ }
1377
+ boardMenuScroller .setTopFixedCount (3 + index );
1338
1378
}
1339
1379
1340
1380
public void onBoardOrPortChange () {
@@ -1432,7 +1472,8 @@ public void rebuildBoardsMenu() throws Exception {
1432
1472
// The first custom menu is the "Board" selection submenu
1433
1473
boardMenu = new JMenu (tr ("Board" ));
1434
1474
boardMenu .putClientProperty ("removeOnWindowDeactivation" , true );
1435
- MenuScroller .setScrollerFor (boardMenu ).setTopFixedCount (1 );
1475
+ boardMenuScroller = MenuScroller .setScrollerFor (boardMenu );
1476
+ boardMenuScroller .setTopFixedCount (1 );
1436
1477
1437
1478
boardMenu .add (new JMenuItem (new AbstractAction (tr ("Boards Manager..." )) {
1438
1479
public void actionPerformed (ActionEvent actionevent ) {
@@ -1472,21 +1513,26 @@ public void actionPerformed(ActionEvent actionevent) {
1472
1513
boardsCustomMenus .add (customMenu );
1473
1514
}
1474
1515
1475
- List <JMenuItem > menuItemsToClickAfterStartup = new LinkedList <>();
1516
+ List <JMenuItem > _menuItemsToClickAfterStartup = new LinkedList <>();
1517
+ boardsButtonGroup = new ButtonGroup ();
1518
+ recentBoardsButtonGroup = new ButtonGroup ();
1519
+ buttonGroupsMap = new HashMap <>();
1476
1520
1477
- ButtonGroup boardsButtonGroup = new ButtonGroup ();
1478
- Map <String , ButtonGroup > buttonGroupsMap = new HashMap <>();
1521
+ boolean hasRecentBoardsMenu = (PreferencesData .getInteger ("editor.recent_boards.size" , 4 ) != 0 );
1522
+
1523
+ if (hasRecentBoardsMenu ) {
1524
+ JMenuItem recentLabel = new JMenuItem (tr ("Recently used boards" ));
1525
+ recentLabel .setEnabled (false );
1526
+ boardMenu .add (recentLabel );
1527
+ }
1479
1528
1480
1529
// Cycle through all packages
1481
- boolean first = true ;
1482
1530
for (TargetPackage targetPackage : BaseNoGui .packages .values ()) {
1483
1531
// For every package cycle through all platform
1484
1532
for (TargetPlatform targetPlatform : targetPackage .platforms ()) {
1485
1533
1486
1534
// Add a separator from the previous platform
1487
- if (!first )
1488
- boardMenu .add (new JSeparator ());
1489
- first = false ;
1535
+ boardMenu .add (new JSeparator ());
1490
1536
1491
1537
// Add a title for each platform
1492
1538
String platformLabel = targetPlatform .getPreferences ().get ("name" );
@@ -1500,7 +1546,7 @@ public void actionPerformed(ActionEvent actionevent) {
1500
1546
for (TargetBoard board : targetPlatform .getBoards ().values ()) {
1501
1547
if (board .getPreferences ().get ("hide" ) != null )
1502
1548
continue ;
1503
- JMenuItem item = createBoardMenusAndCustomMenus (boardsCustomMenus , menuItemsToClickAfterStartup ,
1549
+ JMenuItem item = createBoardMenusAndCustomMenus (boardsCustomMenus , _menuItemsToClickAfterStartup ,
1504
1550
buttonGroupsMap ,
1505
1551
board , targetPlatform , targetPackage );
1506
1552
boardMenu .add (item );
@@ -1509,14 +1555,16 @@ public void actionPerformed(ActionEvent actionevent) {
1509
1555
}
1510
1556
}
1511
1557
1512
- if (menuItemsToClickAfterStartup .isEmpty ()) {
1513
- menuItemsToClickAfterStartup .add (selectFirstEnabledMenuItem (boardMenu ));
1558
+ if (_menuItemsToClickAfterStartup .isEmpty ()) {
1559
+ _menuItemsToClickAfterStartup .add (selectFirstEnabledMenuItem (boardMenu ));
1514
1560
}
1515
1561
1516
- for (JMenuItem menuItemToClick : menuItemsToClickAfterStartup ) {
1562
+ for (JMenuItem menuItemToClick : _menuItemsToClickAfterStartup ) {
1517
1563
menuItemToClick .setSelected (true );
1518
1564
menuItemToClick .getAction ().actionPerformed (new ActionEvent (this , -1 , "" ));
1519
1565
}
1566
+
1567
+ menuItemsToClickAfterStartup = _menuItemsToClickAfterStartup ;
1520
1568
}
1521
1569
1522
1570
private JRadioButtonMenuItem createBoardMenusAndCustomMenus (
@@ -1552,6 +1600,9 @@ public void actionPerformed(ActionEvent actionevent) {
1552
1600
for (final String menuId : customMenus .keySet ()) {
1553
1601
String title = customMenus .get (menuId );
1554
1602
JMenu menu = getBoardCustomMenu (tr (title ));
1603
+ if (menu == null ) {
1604
+ continue ;
1605
+ }
1555
1606
1556
1607
if (board .hasMenu (menuId )) {
1557
1608
PreferencesMap boardCustomMenu = board .getMenuLabels (menuId );
@@ -1614,13 +1665,13 @@ private static boolean ifThereAreVisibleItemsOn(JMenu menu) {
1614
1665
return false ;
1615
1666
}
1616
1667
1617
- private JMenu getBoardCustomMenu (String label ) throws Exception {
1668
+ private JMenu getBoardCustomMenu (String label ) {
1618
1669
for (JMenu menu : boardsCustomMenus ) {
1619
1670
if (label .equals (menu .getText ())) {
1620
1671
return menu ;
1621
1672
}
1622
1673
}
1623
- throw new Exception ( "Custom menu not found!" ) ;
1674
+ return null ;
1624
1675
}
1625
1676
1626
1677
public List <JMenuItem > getProgrammerMenus () {
0 commit comments