@@ -1471,24 +1471,21 @@ public void actionPerformed(ActionEvent actionevent) {
1471
1471
ButtonGroup boardsButtonGroup = new ButtonGroup ();
1472
1472
Map <String , ButtonGroup > buttonGroupsMap = new HashMap <>();
1473
1473
1474
+ List <JMenu > platformMenus = new ArrayList <JMenu >();
1475
+
1474
1476
// Cycle through all packages
1475
- boolean first = true ;
1476
1477
for (TargetPackage targetPackage : BaseNoGui .packages .values ()) {
1477
1478
// For every package cycle through all platform
1478
1479
for (TargetPlatform targetPlatform : targetPackage .platforms ()) {
1479
1480
1480
- // Add a separator from the previous platform
1481
- if (!first )
1482
- boardMenu .add (new JSeparator ());
1483
- first = false ;
1484
-
1485
1481
// Add a title for each platform
1486
1482
String platformLabel = targetPlatform .getPreferences ().get ("name" );
1487
- if (platformLabel != null && !targetPlatform .getBoards ().isEmpty ()) {
1488
- JMenuItem menuLabel = new JMenuItem (tr (platformLabel ));
1489
- menuLabel .setEnabled (false );
1490
- boardMenu .add (menuLabel );
1491
- }
1483
+ if (platformLabel == null )
1484
+ platformLabel = targetPackage .getId () + "-" + targetPlatform .getId ();
1485
+
1486
+ JMenu platformBoardsMenu = new JMenu (tr (platformLabel ));
1487
+ MenuScroller .setScrollerFor (platformBoardsMenu );
1488
+ platformMenus .add (platformBoardsMenu );
1492
1489
1493
1490
// Cycle through all boards of this platform
1494
1491
for (TargetBoard board : targetPlatform .getBoards ().values ()) {
@@ -1497,14 +1494,40 @@ public void actionPerformed(ActionEvent actionevent) {
1497
1494
JMenuItem item = createBoardMenusAndCustomMenus (boardsCustomMenus , menuItemsToClickAfterStartup ,
1498
1495
buttonGroupsMap ,
1499
1496
board , targetPlatform , targetPackage );
1500
- boardMenu .add (item );
1497
+ platformBoardsMenu .add (item );
1501
1498
boardsButtonGroup .add (item );
1502
1499
}
1503
1500
}
1504
1501
}
1505
1502
1503
+ Collections .sort (platformMenus , new JMenuItemTextComparator ());
1504
+
1505
+ JMenuItem firstBoardItem = null ;
1506
+ if (platformMenus .size () == 1 ) {
1507
+ // When just one platform exists, add the board items directly,
1508
+ // rather than using a submenu
1509
+ for (Component boardItem : platformMenus .get (0 ).getMenuComponents ()) {
1510
+ boardMenu .add (boardItem );
1511
+ if (firstBoardItem == null )
1512
+ firstBoardItem = (JMenuItem )boardItem ;
1513
+ }
1514
+ } else {
1515
+ // For multiple platforms, use submenus
1516
+ for (JMenu platformMenu : platformMenus ) {
1517
+ if (firstBoardItem == null && platformMenu .getItemCount () > 0 )
1518
+ firstBoardItem = platformMenu .getItem (0 );
1519
+ boardMenu .add (platformMenu );
1520
+ }
1521
+ }
1522
+
1523
+ if (firstBoardItem == null ) {
1524
+ throw new IllegalStateException ("No available boards" );
1525
+ }
1526
+
1527
+ // If there is no current board yet (first startup, or selected
1528
+ // board no longer defined), select first available board.
1506
1529
if (menuItemsToClickAfterStartup .isEmpty ()) {
1507
- menuItemsToClickAfterStartup .add (selectFirstEnabledMenuItem ( boardMenu ) );
1530
+ menuItemsToClickAfterStartup .add (firstBoardItem );
1508
1531
}
1509
1532
1510
1533
for (JMenuItem menuItemToClick : menuItemsToClickAfterStartup ) {
@@ -1658,16 +1681,6 @@ private static JMenuItem selectVisibleSelectedOrFirstMenuItem(JMenu menu) {
1658
1681
throw new IllegalStateException ("Menu has no enabled items" );
1659
1682
}
1660
1683
1661
- private static JMenuItem selectFirstEnabledMenuItem (JMenu menu ) {
1662
- for (int i = 1 ; i < menu .getItemCount (); i ++) {
1663
- JMenuItem item = menu .getItem (i );
1664
- if (item != null && item .isEnabled ()) {
1665
- return item ;
1666
- }
1667
- }
1668
- throw new IllegalStateException ("Menu has no enabled items" );
1669
- }
1670
-
1671
1684
public void rebuildProgrammerMenu () {
1672
1685
programmerMenus = new LinkedList <>();
1673
1686
0 commit comments