1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-19 08:52:15 +01:00

Added BoardPort.protocolLabel and simplified port menu rendering

This commit is contained in:
Cristian Maglie 2018-11-29 15:40:10 +01:00
parent c03a8bc175
commit 349af4b5cf
2 changed files with 43 additions and 23 deletions

View File

@ -52,7 +52,6 @@ import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
@ -147,9 +146,6 @@ public class Editor extends JFrame implements RunnerListener {
}
}
private final static List<String> BOARD_PROTOCOLS_ORDER = Arrays.asList("serial", "network");
private final static List<String> BOARD_PROTOCOLS_ORDER_TRANSLATIONS = Arrays.asList(tr("Serial ports"), tr("Network ports"));
final Base base;
// otherwise, if the window is resized with the message label
@ -1062,6 +1058,9 @@ public class Editor extends JFrame implements RunnerListener {
}
private void populatePortMenu() {
final List<String> PROTOCOLS_ORDER = Arrays.asList("serial", "network");
final List<String> PROTOCOLS_LABELS = Arrays.asList(tr("Serial ports"), tr("Network ports"));
portMenu.removeAll();
String selectedPort = PreferencesData.get("serial.port");
@ -1070,31 +1069,43 @@ public class Editor extends JFrame implements RunnerListener {
ports = platform.filterPorts(ports, PreferencesData.getBoolean("serial.ports.showall"));
Collections.sort(ports, new Comparator<BoardPort>() {
@Override
public int compare(BoardPort o1, BoardPort o2) {
return (BOARD_PROTOCOLS_ORDER.indexOf(o1.getProtocol()) - BOARD_PROTOCOLS_ORDER.indexOf(o2.getProtocol())) * 10 +
o1.getAddress().compareTo(o2.getAddress());
}
ports.stream() //
.filter(port -> port.getProtocolLabel() == null || port.getProtocolLabel().isEmpty())
.forEach(port -> {
int labelIdx = PROTOCOLS_ORDER.indexOf(port.getProtocol());
if (labelIdx != -1) {
port.setProtocolLabel(PROTOCOLS_LABELS.get(labelIdx));
} else {
port.setProtocolLabel(port.getProtocol());
}
});
Collections.sort(ports, (port1, port2) -> {
String pr1 = port1.getProtocol();
String pr2 = port2.getProtocol();
int prIdx1 = PROTOCOLS_ORDER.contains(pr1) ? PROTOCOLS_ORDER.indexOf(pr1) : 999;
int prIdx2 = PROTOCOLS_ORDER.contains(pr2) ? PROTOCOLS_ORDER.indexOf(pr2) : 999;
int r = prIdx1 - prIdx2;
if (r != 0)
return r;
r = port1.getProtocolLabel().compareTo(port2.getProtocolLabel());
if (r != 0)
return r;
return port1.getAddress().compareTo(port2.getAddress());
});
String lastProtocol = null;
String lastProtocolTranslated;
String lastProtocol = "";
String lastProtocolLabel = "";
for (BoardPort port : ports) {
if (lastProtocol == null || !port.getProtocol().equals(lastProtocol)) {
if (lastProtocol != null) {
if (!port.getProtocol().equals(lastProtocol) || !port.getProtocolLabel().equals(lastProtocolLabel)) {
if (!lastProtocol.isEmpty()) {
portMenu.addSeparator();
}
lastProtocol = port.getProtocol();
if (BOARD_PROTOCOLS_ORDER.indexOf(port.getProtocol()) != -1) {
lastProtocolTranslated = BOARD_PROTOCOLS_ORDER_TRANSLATIONS.get(BOARD_PROTOCOLS_ORDER.indexOf(port.getProtocol()));
} else {
lastProtocolTranslated = port.getProtocol();
}
JMenuItem lastProtocolMenuItem = new JMenuItem(tr(lastProtocolTranslated));
lastProtocolMenuItem.setEnabled(false);
portMenu.add(lastProtocolMenuItem);
lastProtocolLabel = port.getProtocolLabel();
JMenuItem item = new JMenuItem(tr(lastProtocolLabel));
item.setEnabled(false);
portMenu.add(item);
}
String address = port.getAddress();

View File

@ -39,6 +39,7 @@ public class BoardPort {
private String address; // unique name for this port, used by Preferences
private String protocol; // how to communicate, used for Ports menu sections
private String protocolLabel; // protocol extended name to display on GUI
private String boardName;
private String label; // friendly name shown in Ports menu
private final PreferencesMap identificationPrefs; // data to match with boards.txt
@ -76,6 +77,14 @@ public class BoardPort {
this.protocol = protocol;
}
public String getProtocolLabel() {
return protocolLabel;
}
public void setProtocolLabel(String protocolLabel) {
this.protocolLabel = protocolLabel;
}
public String getBoardName() {
return boardName;
}