Skip to content

Commit 860e728

Browse files
cmagliefacchinm
authored andcommitted
Added BoardPort.protocolLabel and simplified port menu rendering
1 parent 5f38daa commit 860e728

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

app/src/processing/app/Editor.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import java.util.ArrayList;
5353
import java.util.Arrays;
5454
import java.util.Collections;
55-
import java.util.Comparator;
5655
import java.util.Enumeration;
5756
import java.util.HashMap;
5857
import java.util.LinkedList;
@@ -160,9 +159,6 @@ public boolean test(SketchController controller) {
160159
}
161160
}
162161

163-
private final static List<String> BOARD_PROTOCOLS_ORDER = Arrays.asList("serial", "network");
164-
private final static List<String> BOARD_PROTOCOLS_ORDER_TRANSLATIONS = Arrays.asList(tr("Serial ports"), tr("Network ports"));
165-
166162
final Base base;
167163

168164
// otherwise, if the window is resized with the message label
@@ -1092,6 +1088,9 @@ public String toString() {
10921088
}
10931089

10941090
private void populatePortMenu() {
1091+
final List<String> PROTOCOLS_ORDER = Arrays.asList("serial", "network");
1092+
final List<String> PROTOCOLS_LABELS = Arrays.asList(tr("Serial ports"), tr("Network ports"));
1093+
10951094
portMenu.removeAll();
10961095

10971096
String selectedPort = PreferencesData.get("serial.port");
@@ -1100,31 +1099,43 @@ private void populatePortMenu() {
11001099

11011100
ports = platform.filterPorts(ports, PreferencesData.getBoolean("serial.ports.showall"));
11021101

1103-
Collections.sort(ports, new Comparator<BoardPort>() {
1104-
@Override
1105-
public int compare(BoardPort o1, BoardPort o2) {
1106-
return (BOARD_PROTOCOLS_ORDER.indexOf(o1.getProtocol()) - BOARD_PROTOCOLS_ORDER.indexOf(o2.getProtocol())) * 10 +
1107-
o1.getAddress().compareTo(o2.getAddress());
1108-
}
1102+
ports.stream() //
1103+
.filter(port -> port.getProtocolLabel() == null || port.getProtocolLabel().isEmpty())
1104+
.forEach(port -> {
1105+
int labelIdx = PROTOCOLS_ORDER.indexOf(port.getProtocol());
1106+
if (labelIdx != -1) {
1107+
port.setProtocolLabel(PROTOCOLS_LABELS.get(labelIdx));
1108+
} else {
1109+
port.setProtocolLabel(port.getProtocol());
1110+
}
1111+
});
1112+
1113+
Collections.sort(ports, (port1, port2) -> {
1114+
String pr1 = port1.getProtocol();
1115+
String pr2 = port2.getProtocol();
1116+
int prIdx1 = PROTOCOLS_ORDER.contains(pr1) ? PROTOCOLS_ORDER.indexOf(pr1) : 999;
1117+
int prIdx2 = PROTOCOLS_ORDER.contains(pr2) ? PROTOCOLS_ORDER.indexOf(pr2) : 999;
1118+
int r = prIdx1 - prIdx2;
1119+
if (r != 0)
1120+
return r;
1121+
r = port1.getProtocolLabel().compareTo(port2.getProtocolLabel());
1122+
if (r != 0)
1123+
return r;
1124+
return port1.getAddress().compareTo(port2.getAddress());
11091125
});
11101126

1111-
String lastProtocol = null;
1112-
String lastProtocolTranslated;
1127+
String lastProtocol = "";
1128+
String lastProtocolLabel = "";
11131129
for (BoardPort port : ports) {
1114-
if (lastProtocol == null || !port.getProtocol().equals(lastProtocol)) {
1115-
if (lastProtocol != null) {
1130+
if (!port.getProtocol().equals(lastProtocol) || !port.getProtocolLabel().equals(lastProtocolLabel)) {
1131+
if (!lastProtocol.isEmpty()) {
11161132
portMenu.addSeparator();
11171133
}
11181134
lastProtocol = port.getProtocol();
1119-
1120-
if (BOARD_PROTOCOLS_ORDER.indexOf(port.getProtocol()) != -1) {
1121-
lastProtocolTranslated = BOARD_PROTOCOLS_ORDER_TRANSLATIONS.get(BOARD_PROTOCOLS_ORDER.indexOf(port.getProtocol()));
1122-
} else {
1123-
lastProtocolTranslated = port.getProtocol();
1124-
}
1125-
JMenuItem lastProtocolMenuItem = new JMenuItem(tr(lastProtocolTranslated));
1126-
lastProtocolMenuItem.setEnabled(false);
1127-
portMenu.add(lastProtocolMenuItem);
1135+
lastProtocolLabel = port.getProtocolLabel();
1136+
JMenuItem item = new JMenuItem(tr(lastProtocolLabel));
1137+
item.setEnabled(false);
1138+
portMenu.add(item);
11281139
}
11291140
String address = port.getAddress();
11301141

arduino-core/src/cc/arduino/packages/BoardPort.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class BoardPort {
3939

4040
private String address; // unique name for this port, used by Preferences
4141
private String protocol; // how to communicate, used for Ports menu sections
42+
private String protocolLabel; // protocol extended name to display on GUI
4243
private String boardName;
4344
private String boardId;
4445
private String label; // friendly name shown in Ports menu
@@ -77,6 +78,14 @@ public void setProtocol(String protocol) {
7778
this.protocol = protocol;
7879
}
7980

81+
public String getProtocolLabel() {
82+
return protocolLabel;
83+
}
84+
85+
public void setProtocolLabel(String protocolLabel) {
86+
this.protocolLabel = protocolLabel;
87+
}
88+
8089
public String getBoardName() {
8190
return boardName;
8291
}

0 commit comments

Comments
 (0)