From 7bcd76332ece503c481ce678b91869ce8ae07c33 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 26 Jan 2020 15:59:43 +0100 Subject: [PATCH] Separate the boards menu per platform Previously, the Tools->Boards menu was one long list, divided into different platforms by (unselectable) headers. When more than one or two platforms were installed, this quickly results in a very long list of boards that is hard to navigate. This commit changes the board menu to have a submenu for each platform, where each submenu contains just the boards for that platform. Note that this first keeps a list of board items and then adds those to the boards menu later. This could have been done directly, but the intermediate list makes it easier to special-case single platform installations, as well as sort the list in subsequent commits. This fixes part of #8858. --- app/src/processing/app/Base.java | 46 ++++++++++++++++---------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index e9ddc9e42..3d27f3586 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1481,24 +1481,21 @@ public class Base { ButtonGroup boardsButtonGroup = new ButtonGroup(); Map buttonGroupsMap = new HashMap<>(); + List platformMenus = new ArrayList(); + // Cycle through all packages - boolean first = true; for (TargetPackage targetPackage : BaseNoGui.packages.values()) { // For every package cycle through all platform for (TargetPlatform targetPlatform : targetPackage.platforms()) { - // Add a separator from the previous platform - if (!first) - boardMenu.add(new JSeparator()); - first = false; - // Add a title for each platform String platformLabel = targetPlatform.getPreferences().get("name"); - if (platformLabel != null && !targetPlatform.getBoards().isEmpty()) { - JMenuItem menuLabel = new JMenuItem(tr(platformLabel)); - menuLabel.setEnabled(false); - boardMenu.add(menuLabel); - } + if (platformLabel == null) + platformLabel = targetPackage.getId() + "-" + targetPlatform.getId(); + + JMenu platformBoardsMenu = new JMenu(tr(platformLabel)); + MenuScroller.setScrollerFor(platformBoardsMenu); + platformMenus.add(platformBoardsMenu); // Cycle through all boards of this platform for (TargetBoard board : targetPlatform.getBoards().values()) { @@ -1507,14 +1504,27 @@ public class Base { JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, menuItemsToClickAfterStartup, buttonGroupsMap, board, targetPlatform, targetPackage); - boardMenu.add(item); + platformBoardsMenu.add(item); boardsButtonGroup.add(item); } } } + JMenuItem firstBoardItem = null; + for (JMenu platformMenu : platformMenus) { + if (firstBoardItem == null && platformMenu.getItemCount() > 0) + firstBoardItem = platformMenu.getItem(0); + boardMenu.add(platformMenu); + } + + if (firstBoardItem == null) { + throw new IllegalStateException("No available boards"); + } + + // If there is no current board yet (first startup, or selected + // board no longer defined), select first available board. if (menuItemsToClickAfterStartup.isEmpty()) { - menuItemsToClickAfterStartup.add(selectFirstEnabledMenuItem(boardMenu)); + menuItemsToClickAfterStartup.add(firstBoardItem); } for (JMenuItem menuItemToClick : menuItemsToClickAfterStartup) { @@ -1669,16 +1679,6 @@ public class Base { throw new IllegalStateException("Menu has no enabled items"); } - private static JMenuItem selectFirstEnabledMenuItem(JMenu menu) { - for (int i = 1; i < menu.getItemCount(); i++) { - JMenuItem item = menu.getItem(i); - if (item != null && item.isEnabled()) { - return item; - } - } - throw new IllegalStateException("Menu has no enabled items"); - } - public void rebuildProgrammerMenu() { programmerMenus = new LinkedList<>(); ButtonGroup group = new ButtonGroup();