Skip to content

Select (first) target board when port is selected and has known board #7120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/cc/arduino/view/preferences/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private void initComponents() {
verifyUploadBox = new javax.swing.JCheckBox();
externalEditorBox = new javax.swing.JCheckBox();
cacheCompiledCore = new javax.swing.JCheckBox();
autoselectBoard = new javax.swing.JCheckBox();
checkUpdatesBox = new javax.swing.JCheckBox();
updateExtensionBox = new javax.swing.JCheckBox();
saveVerifyUploadBox = new javax.swing.JCheckBox();
Expand Down Expand Up @@ -275,6 +276,9 @@ public void mouseEntered(java.awt.event.MouseEvent evt) {
cacheCompiledCore.setText(tr("Aggressively cache compiled core"));
checkboxesContainer.add(cacheCompiledCore);

autoselectBoard.setText(tr("Automatically use the correct target when selecting a known serial port"));
checkboxesContainer.add(autoselectBoard);

checkUpdatesBox.setText(tr("Check for updates on startup"));
checkboxesContainer.add(checkUpdatesBox);

Expand Down Expand Up @@ -710,6 +714,7 @@ private void autoScaleCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//
private javax.swing.JButton extendedAdditionalUrlFieldWindow;
private javax.swing.JCheckBox externalEditorBox;
private javax.swing.JCheckBox cacheCompiledCore;
private javax.swing.JCheckBox autoselectBoard;
private javax.swing.JTextField fontSizeField;
private javax.swing.JLabel fontSizeLabel;
private javax.swing.JLabel jLabel1;
Expand Down Expand Up @@ -806,6 +811,8 @@ private void savePreferencesData() {

PreferencesData.setBoolean("compiler.cache_core", cacheCompiledCore.isSelected());

PreferencesData.setBoolean("editor.autoselectboard", autoselectBoard.isSelected());

PreferencesData.setBoolean("update.check", checkUpdatesBox.isSelected());

PreferencesData.setBoolean("editor.update_extension", updateExtensionBox.isSelected());
Expand Down Expand Up @@ -868,6 +875,8 @@ private void showPrerefencesData() {

cacheCompiledCore.setSelected(PreferencesData.get("compiler.cache_core") == null || PreferencesData.getBoolean("compiler.cache_core"));

autoselectBoard.setSelected(PreferencesData.getBoolean("editor.autoselectboard"));

checkUpdatesBox.setSelected(PreferencesData.getBoolean("update.check"));

updateExtensionBox.setSelected(PreferencesData.get("editor.update_extension") == null || PreferencesData.getBoolean("editor.update_extension"));
Expand Down
33 changes: 26 additions & 7 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public class Base {
// int editorCount;
List<Editor> editors = Collections.synchronizedList(new ArrayList<Editor>());
Editor activeEditor;

private static JMenu boardMenu;

// these menus are shared so that the board and serial port selections
// are the same for all windows (since the board and serial port that are
Expand Down Expand Up @@ -1312,6 +1314,28 @@ public void rebuildExamplesMenu(JMenu menu) {

private static String priorPlatformFolder;
private static boolean newLibraryImported;

public void selectTargetBoard(TargetBoard targetBoard) {
for (int i = 0; i < boardMenu.getItemCount(); i++) {
JMenuItem menuItem = boardMenu.getItem(i);
if (!(menuItem instanceof JRadioButtonMenuItem)) {
continue;
}

JRadioButtonMenuItem radioButtonMenuItem = ((JRadioButtonMenuItem) menuItem);
if (targetBoard.getName().equals(radioButtonMenuItem.getText())) {
radioButtonMenuItem.setSelected(true);
break;
}
}

BaseNoGui.selectBoard(targetBoard);
filterVisibilityOfSubsequentBoardMenus(boardsCustomMenus, targetBoard, 1);

onBoardOrPortChange();
rebuildImportMenu(Editor.importMenu);
rebuildExamplesMenu(Editor.examplesMenu);
}

public void onBoardOrPortChange() {
BaseNoGui.onBoardOrPortChange();
Expand Down Expand Up @@ -1406,7 +1430,7 @@ public void rebuildBoardsMenu() throws Exception {
boardsCustomMenus = new LinkedList<>();

// The first custom menu is the "Board" selection submenu
JMenu boardMenu = new JMenu(tr("Board"));
boardMenu = new JMenu(tr("Board"));
boardMenu.putClientProperty("removeOnWindowDeactivation", true);
MenuScroller.setScrollerFor(boardMenu).setTopFixedCount(1);

Expand Down Expand Up @@ -1512,12 +1536,7 @@ private JRadioButtonMenuItem createBoardMenusAndCustomMenus(
@SuppressWarnings("serial")
Action action = new AbstractAction(board.getName()) {
public void actionPerformed(ActionEvent actionevent) {
BaseNoGui.selectBoard((TargetBoard) getValue("b"));
filterVisibilityOfSubsequentBoardMenus(boardsCustomMenus, (TargetBoard) getValue("b"), 1);

onBoardOrPortChange();
rebuildImportMenu(Editor.importMenu);
rebuildExamplesMenu(Editor.examplesMenu);
selectTargetBoard((TargetBoard) getValue("b"));
}
};
action.putValue("b", board);
Expand Down
23 changes: 18 additions & 5 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import com.jcraft.jsch.JSchException;
import jssc.SerialPortException;
import processing.app.debug.RunnerException;
import processing.app.debug.TargetBoard;
import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform;
import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.DocumentTextChangeListener;
import processing.app.helpers.Keys;
Expand Down Expand Up @@ -1006,19 +1009,21 @@ private void addInternalTools(JMenu menu) {
class SerialMenuListener implements ActionListener {

private final String serialPort;
private final String boardId;

public SerialMenuListener(String serialPort) {
public SerialMenuListener(String serialPort, String boardId) {
this.serialPort = serialPort;
this.boardId = boardId;
}

public void actionPerformed(ActionEvent e) {
selectSerialPort(serialPort);
selectSerialPort(serialPort, boardId);
base.onBoardOrPortChange();
}

}

private void selectSerialPort(String name) {
private void selectSerialPort(String name, String boardId) {
if(portMenu == null) {
System.out.println(tr("serialMenu is null"));
return;
Expand Down Expand Up @@ -1059,6 +1064,13 @@ private void selectSerialPort(String name) {
}
}

if (boardId != null && PreferencesData.getBoolean("editor.autoselectboard")) {
TargetBoard targetBoard = BaseNoGui.getPlatform().resolveBoardById(BaseNoGui.packages, boardId);
if (targetBoard != null) {
base.selectTargetBoard(targetBoard);
}
}

onBoardOrPortChange();
base.onBoardOrPortChange();

Expand Down Expand Up @@ -1102,9 +1114,10 @@ public int compare(BoardPort o1, BoardPort o2) {
}
String address = port.getAddress();
String label = port.getLabel();
String boardId = port.getBoardId();

JCheckBoxMenuItem item = new JCheckBoxMenuItem(label, address.equals(selectedPort));
item.addActionListener(new SerialMenuListener(address));
item.addActionListener(new SerialMenuListener(address, boardId));
portMenu.add(item);
}

Expand Down Expand Up @@ -2111,7 +2124,7 @@ private boolean serialPrompt() {
names,
0);
if (result == null) return false;
selectSerialPort(result);
selectSerialPort(result, null);
base.onBoardOrPortChange();
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions arduino-core/src/cc/arduino/packages/BoardPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class BoardPort {
private String address;
private String protocol;
private String boardName;
private String boardId;
private String vid;
private String pid;
private String iserial;
Expand Down Expand Up @@ -71,6 +72,14 @@ public void setBoardName(String boardName) {
this.boardName = boardName;
}

public String getBoardId() {
return boardId;
}

public void setBoardId(String boardId) {
this.boardId = boardId;
}

public PreferencesMap getPrefs() {
return prefs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void serviceResolved(ServiceEvent serviceEvent) {

port.setAddress(address);
port.setBoardName(name);
port.setBoardId(board);
port.setProtocol("network");
port.setLabel(label);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public synchronized void retriggerDiscovery(boolean polled) {
label += " (" + boardName + ")";
}
boardPort.setBoardName(boardName);
boardPort.setBoardId(board.getId());
}
} else {
if (!parts[1].equals("0000")) {
Expand Down
12 changes: 10 additions & 2 deletions arduino-core/src/processing/app/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,29 @@ public synchronized Map<String, Object> resolveDeviceByVendorIdProductId(String
return null;
}

public String resolveDeviceByBoardID(Map<String, TargetPackage> packages, String boardId) {
public TargetBoard resolveBoardById(Map<String, TargetPackage> packages, String boardId) {
assert packages != null;
assert boardId != null;
for (TargetPackage targetPackage : packages.values()) {
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
for (TargetBoard board : targetPlatform.getBoards().values()) {
if (boardId.equals(board.getId())) {
return board.getName();
return board;
}
}
}
}
return null;
}

public String resolveDeviceByBoardID(Map<String, TargetPackage> packages, String boardId) {
TargetBoard targetBoard = resolveBoardById(packages, boardId);
if (targetBoard != null) {
return targetBoard.getName();
}
return null;
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

public String getName() {
Expand Down