Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divide board menu into submenus #9238

Merged
merged 6 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -999,9 +999,9 @@ public boolean handleQuit() {
// kill uploader (if still alive)
UploaderUtils uploaderInstance = new UploaderUtils();
Uploader uploader = uploaderInstance.getUploaderByPreferences(false);
if (uploader != null && uploader.programmerPid != null && uploader.programmerPid.isAlive()) {
if (uploader != null && Uploader.programmerPid != null && Uploader.programmerPid.isAlive()) {
// kill the stuck programmer
uploader.programmerPid.destroyForcibly();
Uploader.programmerPid.destroyForcibly();
}

if (handleQuitEach()) {
Expand Down Expand Up @@ -1444,8 +1444,9 @@ public void actionPerformed(ActionEvent actionevent) {
String filterText = "";
String dropdownItem = "";
if (actionevent instanceof Event) {
filterText = ((Event) actionevent).getPayload().get("filterText").toString();
dropdownItem = ((Event) actionevent).getPayload().get("dropdownItem").toString();
Event e = ((Event) actionevent);
filterText = e.getPayload().get("filterText").toString();
dropdownItem = e.getPayload().get("dropdownItem").toString();
}
try {
openBoardsManager(filterText, dropdownItem);
Expand Down Expand Up @@ -1481,24 +1482,21 @@ public void actionPerformed(ActionEvent actionevent) {
ButtonGroup boardsButtonGroup = new ButtonGroup();
Map<String, ButtonGroup> buttonGroupsMap = new HashMap<>();

List<JMenu> 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(platformLabel);
MenuScroller.setScrollerFor(platformBoardsMenu);
platformMenus.add(platformBoardsMenu);

// Cycle through all boards of this platform
for (TargetBoard board : targetPlatform.getBoards().values()) {
Expand All @@ -1507,14 +1505,40 @@ public void actionPerformed(ActionEvent actionevent) {
JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, menuItemsToClickAfterStartup,
buttonGroupsMap,
board, targetPlatform, targetPackage);
boardMenu.add(item);
platformBoardsMenu.add(item);
boardsButtonGroup.add(item);
}
}
}

platformMenus.sort((x,y) -> x.getText().compareToIgnoreCase(y.getText()));

JMenuItem firstBoardItem = null;
if (platformMenus.size() == 1) {
// When just one platform exists, add the board items directly,
// rather than using a submenu
for (Component boardItem : platformMenus.get(0).getMenuComponents()) {
boardMenu.add(boardItem);
if (firstBoardItem == null)
firstBoardItem = (JMenuItem)boardItem;
}
} else {
// For multiple platforms, use submenus
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) {
Expand Down Expand Up @@ -1579,7 +1603,7 @@ public void actionPerformed(ActionEvent e) {
};
List<TargetBoard> boards = (List<TargetBoard>) subAction.getValue("board");
if (boards == null) {
boards = new ArrayList<TargetBoard>();
boards = new ArrayList<>();
}
boards.add(board);
subAction.putValue("board", boards);
Expand Down Expand Up @@ -1669,16 +1693,6 @@ private static JMenuItem selectVisibleSelectedOrFirstMenuItem(JMenu menu) {
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();
Expand Down Expand Up @@ -1990,6 +2004,7 @@ public void keyPressed(KeyEvent e) {
Base.this.handleFontSizeChange(-1);
}
break;
default:
}
}
}
Expand Down
26 changes: 17 additions & 9 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,20 @@ private JMenu buildToolsMenu() {
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();
Expand All @@ -772,15 +786,9 @@ public void menuSelected(MenuEvent e) {
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 {
Expand Down
4 changes: 0 additions & 4 deletions arduino-core/src/processing/app/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ public static String format(String fmt, Object... args) {
* This method is an hack to extract words with gettext tool.
*/
protected static void unusedStrings() {
// These phrases are defined in the "platform.txt".
tr("Arduino AVR Boards");
tr("Arduino ARM (32-bits) Boards");

// This word is defined in the "boards.txt".
tr("Processor");
}
Expand Down