3535import java .awt .datatransfer .DataFlavor ;
3636import java .awt .datatransfer .Transferable ;
3737import java .awt .event .ActionEvent ;
38- import java .awt .event .ActionListener ;
3938import java .awt .event .InputEvent ;
4039import java .awt .event .KeyEvent ;
4140import java .awt .event .WindowAdapter ;
@@ -983,21 +982,6 @@ private void addInternalTools(JMenu menu) {
983982 }
984983
985984
986- class SerialMenuListener implements ActionListener {
987-
988- private final String serialPort ;
989-
990- public SerialMenuListener (String serialPort ) {
991- this .serialPort = serialPort ;
992- }
993-
994- public void actionPerformed (ActionEvent e ) {
995- selectSerialPort (serialPort );
996- base .onBoardOrPortChange ();
997- }
998-
999- }
1000-
1001985 private void selectSerialPort (String name ) {
1002986 if (portMenu == null ) {
1003987 System .out .println (tr ("serialMenu is null" ));
@@ -1045,6 +1029,24 @@ private void selectSerialPort(String name) {
10451029 //System.out.println("set to " + get("serial.port"));
10461030 }
10471031
1032+ class BoardPortJCheckBoxMenuItem extends JCheckBoxMenuItem {
1033+ private BoardPort port ;
1034+
1035+ public BoardPortJCheckBoxMenuItem (BoardPort port ) {
1036+ super (port .getLabel ());
1037+ addActionListener (e -> {
1038+ selectSerialPort (port .getAddress ());
1039+ base .onBoardOrPortChange ();
1040+ });
1041+ this .port = port ;
1042+ }
1043+
1044+ @ Override
1045+ public String toString () {
1046+ // This is required for serialPrompt()
1047+ return port .getLabel ();
1048+ }
1049+ }
10481050
10491051 private void populatePortMenu () {
10501052 portMenu .removeAll ();
@@ -1081,10 +1083,9 @@ public int compare(BoardPort o1, BoardPort o2) {
10811083 portMenu .add (lastProtocolMenuItem );
10821084 }
10831085 String address = port .getAddress ();
1084- String label = port .getLabel ();
10851086
1086- JCheckBoxMenuItem item = new JCheckBoxMenuItem ( label , address . equals ( selectedPort ) );
1087- item .addActionListener ( new SerialMenuListener ( address ));
1087+ BoardPortJCheckBoxMenuItem item = new BoardPortJCheckBoxMenuItem ( port );
1088+ item .setSelected ( address . equals ( selectedPort ));
10881089 portMenu .add (item );
10891090 }
10901091
@@ -1955,10 +1956,10 @@ public boolean handleSaveAs() {
19551956
19561957
19571958 private boolean serialPrompt () {
1958- int count = portMenu . getItemCount ();
1959- Object [] names = new Object [ count ];
1960- for ( int i = 0 ; i < count ; i ++) {
1961- names [ i ] = portMenu .getItem (i ). getText ( );
1959+ List < BoardPortJCheckBoxMenuItem > items = new ArrayList <> ();
1960+ for ( int i = 0 ; i < portMenu . getItemCount (); i ++) {
1961+ if ( portMenu . getItem ( i ) instanceof BoardPortJCheckBoxMenuItem )
1962+ items . add (( BoardPortJCheckBoxMenuItem ) portMenu .getItem (i ));
19621963 }
19631964
19641965 String port = PreferencesData .get ("serial.port" );
@@ -1969,12 +1970,12 @@ private boolean serialPrompt() {
19691970 title = I18n .format (tr ("Serial port {0} not found." ), port );
19701971 }
19711972 String question = tr ("Retry the upload with another serial port?" );
1972- String result = (String ) JOptionPane
1973+ BoardPortJCheckBoxMenuItem result = (BoardPortJCheckBoxMenuItem ) JOptionPane
19731974 .showInputDialog (this , title + "\n " + question , title ,
1974- JOptionPane .PLAIN_MESSAGE , null , names , 0 );
1975+ JOptionPane .PLAIN_MESSAGE , null , items . toArray () , 0 );
19751976 if (result == null )
19761977 return false ;
1977- selectSerialPort ( result );
1978+ result . doClick ( );
19781979 base .onBoardOrPortChange ();
19791980 return true ;
19801981 }
0 commit comments