52
52
import java .util .ArrayList ;
53
53
import java .util .Arrays ;
54
54
import java .util .Collections ;
55
- import java .util .Comparator ;
56
55
import java .util .Enumeration ;
57
56
import java .util .HashMap ;
58
57
import java .util .LinkedList ;
@@ -140,9 +139,6 @@ public boolean test(SketchController sketch) {
140
139
}
141
140
}
142
141
143
- private final static List <String > BOARD_PROTOCOLS_ORDER = Arrays .asList ("serial" , "network" );
144
- private final static List <String > BOARD_PROTOCOLS_ORDER_TRANSLATIONS = Arrays .asList (tr ("Serial ports" ), tr ("Network ports" ));
145
-
146
142
final Base base ;
147
143
148
144
// otherwise, if the window is resized with the message label
@@ -1054,6 +1050,9 @@ public String toString() {
1054
1050
}
1055
1051
1056
1052
private void populatePortMenu () {
1053
+ final List <String > PROTOCOLS_ORDER = Arrays .asList ("serial" , "network" );
1054
+ final List <String > PROTOCOLS_LABELS = Arrays .asList (tr ("Serial ports" ), tr ("Network ports" ));
1055
+
1057
1056
portMenu .removeAll ();
1058
1057
1059
1058
String selectedPort = PreferencesData .get ("serial.port" );
@@ -1062,31 +1061,43 @@ private void populatePortMenu() {
1062
1061
1063
1062
ports = platform .filterPorts (ports , PreferencesData .getBoolean ("serial.ports.showall" ));
1064
1063
1065
- Collections .sort (ports , new Comparator <BoardPort >() {
1066
- @ Override
1067
- public int compare (BoardPort o1 , BoardPort o2 ) {
1068
- return (BOARD_PROTOCOLS_ORDER .indexOf (o1 .getProtocol ()) - BOARD_PROTOCOLS_ORDER .indexOf (o2 .getProtocol ())) * 10 +
1069
- o1 .getAddress ().compareTo (o2 .getAddress ());
1070
- }
1064
+ ports .stream () //
1065
+ .filter (port -> port .getProtocolLabel () == null || port .getProtocolLabel ().isEmpty ())
1066
+ .forEach (port -> {
1067
+ int labelIdx = PROTOCOLS_ORDER .indexOf (port .getProtocol ());
1068
+ if (labelIdx != -1 ) {
1069
+ port .setProtocolLabel (PROTOCOLS_LABELS .get (labelIdx ));
1070
+ } else {
1071
+ port .setProtocolLabel (port .getProtocol ());
1072
+ }
1073
+ });
1074
+
1075
+ Collections .sort (ports , (port1 , port2 ) -> {
1076
+ String pr1 = port1 .getProtocol ();
1077
+ String pr2 = port2 .getProtocol ();
1078
+ int prIdx1 = PROTOCOLS_ORDER .contains (pr1 ) ? PROTOCOLS_ORDER .indexOf (pr1 ) : 999 ;
1079
+ int prIdx2 = PROTOCOLS_ORDER .contains (pr2 ) ? PROTOCOLS_ORDER .indexOf (pr2 ) : 999 ;
1080
+ int r = prIdx1 - prIdx2 ;
1081
+ if (r != 0 )
1082
+ return r ;
1083
+ r = port1 .getProtocolLabel ().compareTo (port2 .getProtocolLabel ());
1084
+ if (r != 0 )
1085
+ return r ;
1086
+ return port1 .getAddress ().compareTo (port2 .getAddress ());
1071
1087
});
1072
1088
1073
- String lastProtocol = null ;
1074
- String lastProtocolTranslated ;
1089
+ String lastProtocol = "" ;
1090
+ String lastProtocolLabel = "" ;
1075
1091
for (BoardPort port : ports ) {
1076
- if (lastProtocol == null || !port .getProtocol ().equals (lastProtocol )) {
1077
- if (lastProtocol != null ) {
1092
+ if (! port . getProtocol (). equals ( lastProtocol ) || !port .getProtocolLabel ().equals (lastProtocolLabel )) {
1093
+ if (! lastProtocol . isEmpty () ) {
1078
1094
portMenu .addSeparator ();
1079
1095
}
1080
1096
lastProtocol = port .getProtocol ();
1081
-
1082
- if (BOARD_PROTOCOLS_ORDER .indexOf (port .getProtocol ()) != -1 ) {
1083
- lastProtocolTranslated = BOARD_PROTOCOLS_ORDER_TRANSLATIONS .get (BOARD_PROTOCOLS_ORDER .indexOf (port .getProtocol ()));
1084
- } else {
1085
- lastProtocolTranslated = port .getProtocol ();
1086
- }
1087
- JMenuItem lastProtocolMenuItem = new JMenuItem (tr (lastProtocolTranslated ));
1088
- lastProtocolMenuItem .setEnabled (false );
1089
- portMenu .add (lastProtocolMenuItem );
1097
+ lastProtocolLabel = port .getProtocolLabel ();
1098
+ JMenuItem item = new JMenuItem (tr (lastProtocolLabel ));
1099
+ item .setEnabled (false );
1100
+ portMenu .add (item );
1090
1101
}
1091
1102
String address = port .getAddress ();
1092
1103
0 commit comments