Skip to content

Commit 75d1cb8

Browse files
cmaglieFederico Fissore
authored andcommitted
Available boards are updated right after closing "Boards Installer"
Boards menu list has been moved into Base class where it looks a more appropriate place to reduce interactions between Editor class and Base class. Probably shared menus can be moved in a separate, specific, class.
1 parent 0783f40 commit 75d1cb8

File tree

3 files changed

+71
-74
lines changed

3 files changed

+71
-74
lines changed

app/src/processing/app/Base.java

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public class Base {
8686
List<Editor> editors = Collections.synchronizedList(new ArrayList<Editor>());
8787
Editor activeEditor;
8888

89+
// these menus are shared so that the board and serial port selections
90+
// are the same for all windows (since the board and serial port that are
91+
// actually used are determined by the preferences, which are shared)
92+
static List<JMenu> boardsCustomMenus;
93+
8994
static public void main(String args[]) throws Exception {
9095
System.setProperty("awt.useSystemAAFontSettings", "on");
9196
System.setProperty("swing.aatext", "true");
@@ -238,7 +243,8 @@ public Base(String[] args) throws Exception {
238243
splashScreenHelper.splashText(_("Initializing packages..."));
239244
BaseNoGui.initPackages();
240245
splashScreenHelper.splashText(_("Preparing boards..."));
241-
246+
rebuildBoardsMenu();
247+
242248
// Setup board-dependent variables.
243249
onBoardOrPortChange();
244250

@@ -1114,78 +1120,95 @@ private void openInstallBoardDialog() {
11141120
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor) {
11151121
@Override
11161122
protected void onIndexesUpdated() throws Exception {
1117-
BaseNoGui.reloadAllHardware();
1123+
BaseNoGui.initPackages();
1124+
rebuildBoardsMenu();
11181125
setIndexer(BaseNoGui.indexer);
11191126
}
11201127
};
11211128
managerUI.setIndexer(BaseNoGui.indexer);
11221129
managerUI.setVisible(true);
1130+
// Installer dialog is modal, waits here until closed
1131+
1132+
// Reload all boards (that may have been installed/updated/removed)
1133+
try {
1134+
BaseNoGui.initPackages();
1135+
rebuildBoardsMenu();
1136+
onBoardOrPortChange();
1137+
} catch (Exception e) {
1138+
// TODO Auto-generated catch block
1139+
e.printStackTrace();
1140+
}
11231141
}
11241142

1125-
public void rebuildBoardsMenu(JMenu toolsMenu, Editor editor) throws Exception {
1126-
JMenu boardsMenu = getBoardCustomMenu();
1143+
public void rebuildBoardsMenu() throws Exception {
1144+
boardsCustomMenus = new LinkedList<JMenu>();
11271145

1146+
// The first custom menu is the "Board" selection submenu
1147+
JMenu boardMenu = new JMenu(_("Board"));
1148+
MenuScroller.setScrollerFor(boardMenu);
11281149
@SuppressWarnings("serial")
11291150
Action runInstaller = new AbstractAction("Install boards...") {
11301151
public void actionPerformed(ActionEvent actionevent) {
11311152
openInstallBoardDialog();
11321153
}
11331154
};
1134-
boardsMenu.add(new JMenuItem(runInstaller));
1155+
boardMenu.add(new JMenuItem(runInstaller));
1156+
boardsCustomMenus.add(boardMenu);
11351157

1136-
// If there are no platforms installed skip menu creation
1158+
// If there are no platforms installed we are done
11371159
if (BaseNoGui.packages.size() == 0)
11381160
return;
1139-
1140-
boardsMenu.add(new JSeparator());
11411161

1142-
boolean first = true;
1143-
1144-
List<JMenuItem> menuItemsToClickAfterStartup = new LinkedList<JMenuItem>();
1145-
1146-
ButtonGroup boardsButtonGroup = new ButtonGroup();
1147-
Map<String, ButtonGroup> buttonGroupsMap = new HashMap<String, ButtonGroup>();
1162+
// Separate "Install boards..." command from installed boards
1163+
boardMenu.add(new JSeparator());
11481164

11491165
// Generate custom menus for all platforms
11501166
Set<String> titles = new HashSet<String>();
11511167
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
11521168
for (TargetPlatform targetPlatform : targetPackage.platforms())
11531169
titles.addAll(targetPlatform.getCustomMenus().values());
11541170
}
1155-
for (String title : titles)
1156-
makeBoardCustomMenu(toolsMenu, _(title));
1157-
1171+
for (String title : titles) {
1172+
boardsCustomMenus.add(new JMenu(_(title)));
1173+
}
1174+
1175+
List<JMenuItem> menuItemsToClickAfterStartup = new LinkedList<JMenuItem>();
1176+
1177+
ButtonGroup boardsButtonGroup = new ButtonGroup();
1178+
Map<String, ButtonGroup> buttonGroupsMap = new HashMap<String, ButtonGroup>();
1179+
11581180
// Cycle through all packages
1181+
boolean first = true;
11591182
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
11601183
// For every package cycle through all platform
11611184
for (TargetPlatform targetPlatform : targetPackage.platforms()) {
11621185

11631186
// Add a separator from the previous platform
11641187
if (!first)
1165-
boardsMenu.add(new JSeparator());
1188+
boardMenu.add(new JSeparator());
11661189
first = false;
11671190

11681191
// Add a title for each platform
1169-
String platformLabel = targetPlatform.getPreferences().get("name");
1192+
String platformLabel = targetPlatform.getPreferences().get("name");
11701193
if (platformLabel != null && !targetPlatform.getBoards().isEmpty()) {
11711194
JMenuItem menuLabel = new JMenuItem(_(platformLabel));
11721195
menuLabel.setEnabled(false);
1173-
boardsMenu.add(menuLabel);
1196+
boardMenu.add(menuLabel);
11741197
}
11751198

11761199
// Cycle through all boards of this platform
11771200
for (TargetBoard board : targetPlatform.getBoards().values()) {
11781201
JMenuItem item = createBoardMenusAndCustomMenus(menuItemsToClickAfterStartup,
11791202
buttonGroupsMap,
11801203
board, targetPlatform, targetPackage);
1181-
boardsMenu.add(item);
1204+
boardMenu.add(item);
11821205
boardsButtonGroup.add(item);
11831206
}
11841207
}
11851208
}
11861209

11871210
if (menuItemsToClickAfterStartup.isEmpty()) {
1188-
menuItemsToClickAfterStartup.add(selectFirstEnabledMenuItem(boardsMenu));
1211+
menuItemsToClickAfterStartup.add(selectFirstEnabledMenuItem(boardMenu));
11891212
}
11901213

11911214
for (JMenuItem menuItemToClick : menuItemsToClickAfterStartup) {
@@ -1265,9 +1288,10 @@ public void actionPerformed(ActionEvent e) {
12651288
return item;
12661289
}
12671290

1268-
private static void filterVisibilityOfSubsequentBoardMenus(TargetBoard board, int fromIndex) {
1269-
for (int i = fromIndex; i < Editor.boardsMenus.size(); i++) {
1270-
JMenu menu = Editor.boardsMenus.get(i);
1291+
private static void filterVisibilityOfSubsequentBoardMenus(TargetBoard board,
1292+
int fromIndex) {
1293+
for (int i = fromIndex; i < boardsCustomMenus.size(); i++) {
1294+
JMenu menu = boardsCustomMenus.get(i);
12711295
for (int m = 0; m < menu.getItemCount(); m++) {
12721296
JMenuItem menuItem = menu.getItem(m);
12731297
menuItem.setVisible(menuItem.getAction().getValue("board").equals(board));
@@ -1293,19 +1317,8 @@ private static boolean ifThereAreVisibleItemsOn(JMenu menu) {
12931317
return false;
12941318
}
12951319

1296-
private JMenu makeBoardCustomMenu(JMenu toolsMenu, String label) {
1297-
JMenu menu = new JMenu(label);
1298-
Editor.boardsMenus.add(menu);
1299-
toolsMenu.add(menu);
1300-
return menu;
1301-
}
1302-
1303-
private JMenu getBoardCustomMenu() throws Exception {
1304-
return getBoardCustomMenu(_("Board"));
1305-
}
1306-
13071320
private JMenu getBoardCustomMenu(String label) throws Exception {
1308-
for (JMenu menu : Editor.boardsMenus)
1321+
for (JMenu menu : boardsCustomMenus)
13091322
if (label.equals(menu.getText()))
13101323
return menu;
13111324
throw new Exception("Custom menu not found!");
@@ -1760,6 +1773,10 @@ static public PreferencesMap getBoardPreferences() {
17601773
return BaseNoGui.getBoardPreferences();
17611774
}
17621775

1776+
public static List<JMenu> getBoardsCustomMenus() {
1777+
return boardsCustomMenus;
1778+
}
1779+
17631780
static public File getPortableFolder() {
17641781
return BaseNoGui.getPortableFolder();
17651782
}

app/src/processing/app/Editor.java

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ public class Editor extends JFrame implements RunnerListener {
109109
static JMenu examplesMenu;
110110
static JMenu importMenu;
111111

112-
// these menus are shared so that the board and serial port selections
113-
// are the same for all windows (since the board and serial port that are
114-
// actually used are determined by the preferences, which are shared)
115-
static List<JMenu> boardsMenus;
116112
static JMenu serialMenu;
117113

118114
static AbstractMonitor serialMonitor;
@@ -195,7 +191,7 @@ public void windowActivated(WindowEvent e) {
195191
fileMenu.insert(examplesMenu, 3);
196192
sketchMenu.insert(importMenu, 4);
197193
int offset = 0;
198-
for (JMenu menu : boardsMenus) {
194+
for (JMenu menu : Base.getBoardsCustomMenus()) {
199195
toolsMenu.insert(menu, numTools + offset);
200196
offset++;
201197
}
@@ -209,7 +205,7 @@ public void windowDeactivated(WindowEvent e) {
209205
fileMenu.remove(sketchbookMenu);
210206
fileMenu.remove(examplesMenu);
211207
sketchMenu.remove(importMenu);
212-
for (JMenu menu : boardsMenus) {
208+
for (JMenu menu : Base.getBoardsCustomMenus()) {
213209
toolsMenu.remove(menu);
214210
}
215211
toolsMenu.remove(serialMenu);
@@ -684,63 +680,51 @@ public void actionPerformed(ActionEvent e) {
684680

685681
protected JMenu buildToolsMenu() throws Exception {
686682
toolsMenu = new JMenu(_("Tools"));
687-
JMenu menu = toolsMenu;
688-
JMenuItem item;
689683

690-
addInternalTools(menu);
684+
addInternalTools(toolsMenu);
691685

692-
item = newJMenuItemShift(_("Serial Monitor"), 'M');
686+
JMenuItem item = newJMenuItemShift(_("Serial Monitor"), 'M');
693687
item.addActionListener(new ActionListener() {
694688
public void actionPerformed(ActionEvent e) {
695689
handleSerial();
696690
}
697691
});
698-
menu.add(item);
692+
toolsMenu.add(item);
699693

700-
addTools(menu, Base.getToolsFolder());
694+
addTools(toolsMenu, Base.getToolsFolder());
701695
File sketchbookTools = new File(Base.getSketchbookFolder(), "tools");
702-
addTools(menu, sketchbookTools);
696+
addTools(toolsMenu, sketchbookTools);
703697

704-
menu.addSeparator();
698+
toolsMenu.addSeparator();
705699

706-
numTools = menu.getItemCount();
700+
numTools = toolsMenu.getItemCount();
707701

708702
// XXX: DAM: these should probably be implemented using the Tools plugin
709703
// API, if possible (i.e. if it supports custom actions, etc.)
710704

711-
if (boardsMenus == null) {
712-
boardsMenus = new LinkedList<JMenu>();
713-
714-
JMenu boardsMenu = new JMenu(_("Board"));
715-
MenuScroller.setScrollerFor(boardsMenu);
716-
Editor.boardsMenus.add(boardsMenu);
717-
toolsMenu.add(boardsMenu);
718-
719-
base.rebuildBoardsMenu(toolsMenu, this);
720-
//Debug: rebuild imports
721-
importMenu.removeAll();
722-
base.rebuildImportMenu(importMenu);
705+
for (JMenu menu : Base.getBoardsCustomMenus()) {
706+
toolsMenu.add(menu);
723707
}
724708

725709
if (serialMenu == null)
726710
serialMenu = new JMenu(_("Port"));
727711
populatePortMenu();
728-
menu.add(serialMenu);
729-
menu.addSeparator();
712+
toolsMenu.add(serialMenu);
713+
toolsMenu.addSeparator();
730714

731715
JMenu programmerMenu = new JMenu(_("Programmer"));
732716
base.rebuildProgrammerMenu(programmerMenu);
733-
menu.add(programmerMenu);
717+
toolsMenu.add(programmerMenu);
734718

735719
item = new JMenuItem(_("Burn Bootloader"));
736720
item.addActionListener(new ActionListener() {
737721
public void actionPerformed(ActionEvent e) {
738722
handleBurnBootloader();
739723
}
740724
});
741-
menu.add(item);
725+
toolsMenu.add(item);
742726

743-
menu.addMenuListener(new MenuListener() {
727+
toolsMenu.addMenuListener(new MenuListener() {
744728
public void menuCanceled(MenuEvent e) {}
745729
public void menuDeselected(MenuEvent e) {}
746730
public void menuSelected(MenuEvent e) {
@@ -749,7 +733,7 @@ public void menuSelected(MenuEvent e) {
749733
}
750734
});
751735

752-
return menu;
736+
return toolsMenu;
753737
}
754738

755739

arduino-core/src/processing/app/BaseNoGui.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,6 @@ static public void initLogger() {
587587
}
588588

589589
static public void initPackages() throws Exception {
590-
reloadAllHardware();
591-
}
592-
593-
static public void reloadAllHardware() throws Exception {
594590
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder());
595591
File indexFile = indexer.getIndexFile();
596592
if (!indexFile.isFile()) {

0 commit comments

Comments
 (0)