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 ;
96
95
import cc .arduino .view .findreplace .FindReplace ;
97
96
import jssc .SerialPortException ;
98
97
import processing .app .debug .RunnerException ;
98
+ import processing .app .debug .TargetBoard ;
99
99
import processing .app .forms .PasswordAuthorizationDialog ;
100
100
import processing .app .helpers .DocumentTextChangeListener ;
101
101
import processing .app .helpers .Keys ;
@@ -149,9 +149,6 @@ public boolean test(SketchController controller) {
149
149
}
150
150
}
151
151
152
- private final static List <String > BOARD_PROTOCOLS_ORDER = Arrays .asList ("serial" , "network" );
153
- private final static List <String > BOARD_PROTOCOLS_ORDER_TRANSLATIONS = Arrays .asList (tr ("Serial ports" ), tr ("Network ports" ));
154
-
155
152
final Base base ;
156
153
157
154
// otherwise, if the window is resized with the message label
@@ -1043,22 +1040,30 @@ class BoardPortJCheckBoxMenuItem extends JCheckBoxMenuItem {
1043
1040
private BoardPort port ;
1044
1041
1045
1042
public BoardPortJCheckBoxMenuItem (BoardPort port ) {
1046
- super (port .getLabel ());
1043
+ super ();
1044
+ this .port = port ;
1045
+ setText (toString ());
1047
1046
addActionListener (e -> {
1048
1047
selectSerialPort (port .getAddress ());
1049
1048
base .onBoardOrPortChange ();
1050
1049
});
1051
- this .port = port ;
1052
1050
}
1053
1051
1054
1052
@ Override
1055
1053
public String toString () {
1056
1054
// This is required for serialPrompt()
1057
- return port .getLabel ();
1055
+ String label = port .getLabel ();
1056
+ if (port .getBoardName () != null && !port .getBoardName ().isEmpty ()) {
1057
+ label += " (" + port .getBoardName () + ")" ;
1058
+ }
1059
+ return label ;
1058
1060
}
1059
1061
}
1060
1062
1061
1063
private void populatePortMenu () {
1064
+ final List <String > PROTOCOLS_ORDER = Arrays .asList ("serial" , "network" );
1065
+ final List <String > PROTOCOLS_LABELS = Arrays .asList (tr ("Serial ports" ), tr ("Network ports" ));
1066
+
1062
1067
portMenu .removeAll ();
1063
1068
1064
1069
String selectedPort = PreferencesData .get ("serial.port" );
@@ -1067,31 +1072,43 @@ private void populatePortMenu() {
1067
1072
1068
1073
ports = platform .filterPorts (ports , PreferencesData .getBoolean ("serial.ports.showall" ));
1069
1074
1070
- Collections .sort (ports , new Comparator <BoardPort >() {
1071
- @ Override
1072
- public int compare (BoardPort o1 , BoardPort o2 ) {
1073
- return (BOARD_PROTOCOLS_ORDER .indexOf (o1 .getProtocol ()) - BOARD_PROTOCOLS_ORDER .indexOf (o2 .getProtocol ())) * 10 +
1074
- o1 .getAddress ().compareTo (o2 .getAddress ());
1075
- }
1075
+ ports .stream () //
1076
+ .filter (port -> port .getProtocolLabel () == null || port .getProtocolLabel ().isEmpty ())
1077
+ .forEach (port -> {
1078
+ int labelIdx = PROTOCOLS_ORDER .indexOf (port .getProtocol ());
1079
+ if (labelIdx != -1 ) {
1080
+ port .setProtocolLabel (PROTOCOLS_LABELS .get (labelIdx ));
1081
+ } else {
1082
+ port .setProtocolLabel (port .getProtocol ());
1083
+ }
1084
+ });
1085
+
1086
+ Collections .sort (ports , (port1 , port2 ) -> {
1087
+ String pr1 = port1 .getProtocol ();
1088
+ String pr2 = port2 .getProtocol ();
1089
+ int prIdx1 = PROTOCOLS_ORDER .contains (pr1 ) ? PROTOCOLS_ORDER .indexOf (pr1 ) : 999 ;
1090
+ int prIdx2 = PROTOCOLS_ORDER .contains (pr2 ) ? PROTOCOLS_ORDER .indexOf (pr2 ) : 999 ;
1091
+ int r = prIdx1 - prIdx2 ;
1092
+ if (r != 0 )
1093
+ return r ;
1094
+ r = port1 .getProtocolLabel ().compareTo (port2 .getProtocolLabel ());
1095
+ if (r != 0 )
1096
+ return r ;
1097
+ return port1 .getAddress ().compareTo (port2 .getAddress ());
1076
1098
});
1077
1099
1078
- String lastProtocol = null ;
1079
- String lastProtocolTranslated ;
1100
+ String lastProtocol = "" ;
1101
+ String lastProtocolLabel = "" ;
1080
1102
for (BoardPort port : ports ) {
1081
- if (lastProtocol == null || !port .getProtocol ().equals (lastProtocol )) {
1082
- if (lastProtocol != null ) {
1103
+ if (! port . getProtocol (). equals ( lastProtocol ) || !port .getProtocolLabel ().equals (lastProtocolLabel )) {
1104
+ if (! lastProtocol . isEmpty () ) {
1083
1105
portMenu .addSeparator ();
1084
1106
}
1085
1107
lastProtocol = port .getProtocol ();
1086
-
1087
- if (BOARD_PROTOCOLS_ORDER .indexOf (port .getProtocol ()) != -1 ) {
1088
- lastProtocolTranslated = BOARD_PROTOCOLS_ORDER_TRANSLATIONS .get (BOARD_PROTOCOLS_ORDER .indexOf (port .getProtocol ()));
1089
- } else {
1090
- lastProtocolTranslated = port .getProtocol ();
1091
- }
1092
- JMenuItem lastProtocolMenuItem = new JMenuItem (tr (lastProtocolTranslated ));
1093
- lastProtocolMenuItem .setEnabled (false );
1094
- portMenu .add (lastProtocolMenuItem );
1108
+ lastProtocolLabel = port .getProtocolLabel ();
1109
+ JMenuItem item = new JMenuItem (tr (lastProtocolLabel ));
1110
+ item .setEnabled (false );
1111
+ portMenu .add (item );
1095
1112
}
1096
1113
String address = port .getAddress ();
1097
1114
@@ -2403,9 +2420,9 @@ private void handleBoardInfo() {
2403
2420
for (BoardPort port : ports ) {
2404
2421
if (port .getAddress ().equals (selectedPort )) {
2405
2422
label = port .getBoardName ();
2406
- vid = port .getVID ( );
2407
- pid = port .getPID ( );
2408
- iserial = port .getISerial ( );
2423
+ vid = port .getPrefs (). get ( "vid" );
2424
+ pid = port .getPrefs (). get ( "pid" );
2425
+ iserial = port .getPrefs (). get ( "iserial" );
2409
2426
protocol = port .getProtocol ();
2410
2427
found = true ;
2411
2428
break ;
@@ -2575,12 +2592,12 @@ private void statusEmpty() {
2575
2592
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2576
2593
2577
2594
protected void onBoardOrPortChange () {
2578
- Map < String , String > boardPreferences = BaseNoGui .getBoardPreferences ();
2579
- if (boardPreferences != null )
2580
- lineStatus .setBoardName (boardPreferences . get ( "name" ));
2595
+ TargetBoard board = BaseNoGui .getTargetBoard ();
2596
+ if (board != null )
2597
+ lineStatus .setBoardName (board . getName ( ));
2581
2598
else
2582
2599
lineStatus .setBoardName ("-" );
2583
- lineStatus .setSerialPort (PreferencesData .get ("serial.port" ));
2600
+ lineStatus .setPort (PreferencesData .get ("serial.port" ));
2584
2601
lineStatus .repaint ();
2585
2602
}
2586
2603
0 commit comments