From eed4a43b547ad04f2c55a9a3a987b75233e6d0cd Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sun, 3 Mar 2013 11:15:57 +0100 Subject: [PATCH] Refactored board specific options into TargetBoard --- app/src/processing/app/Base.java | 45 +++++++++++-------- app/src/processing/app/debug/TargetBoard.java | 20 ++++++++- .../processing/app/debug/TargetPlatform.java | 39 +++++++++++++--- 3 files changed, 76 insertions(+), 28 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 3057a1da9..c1f378db0 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1228,7 +1228,6 @@ public class Base { // For every package cycle through all platform for (TargetPlatform targetPlatform : targetPackage.platforms()) { String platformName = targetPlatform.getName(); - PreferencesMap customMenus = targetPlatform.getCustomMenus(); if (targetPlatform.getPreferences().get("name") == null || targetPlatform.getBoards().isEmpty()) { continue; @@ -1265,14 +1264,17 @@ public class Base { menuItemsToClickAfterStartup.add(item); } + PreferencesMap customMenus = targetPlatform.getCustomMenus(); int i = 0; - for (final String customMenuId : customMenus.topLevelKeySet()) { - String title = customMenus.get(customMenuId); + for (final String menuId : customMenus.keySet()) { + String title = customMenus.get(menuId); JMenu menu = makeOrGetBoardMenu(toolsMenu, _(title)); - Map customMenu = customMenus.subTree(customMenuId).firstLevelMap(); - if (customMenu.containsKey(boardId)) { - PreferencesMap boardCustomMenu = customMenu.get(boardId); + //Map customMenu = customMenus.subTree(menuId).firstLevelMap(); + if (board.hasMenuOptions(menuId)) { + //if (customMenu.containsKey(boardId)) { + //PreferencesMap boardCustomMenu = customMenu.get(boardId); + PreferencesMap boardCustomMenu = board.getMenuOptions(menuId); 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 i++; for (String customMenuOption : boardCustomMenu.topLevelKeySet()) { @@ -1282,7 +1284,7 @@ public class Base { Preferences.set("target_package", (String) getValue("package")); Preferences.set("target_platform", (String) getValue("platform")); Preferences.set("board", (String) getValue("board")); - Preferences.set("custom_" + customMenuId, (String) getValue("board") + "_" + (String) getValue("custom_menu_option")); + Preferences.set("custom_" + menuId, (String) getValue("board") + "_" + (String) getValue("custom_menu_option")); filterVisibilityOfSubsequentBoardMenus((String) getValue("board"), currentIndex); @@ -1297,15 +1299,15 @@ public class Base { subAction.putValue("package", packageName); subAction.putValue("platform", platformName); - if (!buttonGroupsMap.containsKey(customMenuId)) { - buttonGroupsMap.put(customMenuId, new ButtonGroup()); + if (!buttonGroupsMap.containsKey(menuId)) { + buttonGroupsMap.put(menuId, new ButtonGroup()); } item = new JRadioButtonMenuItem(subAction); menu.add(item); - buttonGroupsMap.get(customMenuId).add(item); + buttonGroupsMap.get(menuId).add(item); - String selectedCustomMenuEntry = Preferences.get("custom_" + customMenuId); + String selectedCustomMenuEntry = Preferences.get("custom_" + menuId); if (selBoard.equals(boardId) && (boardId + "_" + customMenuOption).equals(selectedCustomMenuEntry)) { menuItemsToClickAfterStartup.add(item); } @@ -1932,16 +1934,21 @@ public class Base { static public PreferencesMap getBoardPreferences() { TargetPlatform target = getTargetPlatform(); - String board = Preferences.get("board"); - PreferencesMap boardPreferences = new PreferencesMap(target.getBoard(board).getPreferences()); + String boardId = Preferences.get("board"); + TargetBoard board = target.getBoard(boardId); + PreferencesMap boardPreferences = new PreferencesMap(board.getPreferences()); PreferencesMap customMenus = target.getCustomMenus(); - for (String customMenuID : customMenus.topLevelKeySet()) { - PreferencesMap boardCustomMenu = customMenus.subTree(customMenuID).subTree(board); - String selectedCustomMenuEntry = Preferences.get("custom_" + customMenuID); - if (boardCustomMenu.size() > 0 && selectedCustomMenuEntry != null && selectedCustomMenuEntry.startsWith(board)) { - String menuEntryId = selectedCustomMenuEntry.substring(selectedCustomMenuEntry.indexOf("_") + 1); + for (String menuId : customMenus.keySet()) { + PreferencesMap boardCustomMenu = board.getMenuOptions(menuId); + String selectedCustomMenuEntry = Preferences.get("custom_" + menuId); + if (boardCustomMenu != null && boardCustomMenu.size() > 0 && + selectedCustomMenuEntry != null && + selectedCustomMenuEntry.startsWith(boardId)) { + String menuEntryId = selectedCustomMenuEntry + .substring(selectedCustomMenuEntry.indexOf("_") + 1); boardPreferences.putAll(boardCustomMenu.subTree(menuEntryId)); - boardPreferences.put("name", boardPreferences.get("name") + ", " + boardCustomMenu.get(menuEntryId)); + boardPreferences.put("name", boardPreferences.get("name") + ", " + + boardCustomMenu.get(menuEntryId)); } } return boardPreferences; diff --git a/app/src/processing/app/debug/TargetBoard.java b/app/src/processing/app/debug/TargetBoard.java index bb489c13e..2ee0ec71a 100644 --- a/app/src/processing/app/debug/TargetBoard.java +++ b/app/src/processing/app/debug/TargetBoard.java @@ -1,11 +1,15 @@ package processing.app.debug; +import java.util.LinkedHashMap; +import java.util.Map; + import processing.app.helpers.PreferencesMap; public class TargetBoard { - String id; - PreferencesMap prefs; + private String id; + private PreferencesMap prefs; + private Map menuOptions = new LinkedHashMap(); /** * Create a TargetBoard based on preferences passed as argument. @@ -44,4 +48,16 @@ public class TargetBoard { public PreferencesMap getPreferences() { return prefs; } + + public void setMenuOptions(String menuId, PreferencesMap _menuOptions) { + menuOptions.put(menuId, _menuOptions); + } + + public PreferencesMap getMenuOptions(String menuId) { + return menuOptions.get(menuId); + } + + public boolean hasMenuOptions(String menuId) { + return menuOptions.containsKey(menuId); + } } diff --git a/app/src/processing/app/debug/TargetPlatform.java b/app/src/processing/app/debug/TargetPlatform.java index e9dd6bb58..297f8c908 100644 --- a/app/src/processing/app/debug/TargetPlatform.java +++ b/app/src/processing/app/debug/TargetPlatform.java @@ -53,6 +53,9 @@ public class TargetPlatform { */ private PreferencesMap preferences = new PreferencesMap(); + /** + * Contains labels for top level menus + */ private PreferencesMap customMenus = new PreferencesMap(); public TargetPlatform(String _name, File _folder) @@ -69,19 +72,41 @@ public class TargetPlatform { // Load boards try { - PreferencesMap p = new PreferencesMap(boardsFile); - Map boardsPreferences = p.firstLevelMap(); + Map boardsPreferences = new PreferencesMap( + boardsFile).firstLevelMap(); - // Create custom menus using the key "menu" and its subkeys - if (boardsPreferences.containsKey("menu")) { - customMenus = new PreferencesMap(boardsPreferences.get("menu")); - boardsPreferences.remove("menu"); + // Create custom menus for this platform + PreferencesMap menus = boardsPreferences.get("menu"); + boardsPreferences.remove("menu"); + if (menus != null) + customMenus = menus.topLevelMap(); + + // Create maps for every menu option: + // - a Map that pairs a specific menu option (e.g. "cpu") to + // - a Map that pairs a specific board (e.g. "duemilanove") to + // - a PrefenceMap with all the options that overrides default + // configuration values + Map> subMenus = new LinkedHashMap>(); + for (String id : customMenus.keySet()) { + subMenus.put(id, menus.subTree(id).firstLevelMap()); } // Create boards for (String id : boardsPreferences.keySet()) { PreferencesMap preferences = boardsPreferences.get(id); - boards.put(id, new TargetBoard(id, preferences)); + TargetBoard board = new TargetBoard(id, preferences); + + if (menus != null) { + // Build custom menu for the specified board + PreferencesMap boardCustomMenu = new PreferencesMap(); + for (String menuId : customMenus.keySet()) { + // Check if the board has option for this custom menu + if (subMenus.get(menuId).containsKey(id)) + // Add specific custom menu to the board + board.setMenuOptions(menuId, subMenus.get(menuId).get(id)); + } + } + boards.put(id, board); } } catch (IOException e) { throw new TargetPlatformException(format(_("Error loading {0}"),