Skip to content

Commit e5551bf

Browse files
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.
1 parent 6116a8e commit e5551bf

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Diff for: app/src/processing/app/Editor.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,20 @@ private JMenu buildToolsMenu() {
761761
toolsMenu.add(item);
762762

763763
toolsMenu.addMenuListener(new StubMenuListener() {
764+
public JMenuItem getSelectedItemRecursive(JMenu menu) {
765+
int count = menu.getItemCount();
766+
for (int i=0; i < count; i++) {
767+
JMenuItem item = menu.getItem(i);
768+
769+
if ((item instanceof JMenu))
770+
item = getSelectedItemRecursive((JMenu)item);
771+
772+
if (item != null && item.isSelected())
773+
return item;
774+
}
775+
return null;
776+
}
777+
764778
public void menuSelected(MenuEvent e) {
765779
//System.out.println("Tools menu selected.");
766780
populatePortMenu();
@@ -772,15 +786,9 @@ public void menuSelected(MenuEvent e) {
772786
String basename = name;
773787
int index = name.indexOf(':');
774788
if (index > 0) basename = name.substring(0, index);
775-
String sel = null;
776-
int count = menu.getItemCount();
777-
for (int i=0; i < count; i++) {
778-
JMenuItem item = menu.getItem(i);
779-
if (item != null && item.isSelected()) {
780-
sel = item.getText();
781-
if (sel != null) break;
782-
}
783-
}
789+
790+
JMenuItem item = getSelectedItemRecursive(menu);
791+
String sel = item != null ? item.getText() : null;
784792
if (sel == null) {
785793
if (!name.equals(basename)) menu.setText(basename);
786794
} else {

0 commit comments

Comments
 (0)