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 ;
@@ -147,9 +146,6 @@ public boolean test(SketchController controller) {
147
146
}
148
147
}
149
148
150
- private final static List <String > BOARD_PROTOCOLS_ORDER = Arrays .asList ("serial" , "network" );
151
- private final static List <String > BOARD_PROTOCOLS_ORDER_TRANSLATIONS = Arrays .asList (tr ("Serial ports" ), tr ("Network ports" ));
152
-
153
149
final Base base ;
154
150
155
151
// otherwise, if the window is resized with the message label
@@ -1062,6 +1058,9 @@ public String toString() {
1062
1058
}
1063
1059
1064
1060
private void populatePortMenu () {
1061
+ final List <String > PROTOCOLS_ORDER = Arrays .asList ("serial" , "network" );
1062
+ final List <String > PROTOCOLS_LABELS = Arrays .asList (tr ("Serial ports" ), tr ("Network ports" ));
1063
+
1065
1064
portMenu .removeAll ();
1066
1065
1067
1066
String selectedPort = PreferencesData .get ("serial.port" );
@@ -1070,31 +1069,43 @@ private void populatePortMenu() {
1070
1069
1071
1070
ports = platform .filterPorts (ports , PreferencesData .getBoolean ("serial.ports.showall" ));
1072
1071
1073
- Collections .sort (ports , new Comparator <BoardPort >() {
1074
- @ Override
1075
- public int compare (BoardPort o1 , BoardPort o2 ) {
1076
- return (BOARD_PROTOCOLS_ORDER .indexOf (o1 .getProtocol ()) - BOARD_PROTOCOLS_ORDER .indexOf (o2 .getProtocol ())) * 10 +
1077
- o1 .getAddress ().compareTo (o2 .getAddress ());
1078
- }
1072
+ ports .stream () //
1073
+ .filter (port -> port .getProtocolLabel () == null || port .getProtocolLabel ().isEmpty ())
1074
+ .forEach (port -> {
1075
+ int labelIdx = PROTOCOLS_ORDER .indexOf (port .getProtocol ());
1076
+ if (labelIdx != -1 ) {
1077
+ port .setProtocolLabel (PROTOCOLS_LABELS .get (labelIdx ));
1078
+ } else {
1079
+ port .setProtocolLabel (port .getProtocol ());
1080
+ }
1081
+ });
1082
+
1083
+ Collections .sort (ports , (port1 , port2 ) -> {
1084
+ String pr1 = port1 .getProtocol ();
1085
+ String pr2 = port2 .getProtocol ();
1086
+ int prIdx1 = PROTOCOLS_ORDER .contains (pr1 ) ? PROTOCOLS_ORDER .indexOf (pr1 ) : 999 ;
1087
+ int prIdx2 = PROTOCOLS_ORDER .contains (pr2 ) ? PROTOCOLS_ORDER .indexOf (pr2 ) : 999 ;
1088
+ int r = prIdx1 - prIdx2 ;
1089
+ if (r != 0 )
1090
+ return r ;
1091
+ r = port1 .getProtocolLabel ().compareTo (port2 .getProtocolLabel ());
1092
+ if (r != 0 )
1093
+ return r ;
1094
+ return port1 .getAddress ().compareTo (port2 .getAddress ());
1079
1095
});
1080
1096
1081
- String lastProtocol = null ;
1082
- String lastProtocolTranslated ;
1097
+ String lastProtocol = "" ;
1098
+ String lastProtocolLabel = "" ;
1083
1099
for (BoardPort port : ports ) {
1084
- if (lastProtocol == null || !port .getProtocol ().equals (lastProtocol )) {
1085
- if (lastProtocol != null ) {
1100
+ if (! port . getProtocol (). equals ( lastProtocol ) || !port .getProtocolLabel ().equals (lastProtocolLabel )) {
1101
+ if (! lastProtocol . isEmpty () ) {
1086
1102
portMenu .addSeparator ();
1087
1103
}
1088
1104
lastProtocol = port .getProtocol ();
1089
-
1090
- if (BOARD_PROTOCOLS_ORDER .indexOf (port .getProtocol ()) != -1 ) {
1091
- lastProtocolTranslated = BOARD_PROTOCOLS_ORDER_TRANSLATIONS .get (BOARD_PROTOCOLS_ORDER .indexOf (port .getProtocol ()));
1092
- } else {
1093
- lastProtocolTranslated = port .getProtocol ();
1094
- }
1095
- JMenuItem lastProtocolMenuItem = new JMenuItem (tr (lastProtocolTranslated ));
1096
- lastProtocolMenuItem .setEnabled (false );
1097
- portMenu .add (lastProtocolMenuItem );
1105
+ lastProtocolLabel = port .getProtocolLabel ();
1106
+ JMenuItem item = new JMenuItem (tr (lastProtocolLabel ));
1107
+ item .setEnabled (false );
1108
+ portMenu .add (item );
1098
1109
}
1099
1110
String address = port .getAddress ();
1100
1111
0 commit comments