@@ -1471,24 +1471,21 @@ public void actionPerformed(ActionEvent actionevent) {
14711471 ButtonGroup boardsButtonGroup = new ButtonGroup ();
14721472 Map <String , ButtonGroup > buttonGroupsMap = new HashMap <>();
14731473
1474+ List <JMenu > platformMenus = new ArrayList <JMenu >();
1475+
14741476 // Cycle through all packages
1475- boolean first = true ;
14761477 for (TargetPackage targetPackage : BaseNoGui .packages .values ()) {
14771478 // For every package cycle through all platform
14781479 for (TargetPlatform targetPlatform : targetPackage .platforms ()) {
14791480
1480- // Add a separator from the previous platform
1481- if (!first )
1482- boardMenu .add (new JSeparator ());
1483- first = false ;
1484-
14851481 // Add a title for each platform
14861482 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 );
14921489
14931490 // Cycle through all boards of this platform
14941491 for (TargetBoard board : targetPlatform .getBoards ().values ()) {
@@ -1497,14 +1494,40 @@ public void actionPerformed(ActionEvent actionevent) {
14971494 JMenuItem item = createBoardMenusAndCustomMenus (boardsCustomMenus , menuItemsToClickAfterStartup ,
14981495 buttonGroupsMap ,
14991496 board , targetPlatform , targetPackage );
1500- boardMenu .add (item );
1497+ platformBoardsMenu .add (item );
15011498 boardsButtonGroup .add (item );
15021499 }
15031500 }
15041501 }
15051502
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.
15061529 if (menuItemsToClickAfterStartup .isEmpty ()) {
1507- menuItemsToClickAfterStartup .add (selectFirstEnabledMenuItem ( boardMenu ) );
1530+ menuItemsToClickAfterStartup .add (firstBoardItem );
15081531 }
15091532
15101533 for (JMenuItem menuItemToClick : menuItemsToClickAfterStartup ) {
@@ -1658,16 +1681,6 @@ private static JMenuItem selectVisibleSelectedOrFirstMenuItem(JMenu menu) {
16581681 throw new IllegalStateException ("Menu has no enabled items" );
16591682 }
16601683
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-
16711684 public void rebuildProgrammerMenu () {
16721685 programmerMenus = new LinkedList <>();
16731686
0 commit comments