5252import java .util .ArrayList ;
5353import java .util .Arrays ;
5454import java .util .Collections ;
55- import java .util .Comparator ;
5655import java .util .Enumeration ;
5756import java .util .HashMap ;
5857import 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
0 commit comments