Skip to content

Commit eed4a43

Browse files
committed
Refactored board specific options into TargetBoard
1 parent 3c01c5f commit eed4a43

File tree

3 files changed

+77
-29
lines changed

3 files changed

+77
-29
lines changed

app/src/processing/app/Base.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,6 @@ public void rebuildBoardsMenu(JMenu toolsMenu, final Editor editor) {
12281228
// For every package cycle through all platform
12291229
for (TargetPlatform targetPlatform : targetPackage.platforms()) {
12301230
String platformName = targetPlatform.getName();
1231-
PreferencesMap customMenus = targetPlatform.getCustomMenus();
12321231

12331232
if (targetPlatform.getPreferences().get("name") == null || targetPlatform.getBoards().isEmpty()) {
12341233
continue;
@@ -1265,14 +1264,17 @@ public void actionPerformed(ActionEvent actionevent) {
12651264
menuItemsToClickAfterStartup.add(item);
12661265
}
12671266

1267+
PreferencesMap customMenus = targetPlatform.getCustomMenus();
12681268
int i = 0;
1269-
for (final String customMenuId : customMenus.topLevelKeySet()) {
1270-
String title = customMenus.get(customMenuId);
1269+
for (final String menuId : customMenus.keySet()) {
1270+
String title = customMenus.get(menuId);
12711271
JMenu menu = makeOrGetBoardMenu(toolsMenu, _(title));
12721272

1273-
Map<String, PreferencesMap> customMenu = customMenus.subTree(customMenuId).firstLevelMap();
1274-
if (customMenu.containsKey(boardId)) {
1275-
PreferencesMap boardCustomMenu = customMenu.get(boardId);
1273+
//Map<String, PreferencesMap> customMenu = customMenus.subTree(menuId).firstLevelMap();
1274+
if (board.hasMenuOptions(menuId)) {
1275+
//if (customMenu.containsKey(boardId)) {
1276+
//PreferencesMap boardCustomMenu = customMenu.get(boardId);
1277+
PreferencesMap boardCustomMenu = board.getMenuOptions(menuId);
12761278
final int currentIndex = i + 1 + 1; //plus 1 to skip the first board menu, plus 1 to keep the custom menu next to this one
12771279
i++;
12781280
for (String customMenuOption : boardCustomMenu.topLevelKeySet()) {
@@ -1282,7 +1284,7 @@ public void actionPerformed(ActionEvent e) {
12821284
Preferences.set("target_package", (String) getValue("package"));
12831285
Preferences.set("target_platform", (String) getValue("platform"));
12841286
Preferences.set("board", (String) getValue("board"));
1285-
Preferences.set("custom_" + customMenuId, (String) getValue("board") + "_" + (String) getValue("custom_menu_option"));
1287+
Preferences.set("custom_" + menuId, (String) getValue("board") + "_" + (String) getValue("custom_menu_option"));
12861288

12871289
filterVisibilityOfSubsequentBoardMenus((String) getValue("board"), currentIndex);
12881290

@@ -1297,15 +1299,15 @@ public void actionPerformed(ActionEvent e) {
12971299
subAction.putValue("package", packageName);
12981300
subAction.putValue("platform", platformName);
12991301

1300-
if (!buttonGroupsMap.containsKey(customMenuId)) {
1301-
buttonGroupsMap.put(customMenuId, new ButtonGroup());
1302+
if (!buttonGroupsMap.containsKey(menuId)) {
1303+
buttonGroupsMap.put(menuId, new ButtonGroup());
13021304
}
13031305

13041306
item = new JRadioButtonMenuItem(subAction);
13051307
menu.add(item);
1306-
buttonGroupsMap.get(customMenuId).add(item);
1308+
buttonGroupsMap.get(menuId).add(item);
13071309

1308-
String selectedCustomMenuEntry = Preferences.get("custom_" + customMenuId);
1310+
String selectedCustomMenuEntry = Preferences.get("custom_" + menuId);
13091311
if (selBoard.equals(boardId) && (boardId + "_" + customMenuOption).equals(selectedCustomMenuEntry)) {
13101312
menuItemsToClickAfterStartup.add(item);
13111313
}
@@ -1932,16 +1934,21 @@ static public TargetPlatform getCurrentTargetPlatformFromPackage(String pack) {
19321934

19331935
static public PreferencesMap getBoardPreferences() {
19341936
TargetPlatform target = getTargetPlatform();
1935-
String board = Preferences.get("board");
1936-
PreferencesMap boardPreferences = new PreferencesMap(target.getBoard(board).getPreferences());
1937+
String boardId = Preferences.get("board");
1938+
TargetBoard board = target.getBoard(boardId);
1939+
PreferencesMap boardPreferences = new PreferencesMap(board.getPreferences());
19371940
PreferencesMap customMenus = target.getCustomMenus();
1938-
for (String customMenuID : customMenus.topLevelKeySet()) {
1939-
PreferencesMap boardCustomMenu = customMenus.subTree(customMenuID).subTree(board);
1940-
String selectedCustomMenuEntry = Preferences.get("custom_" + customMenuID);
1941-
if (boardCustomMenu.size() > 0 && selectedCustomMenuEntry != null && selectedCustomMenuEntry.startsWith(board)) {
1942-
String menuEntryId = selectedCustomMenuEntry.substring(selectedCustomMenuEntry.indexOf("_") + 1);
1941+
for (String menuId : customMenus.keySet()) {
1942+
PreferencesMap boardCustomMenu = board.getMenuOptions(menuId);
1943+
String selectedCustomMenuEntry = Preferences.get("custom_" + menuId);
1944+
if (boardCustomMenu != null && boardCustomMenu.size() > 0 &&
1945+
selectedCustomMenuEntry != null &&
1946+
selectedCustomMenuEntry.startsWith(boardId)) {
1947+
String menuEntryId = selectedCustomMenuEntry
1948+
.substring(selectedCustomMenuEntry.indexOf("_") + 1);
19431949
boardPreferences.putAll(boardCustomMenu.subTree(menuEntryId));
1944-
boardPreferences.put("name", boardPreferences.get("name") + ", " + boardCustomMenu.get(menuEntryId));
1950+
boardPreferences.put("name", boardPreferences.get("name") + ", " +
1951+
boardCustomMenu.get(menuEntryId));
19451952
}
19461953
}
19471954
return boardPreferences;

app/src/processing/app/debug/TargetBoard.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package processing.app.debug;
22

3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
36
import processing.app.helpers.PreferencesMap;
47

58
public class TargetBoard {
69

7-
String id;
8-
PreferencesMap prefs;
10+
private String id;
11+
private PreferencesMap prefs;
12+
private Map<String, PreferencesMap> menuOptions = new LinkedHashMap<String, PreferencesMap>();
913

1014
/**
1115
* Create a TargetBoard based on preferences passed as argument.
@@ -44,4 +48,16 @@ public String getId() {
4448
public PreferencesMap getPreferences() {
4549
return prefs;
4650
}
51+
52+
public void setMenuOptions(String menuId, PreferencesMap _menuOptions) {
53+
menuOptions.put(menuId, _menuOptions);
54+
}
55+
56+
public PreferencesMap getMenuOptions(String menuId) {
57+
return menuOptions.get(menuId);
58+
}
59+
60+
public boolean hasMenuOptions(String menuId) {
61+
return menuOptions.containsKey(menuId);
62+
}
4763
}

app/src/processing/app/debug/TargetPlatform.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public class TargetPlatform {
5353
*/
5454
private PreferencesMap preferences = new PreferencesMap();
5555

56+
/**
57+
* Contains labels for top level menus
58+
*/
5659
private PreferencesMap customMenus = new PreferencesMap();
5760

5861
public TargetPlatform(String _name, File _folder)
@@ -69,19 +72,41 @@ public TargetPlatform(String _name, File _folder)
6972

7073
// Load boards
7174
try {
72-
PreferencesMap p = new PreferencesMap(boardsFile);
73-
Map<String, PreferencesMap> boardsPreferences = p.firstLevelMap();
74-
75-
// Create custom menus using the key "menu" and its subkeys
76-
if (boardsPreferences.containsKey("menu")) {
77-
customMenus = new PreferencesMap(boardsPreferences.get("menu"));
78-
boardsPreferences.remove("menu");
75+
Map<String, PreferencesMap> boardsPreferences = new PreferencesMap(
76+
boardsFile).firstLevelMap();
77+
78+
// Create custom menus for this platform
79+
PreferencesMap menus = boardsPreferences.get("menu");
80+
boardsPreferences.remove("menu");
81+
if (menus != null)
82+
customMenus = menus.topLevelMap();
83+
84+
// Create maps for every menu option:
85+
// - a Map that pairs a specific menu option (e.g. "cpu") to
86+
// - a Map that pairs a specific board (e.g. "duemilanove") to
87+
// - a PrefenceMap with all the options that overrides default
88+
// configuration values
89+
Map<String, Map<String, PreferencesMap>> subMenus = new LinkedHashMap<String, Map<String, PreferencesMap>>();
90+
for (String id : customMenus.keySet()) {
91+
subMenus.put(id, menus.subTree(id).firstLevelMap());
7992
}
8093

8194
// Create boards
8295
for (String id : boardsPreferences.keySet()) {
8396
PreferencesMap preferences = boardsPreferences.get(id);
84-
boards.put(id, new TargetBoard(id, preferences));
97+
TargetBoard board = new TargetBoard(id, preferences);
98+
99+
if (menus != null) {
100+
// Build custom menu for the specified board
101+
PreferencesMap boardCustomMenu = new PreferencesMap();
102+
for (String menuId : customMenus.keySet()) {
103+
// Check if the board has option for this custom menu
104+
if (subMenus.get(menuId).containsKey(id))
105+
// Add specific custom menu to the board
106+
board.setMenuOptions(menuId, subMenus.get(menuId).get(id));
107+
}
108+
}
109+
boards.put(id, board);
85110
}
86111
} catch (IOException e) {
87112
throw new TargetPlatformException(format(_("Error loading {0}"),

0 commit comments

Comments
 (0)