Skip to content

Commit 3c01c5f

Browse files
committed
A lot of refactoring on Preferences, custom menus and Boards:
- Merged MapWithSubkeys into PreferencesMap. - Added TargetBoard class. - Simplified a bit submenu generation.
1 parent 3731134 commit 3c01c5f

File tree

9 files changed

+368
-262
lines changed

9 files changed

+368
-262
lines changed

app/src/processing/app/Base.java

Lines changed: 72 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030

3131
import javax.swing.*;
3232

33+
import processing.app.debug.TargetBoard;
3334
import processing.app.debug.TargetPackage;
3435
import processing.app.debug.TargetPlatform;
36+
import processing.app.debug.TargetPlatformException;
3537
import processing.app.helpers.FileUtils;
36-
import processing.app.helpers.Maps;
3738
import processing.app.helpers.PreferencesMap;
3839
import processing.app.helpers.filefilters.OnlyDirs;
3940
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
4041
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;import processing.app.packages.Library;
4142
import processing.app.packages.LibraryList;
42-
import processing.app.tools.MapWithSubkeys;
4343
import processing.app.tools.ZipDeflater;
4444
import processing.core.*;
4545
import static processing.app.I18n._;
@@ -116,7 +116,6 @@ public class Base {
116116
static File portableFolder = null;
117117
static final String portableSketchbookFolder = "sketchbook";
118118

119-
120119
static public void main(String args[]) throws Exception {
121120
initPlatform();
122121

@@ -290,6 +289,11 @@ public Base(String[] args) throws Exception {
290289
packages = new HashMap<String, TargetPackage>();
291290
loadHardware(getHardwareFolder());
292291
loadHardware(getSketchbookHardwareFolder());
292+
if (packages.size() == 0) {
293+
System.out.println(_("No valid configured cores found! Exiting..."));
294+
System.exit(3);
295+
}
296+
293297
// Setup board-dependent variables.
294298
onBoardOrPortChange();
295299

@@ -1180,7 +1184,7 @@ public void onBoardOrPortChange() {
11801184
try {
11811185
libraries = scanLibraries(librariesFolders);
11821186
} catch (IOException e) {
1183-
showWarning(_("Error"), _("Error reading preferences"), e);
1187+
showWarning(_("Error"), _("Error loading libraries"), e);
11841188
}
11851189
String currentArch = Base.getTargetPlatform().getName();
11861190
libraries = libraries.filterByArchitecture(currentArch);
@@ -1224,87 +1228,86 @@ public void rebuildBoardsMenu(JMenu toolsMenu, final Editor editor) {
12241228
// For every package cycle through all platform
12251229
for (TargetPlatform targetPlatform : targetPackage.platforms()) {
12261230
String platformName = targetPlatform.getName();
1227-
Map<String, PreferencesMap> boards = targetPlatform.getBoards();
1231+
PreferencesMap customMenus = targetPlatform.getCustomMenus();
12281232

12291233
if (targetPlatform.getPreferences().get("name") == null || targetPlatform.getBoards().isEmpty()) {
12301234
continue;
12311235
}
12321236

12331237
// Add a title for each group of boards
1234-
if (!first) {
1238+
if (!first)
12351239
boardsMenu.add(new JSeparator());
1236-
}
12371240
first = false;
12381241

12391242
JMenuItem separator = new JMenuItem(_(targetPlatform.getPreferences().get("name")));
12401243
separator.setEnabled(false);
12411244
boardsMenu.add(separator);
12421245

12431246
// For every platform cycle through all boards
1244-
for (final String boardID : targetPlatform.getBoards().keySet()) {
1247+
for (TargetBoard board : targetPlatform.getBoards().values()) {
12451248
// Setup a menu item for the current board
1246-
String boardName = boards.get(boardID).get("name");
1249+
String boardName = board.getName();
1250+
String boardId = board.getId();
12471251
@SuppressWarnings("serial")
12481252
Action action = new AbstractAction(boardName) {
12491253
public void actionPerformed(ActionEvent actionevent) {
12501254
selectBoard((String) getValue("b"), editor);
12511255
}
12521256
};
1253-
action.putValue("b", packageName + ":" + platformName + ":" + boardID);
1257+
action.putValue("b", packageName + ":" + platformName + ":" + boardId);
12541258

12551259
JRadioButtonMenuItem item = new JRadioButtonMenuItem(action);
12561260
boardsMenu.add(item);
12571261
boardsButtonGroup.add(item);
12581262

1259-
if (selBoard.equals(boardID) && selPackage.equals(packageName)
1263+
if (selBoard.equals(boardId) && selPackage.equals(packageName)
12601264
&& selPlatform.equals(platformName)) {
12611265
menuItemsToClickAfterStartup.add(item);
12621266
}
12631267

1264-
if (targetPlatform.getCustomMenus() != null) {
1265-
List<String> customMenuIDs = new LinkedList<String>(targetPlatform.getCustomMenus().getKeys());
1266-
for (int i = 0; i < customMenuIDs.size(); i++) {
1267-
final String customMenuID = customMenuIDs.get(i);
1268-
JMenu menu = makeOrGetBoardMenu(toolsMenu, _(targetPlatform.getCustomMenus().getValueOf(customMenuID)));
1269-
MapWithSubkeys customMenu = targetPlatform.getCustomMenus().get(customMenuID);
1270-
if (customMenu.getKeys().contains(boardID)) {
1271-
MapWithSubkeys boardCustomMenu = customMenu.get(boardID);
1272-
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
1273-
for (final String customMenuOption : boardCustomMenu.getKeys()) {
1274-
@SuppressWarnings("serial")
1275-
Action subAction = new AbstractAction(_(boardCustomMenu.getValueOf(customMenuOption))) {
1276-
1277-
public void actionPerformed(ActionEvent e) {
1278-
Preferences.set("target_package", (String) getValue("package"));
1279-
Preferences.set("target_platform", (String) getValue("platform"));
1280-
Preferences.set("board", (String) getValue("board"));
1281-
Preferences.set("custom_" + customMenuID, boardID + "_" + (String) getValue("custom_menu_option"));
1282-
1283-
filterVisibilityOfSubsequentBoardMenus((String) getValue("board"), currentIndex);
1284-
1285-
onBoardOrPortChange();
1286-
Sketch.buildSettingChanged();
1287-
rebuildImportMenu(Editor.importMenu, editor);
1288-
rebuildExamplesMenu(Editor.examplesMenu);
1289-
}
1290-
};
1291-
subAction.putValue("board", boardID);
1292-
subAction.putValue("custom_menu_option", customMenuOption);
1293-
subAction.putValue("package", packageName);
1294-
subAction.putValue("platform", platformName);
1295-
1296-
if (!buttonGroupsMap.containsKey(customMenuID)) {
1297-
buttonGroupsMap.put(customMenuID, new ButtonGroup());
1268+
int i = 0;
1269+
for (final String customMenuId : customMenus.topLevelKeySet()) {
1270+
String title = customMenus.get(customMenuId);
1271+
JMenu menu = makeOrGetBoardMenu(toolsMenu, _(title));
1272+
1273+
Map<String, PreferencesMap> customMenu = customMenus.subTree(customMenuId).firstLevelMap();
1274+
if (customMenu.containsKey(boardId)) {
1275+
PreferencesMap boardCustomMenu = customMenu.get(boardId);
1276+
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
1277+
i++;
1278+
for (String customMenuOption : boardCustomMenu.topLevelKeySet()) {
1279+
@SuppressWarnings("serial")
1280+
Action subAction = new AbstractAction(_(boardCustomMenu.get(customMenuOption))) {
1281+
public void actionPerformed(ActionEvent e) {
1282+
Preferences.set("target_package", (String) getValue("package"));
1283+
Preferences.set("target_platform", (String) getValue("platform"));
1284+
Preferences.set("board", (String) getValue("board"));
1285+
Preferences.set("custom_" + customMenuId, (String) getValue("board") + "_" + (String) getValue("custom_menu_option"));
1286+
1287+
filterVisibilityOfSubsequentBoardMenus((String) getValue("board"), currentIndex);
1288+
1289+
onBoardOrPortChange();
1290+
Sketch.buildSettingChanged();
1291+
rebuildImportMenu(Editor.importMenu, editor);
1292+
rebuildExamplesMenu(Editor.examplesMenu);
12981293
}
1294+
};
1295+
subAction.putValue("board", boardId);
1296+
subAction.putValue("custom_menu_option", customMenuOption);
1297+
subAction.putValue("package", packageName);
1298+
subAction.putValue("platform", platformName);
1299+
1300+
if (!buttonGroupsMap.containsKey(customMenuId)) {
1301+
buttonGroupsMap.put(customMenuId, new ButtonGroup());
1302+
}
12991303

1300-
item = new JRadioButtonMenuItem(subAction);
1301-
menu.add(item);
1302-
buttonGroupsMap.get(customMenuID).add(item);
1304+
item = new JRadioButtonMenuItem(subAction);
1305+
menu.add(item);
1306+
buttonGroupsMap.get(customMenuId).add(item);
13031307

1304-
String selectedCustomMenuEntry = Preferences.get("custom_" + customMenuID);
1305-
if (selBoard.equals(boardID) && (boardID + "_" + customMenuOption).equals(selectedCustomMenuEntry)) {
1306-
menuItemsToClickAfterStartup.add(item);
1307-
}
1308+
String selectedCustomMenuEntry = Preferences.get("custom_" + customMenuId);
1309+
if (selBoard.equals(boardId) && (boardId + "_" + customMenuOption).equals(selectedCustomMenuEntry)) {
1310+
menuItemsToClickAfterStartup.add(item);
13081311
}
13091312
}
13101313
}
@@ -1604,7 +1607,13 @@ protected void loadHardware(File folder) {
16041607
if (target.equals("tools"))
16051608
continue;
16061609
File subfolder = new File(folder, target);
1607-
packages.put(target, new TargetPackage(target, subfolder));
1610+
1611+
try {
1612+
packages.put(target, new TargetPackage(target, subfolder));
1613+
} catch (TargetPlatformException e) {
1614+
System.out.println("WARNING: Error loading hardware folder " + target);
1615+
System.out.println(" " + e.getMessage());
1616+
}
16081617
}
16091618
}
16101619

@@ -1921,19 +1930,18 @@ static public TargetPlatform getCurrentTargetPlatformFromPackage(String pack) {
19211930
return getTargetPlatform(pack, Preferences.get("target_platform"));
19221931
}
19231932

1924-
static public Map<String, String> getBoardPreferences() {
1933+
static public PreferencesMap getBoardPreferences() {
19251934
TargetPlatform target = getTargetPlatform();
19261935
String board = Preferences.get("board");
1927-
Map<String, String> boardPreferences = Maps.merge(target.getBoards().get(board), new LinkedHashMap<String, String>());
1928-
if (target.getCustomMenus() != null) {
1929-
for (String customMenuID : target.getCustomMenus().getKeys()) {
1930-
MapWithSubkeys boardCustomMenu = target.getCustomMenus().get(customMenuID).get(board);
1931-
String selectedCustomMenuEntry = Preferences.get("custom_" + customMenuID);
1932-
if (boardCustomMenu != null && selectedCustomMenuEntry != null && selectedCustomMenuEntry.startsWith(board)) {
1933-
String menuEntryId = selectedCustomMenuEntry.substring(selectedCustomMenuEntry.indexOf("_") + 1);
1934-
Maps.merge(boardCustomMenu.get(menuEntryId).getValues(), boardPreferences);
1935-
boardPreferences.put("name", boardPreferences.get("name") + ", " + boardCustomMenu.getValueOf(menuEntryId));
1936-
}
1936+
PreferencesMap boardPreferences = new PreferencesMap(target.getBoard(board).getPreferences());
1937+
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);
1943+
boardPreferences.putAll(boardCustomMenu.subTree(menuEntryId));
1944+
boardPreferences.put("name", boardPreferences.get("name") + ", " + boardCustomMenu.get(menuEntryId));
19371945
}
19381946
}
19391947
return boardPreferences;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package processing.app.debug;
2+
3+
import processing.app.helpers.PreferencesMap;
4+
5+
public class TargetBoard {
6+
7+
String id;
8+
PreferencesMap prefs;
9+
10+
/**
11+
* Create a TargetBoard based on preferences passed as argument.
12+
*
13+
* @param _prefs
14+
* @return
15+
*/
16+
public TargetBoard(String _id, PreferencesMap _prefs) {
17+
id = _id;
18+
prefs = new PreferencesMap(_prefs);
19+
}
20+
21+
/**
22+
* Get the name of the board.
23+
*
24+
* @return
25+
*/
26+
public String getName() {
27+
return prefs.get("name");
28+
}
29+
30+
/**
31+
* Get the identifier of the board
32+
*
33+
* @return
34+
*/
35+
public String getId() {
36+
return id;
37+
}
38+
39+
/**
40+
* Get the full preferences map of the board with a given identifier
41+
*
42+
* @return
43+
*/
44+
public PreferencesMap getPreferences() {
45+
return prefs;
46+
}
47+
}

app/src/processing/app/debug/TargetPackage.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,31 @@
2525

2626
import java.io.File;
2727
import java.util.Collection;
28-
import java.util.HashMap;
28+
import java.util.LinkedHashMap;
2929
import java.util.Map;
3030

3131
import processing.app.helpers.filefilters.OnlyDirs;
3232

3333
public class TargetPackage {
3434

35-
private final String name;
35+
private String name;
3636

37-
Map<String, TargetPlatform> platforms = new HashMap<String, TargetPlatform>();
37+
Map<String, TargetPlatform> platforms = new LinkedHashMap<String, TargetPlatform>();
3838

39-
public TargetPackage(String name, File folder) {
40-
this.name = name;
39+
public TargetPackage(String packageName, File packageFolder)
40+
throws TargetPlatformException {
41+
name = packageName;
4142

42-
String[] platformsList = folder.list(new OnlyDirs());
43-
if (platformsList != null) {
44-
for (String platformName : platformsList) {
45-
File platformFolder = new File(folder, platformName);
46-
if (platformFolder.exists() && platformFolder.canRead()) {
47-
TargetPlatform platform = new TargetPlatform(platformName, platformFolder);
48-
platforms.put(platformName, platform);
49-
}
50-
}
43+
File[] folders = packageFolder.listFiles(new OnlyDirs());
44+
if (folders == null)
45+
return;
46+
47+
for (File folder : folders) {
48+
if (!folder.exists() || !folder.canRead())
49+
continue;
50+
String arch = folder.getName();
51+
TargetPlatform platform = new TargetPlatform(arch, folder);
52+
platforms.put(arch, platform);
5153
}
5254
}
5355

0 commit comments

Comments
 (0)