From 7c4205b4120641b5d92c8d827621b3a9de8c373d Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 19 Sep 2019 19:31:58 +0200 Subject: [PATCH] Handle nested submenus of the Tools menu There is some code that, for each submenu under Tools, shows the selected item in the label of the submenu itself (e.g. before opening the submenu). This was done whenever the Tools menu is opened and iterated over all the items in the submenu to identify the s Previously, this code only looked at direct children of the submenu. Now, this code also looks through submenus recursively, to keep the code working even when items are divided over sub-submenus. This makes a small behaviour change: previously, the first selected item with a non-zero label was used. Now, the first selected item is used, which makes the code a bit cleaner. I cannot quickly see any case where the first selected item would have an empty text (and even more that there is *another* selected item), so this check seems unnecessary. If this case would occur nonetheless, it would only mean the selected item is not displayed in the tools menu, nothing would otherwise break. --- app/src/processing/app/Editor.java | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index f0c7c1924..2ec29c498 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -761,6 +761,20 @@ public class Editor extends JFrame implements RunnerListener { toolsMenu.add(item); toolsMenu.addMenuListener(new StubMenuListener() { + public JMenuItem getSelectedItemRecursive(JMenu menu) { + int count = menu.getItemCount(); + for (int i=0; i < count; i++) { + JMenuItem item = menu.getItem(i); + + if ((item instanceof JMenu)) + item = getSelectedItemRecursive((JMenu)item); + + if (item != null && item.isSelected()) + return item; + } + return null; + } + public void menuSelected(MenuEvent e) { //System.out.println("Tools menu selected."); populatePortMenu(); @@ -772,15 +786,9 @@ public class Editor extends JFrame implements RunnerListener { String basename = name; int index = name.indexOf(':'); if (index > 0) basename = name.substring(0, index); - String sel = null; - int count = menu.getItemCount(); - for (int i=0; i < count; i++) { - JMenuItem item = menu.getItem(i); - if (item != null && item.isSelected()) { - sel = item.getText(); - if (sel != null) break; - } - } + + JMenuItem item = getSelectedItemRecursive(menu); + String sel = item != null ? item.getText() : null; if (sel == null) { if (!name.equals(basename)) menu.setText(basename); } else {