@@ -15,34 +15,75 @@ import java.io.FileWriter
1515import java.io.IOException
1616import java.util.*
1717import javax.swing.*
18+ import javax.swing.event.PopupMenuEvent
19+ import javax.swing.event.PopupMenuListener
20+
21+ data class PortElement (val portPath : String , val portInfo : String ) {
22+ override fun toString (): String {
23+ if (portInfo == " Unknown" )
24+ return portPath
25+ return " $portInfo ($portPath )"
26+ }
27+ }
1828
1929open class StartScreen (config : DebuggerConfig ) : AboutScreen(config) {
2030 init {
2131 setLocationRelativeTo(null )
2232 defaultCloseOperation = EXIT_ON_CLOSE
2333 }
2434
25- override fun addOptions (mainPanel : JPanel ) {
26- val portComboBox = JComboBox <String >().apply {
27- setAlignmentX(CENTER_ALIGNMENT )
28- maximumSize = Dimension (250 , 500 )
29- for (port in SerialPort .getCommPorts()) {
30- addItem(port.systemPortPath)
35+ fun updatePortOptions (comboBox : JComboBox <PortElement >) {
36+ comboBox.removeAllItems()
37+ for (port in SerialPort .getCommPorts()) {
38+ comboBox.addItem(PortElement (port.systemPortPath, port.portDescription))
39+ }
40+ }
41+
42+ /* *
43+ * If the config file specifies a port to use, use that one. Otherwise, try to find a WARDuino microcontroller.
44+ */
45+ fun determinePreferredOption (comboBox : JComboBox <PortElement >) {
46+ if (config.port != null ) {
47+ comboBox.selectedItem = config.port
48+ }
49+ else {
50+ var i = 0
51+ while (i < comboBox.itemCount && ! comboBox.getItemAt(i).portInfo.startsWith(" WARDuino" )) {
52+ i++
3153 }
32- if (config.port != null ) {
33- selectedItem = config.port
54+ if (i < comboBox.itemCount ) {
55+ comboBox. selectedItem = comboBox.getItemAt(i)
3456 }
3557 }
58+ }
59+
60+ override fun addOptions (mainPanel : JPanel ) {
61+ val portComboBox = JComboBox <PortElement >().apply {
62+ prototypeDisplayValue = PortElement (" " , " " )
63+ setAlignmentX(CENTER_ALIGNMENT )
64+ maximumSize = Dimension (Integer .MAX_VALUE , 500 )
65+ updatePortOptions(this )
66+ determinePreferredOption(this )
67+ addPopupMenuListener(object : PopupMenuListener {
68+ override fun popupMenuWillBecomeVisible (e : PopupMenuEvent ? ) {
69+ val currentItem = selectedItem as PortElement
70+ updatePortOptions(this @apply)
71+ selectedItem = currentItem
72+ }
73+
74+ override fun popupMenuWillBecomeInvisible (e : PopupMenuEvent ? ) {}
75+
76+ override fun popupMenuCanceled (e : PopupMenuEvent ? ) {}
77+ })
78+ }
3679 val portBox = Box .createHorizontalBox()
3780 portBox.border = BorderFactory .createEmptyBorder(10 , 20 , 0 , 20 )
81+ portBox.add(JLabel (" Port: " ))
3882 portBox.add(portComboBox)
3983 portBox.add(JButton (FlatSVGIcon (javaClass.getResource(" /refresh.svg" ))).apply {
4084 addActionListener {
41- val currentItem = portComboBox.selectedItem as String
42- portComboBox.removeAllItems()
43- for (port in SerialPort .getCommPorts()) {
44- portComboBox.addItem(port.systemPortPath) // TODO: We can use the device name CONFIG_USB_DEVICE_PRODUCT
45- }
85+ val currentItem = portComboBox.selectedItem as PortElement
86+ updatePortOptions(portComboBox)
4687 portComboBox.selectedItem = currentItem
4788 }
4889 })
@@ -67,7 +108,7 @@ open class StartScreen(config: DebuggerConfig) : AboutScreen(config) {
67108 if (chooser.showOpenDialog(this ) == JFileChooser .APPROVE_OPTION ) {
68109 recentProperties.setProperty(" lastDir" , chooser.selectedFile.parent)
69110 recentProperties.store(FileWriter (recentConfig), null )
70- if (! startDebugger(chooser.selectedFile, emulatorCheckbox.isSelected, portComboBox.selectedItem as String? )) {
111+ if (! startDebugger(chooser.selectedFile, emulatorCheckbox.isSelected, ( portComboBox.selectedItem as PortElement ).portPath )) {
71112 return @addActionListener
72113 }
73114 isVisible = false
0 commit comments