()
| 1071 | } |
| 1072 | |
| 1073 | private void populatePortMenu() { |
| 1074 | final List<String> PROTOCOLS_ORDER = Arrays.asList("serial", "network"); |
| 1075 | final List<String> PROTOCOLS_LABELS = Arrays.asList(tr("Serial ports"), tr("Network ports")); |
| 1076 | |
| 1077 | portMenu.removeAll(); |
| 1078 | |
| 1079 | String selectedPort = PreferencesData.get("serial.port"); |
| 1080 | |
| 1081 | List<BoardPort> ports = Base.getDiscoveryManager().discovery(); |
| 1082 | |
| 1083 | ports = platform.filterPorts(ports, PreferencesData.getBoolean("serial.ports.showall")); |
| 1084 | |
| 1085 | ports.stream() // |
| 1086 | .filter(port -> port.getProtocolLabel() == null || port.getProtocolLabel().isEmpty()) |
| 1087 | .forEach(port -> { |
| 1088 | int labelIdx = PROTOCOLS_ORDER.indexOf(port.getProtocol()); |
| 1089 | if (labelIdx != -1) { |
| 1090 | port.setProtocolLabel(PROTOCOLS_LABELS.get(labelIdx)); |
| 1091 | } else { |
| 1092 | port.setProtocolLabel(port.getProtocol()); |
| 1093 | } |
| 1094 | }); |
| 1095 | |
| 1096 | Collections.sort(ports, (port1, port2) -> { |
| 1097 | String pr1 = port1.getProtocol(); |
| 1098 | String pr2 = port2.getProtocol(); |
| 1099 | int prIdx1 = PROTOCOLS_ORDER.contains(pr1) ? PROTOCOLS_ORDER.indexOf(pr1) : 999; |
| 1100 | int prIdx2 = PROTOCOLS_ORDER.contains(pr2) ? PROTOCOLS_ORDER.indexOf(pr2) : 999; |
| 1101 | int r = prIdx1 - prIdx2; |
| 1102 | if (r != 0) |
| 1103 | return r; |
| 1104 | r = port1.getProtocolLabel().compareTo(port2.getProtocolLabel()); |
| 1105 | if (r != 0) |
| 1106 | return r; |
| 1107 | return port1.getAddress().compareTo(port2.getAddress()); |
| 1108 | }); |
| 1109 | |
| 1110 | String lastProtocol = ""; |
| 1111 | String lastProtocolLabel = ""; |
| 1112 | for (BoardPort port : ports) { |
| 1113 | if (!port.getProtocol().equals(lastProtocol) || !port.getProtocolLabel().equals(lastProtocolLabel)) { |
| 1114 | if (!lastProtocol.isEmpty()) { |
| 1115 | portMenu.addSeparator(); |
| 1116 | } |
| 1117 | lastProtocol = port.getProtocol(); |
| 1118 | lastProtocolLabel = port.getProtocolLabel(); |
| 1119 | JMenuItem item = new JMenuItem(tr(lastProtocolLabel)); |
| 1120 | item.setEnabled(false); |
| 1121 | portMenu.add(item); |
| 1122 | } |
| 1123 | String address = port.getAddress(); |
| 1124 | |
| 1125 | BoardPortJCheckBoxMenuItem item = new BoardPortJCheckBoxMenuItem(port); |
| 1126 | item.setSelected(address.equals(selectedPort)); |
| 1127 | portMenu.add(item); |
| 1128 | } |
| 1129 | |
| 1130 | portMenu.setEnabled(portMenu.getMenuComponentCount() > 0); |
no test coverage detected