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 ;
@@ -160,9 +159,6 @@ public boolean test(SketchController controller) {
160
159
}
161
160
}
162
161
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
-
166
162
final Base base ;
167
163
168
164
// otherwise, if the window is resized with the message label
@@ -1092,6 +1088,9 @@ public String toString() {
1092
1088
}
1093
1089
1094
1090
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
+
1095
1094
portMenu .removeAll ();
1096
1095
1097
1096
String selectedPort = PreferencesData .get ("serial.port" );
@@ -1100,31 +1099,43 @@ private void populatePortMenu() {
1100
1099
1101
1100
ports = platform .filterPorts (ports , PreferencesData .getBoolean ("serial.ports.showall" ));
1102
1101
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 ());
1109
1125
});
1110
1126
1111
- String lastProtocol = null ;
1112
- String lastProtocolTranslated ;
1127
+ String lastProtocol = "" ;
1128
+ String lastProtocolLabel = "" ;
1113
1129
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 () ) {
1116
1132
portMenu .addSeparator ();
1117
1133
}
1118
1134
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 );
1128
1139
}
1129
1140
String address = port .getAddress ();
1130
1141
0 commit comments