Skip to content

Commit 46c930c

Browse files
Pass TargetBoard objects around instead of strings
Previously, strings containing the board id, or a joined version of the package, platform and board id were passed around. Since comparing objects is easier than strings and since parsing strings can be fragile, it's better to just pass the TargetBoard objects. There is one case where string parsing is still required: when parsing the --board commandline option. However, the parsing is now done in the right place, when handling the commandline, instead of in a generic selectBoard method.
1 parent 89fa1c4 commit 46c930c

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

app/src/processing/app/Base.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,20 @@ public Base(String[] args) throws Exception {
395395
Thread.sleep(10);
396396

397397
// Do board selection if requested
398-
if (selectBoard != null)
399-
selectBoard(selectBoard);
398+
if (selectBoard != null) {
399+
String[] split = selectBoard.split(":");
400+
401+
TargetBoard targetBoard = getTargetPlatform(split[0], split[1]).getBoard(split[2]);
402+
selectBoard(targetBoard);
403+
404+
if (split.length > 3) {
405+
String[] customsParts = split[3].split(",");
406+
for (String customParts : customsParts) {
407+
String[] keyValue = customParts.split("=");
408+
Preferences.set("custom_" + keyValue[0].trim(), targetBoard.getId() + "_" + keyValue[1].trim());
409+
}
410+
}
411+
}
400412

401413
if (doUpload) {
402414
// Build and upload
@@ -1347,10 +1359,10 @@ private JRadioButtonMenuItem createBoardMenusAndCustomMenus(
13471359
@SuppressWarnings("serial")
13481360
Action action = new AbstractAction(board.getName()) {
13491361
public void actionPerformed(ActionEvent actionevent) {
1350-
selectBoard((String) getValue("b"));
1362+
selectBoard((TargetBoard)getValue("b"));
13511363
}
13521364
};
1353-
action.putValue("b", packageName + ":" + platformName + ":" + boardId);
1365+
action.putValue("b", board);
13541366

13551367
JRadioButtonMenuItem item = new JRadioButtonMenuItem(action);
13561368

@@ -1373,11 +1385,11 @@ public void actionPerformed(ActionEvent actionevent) {
13731385
@SuppressWarnings("serial")
13741386
Action subAction = new AbstractAction(_(boardCustomMenu.get(customMenuOption))) {
13751387
public void actionPerformed(ActionEvent e) {
1376-
Preferences.set("custom_" + menuId, getValue("board") + "_" + getValue("custom_menu_option"));
1388+
Preferences.set("custom_" + menuId, ((TargetBoard)getValue("board")).getId() + "_" + getValue("custom_menu_option"));
13771389
Sketch.buildSettingChanged();
13781390
}
13791391
};
1380-
subAction.putValue("board", boardId);
1392+
subAction.putValue("board", board);
13811393
subAction.putValue("custom_menu_option", customMenuOption);
13821394

13831395
if (!buttonGroupsMap.containsKey(menuId)) {
@@ -1399,12 +1411,12 @@ public void actionPerformed(ActionEvent e) {
13991411
return item;
14001412
}
14011413

1402-
private static void filterVisibilityOfSubsequentBoardMenus(String boardID, int fromIndex) {
1414+
private static void filterVisibilityOfSubsequentBoardMenus(TargetBoard board, int fromIndex) {
14031415
for (int i = fromIndex; i < Editor.boardsMenus.size(); i++) {
14041416
JMenu menu = Editor.boardsMenus.get(i);
14051417
for (int m = 0; m < menu.getItemCount(); m++) {
14061418
JMenuItem menuItem = menu.getItem(m);
1407-
menuItem.setVisible(menuItem.getAction().getValue("board").equals(boardID));
1419+
menuItem.setVisible(menuItem.getAction().getValue("board").equals(board));
14081420
}
14091421
menu.setVisible(ifThereAreVisibleItemsOn(menu));
14101422

@@ -1477,22 +1489,15 @@ private static JMenuItem selectFirstEnabledMenuItem(JMenu menu) {
14771489
}
14781490

14791491

1480-
private void selectBoard(String selectBoard) {
1481-
String[] split = selectBoard.split(":");
1482-
Preferences.set("target_package", split[0]);
1483-
Preferences.set("target_platform", split[1]);
1484-
String boardId = split[2];
1485-
Preferences.set("board", boardId);
1492+
private void selectBoard(TargetBoard targetBoard) {
1493+
TargetPlatform targetPlatform = targetBoard.getContainerPlatform();
1494+
TargetPackage targetPackage = targetPlatform.getContainerPackage();
14861495

1487-
if (split.length > 3) {
1488-
String[] customsParts = split[3].split(",");
1489-
for (String customParts : customsParts) {
1490-
String[] keyValue = customParts.split("=");
1491-
Preferences.set("custom_" + keyValue[0].trim(), boardId + "_" + keyValue[1].trim());
1492-
}
1493-
}
1496+
Preferences.set("target_package", targetPackage.getId());
1497+
Preferences.set("target_platform", targetPlatform.getId());
1498+
Preferences.set("board", targetBoard.getId());
14941499

1495-
filterVisibilityOfSubsequentBoardMenus(boardId, 1);
1500+
filterVisibilityOfSubsequentBoardMenus(targetBoard, 1);
14961501

14971502
onBoardOrPortChange();
14981503
Sketch.buildSettingChanged();

0 commit comments

Comments
 (0)