16
16
package jcs .ui .settings ;
17
17
18
18
import com .fazecast .jSerialComm .SerialPort ;
19
- import java .net .InetAddress ;
20
19
import java .util .ArrayList ;
21
20
import java .util .List ;
22
21
import java .util .concurrent .ExecutorService ;
23
22
import java .util .concurrent .Executors ;
24
23
import javax .swing .ComboBoxModel ;
25
24
import javax .swing .DefaultComboBoxModel ;
25
+ import javax .swing .JOptionPane ;
26
26
import javax .swing .UIManager ;
27
27
import javax .swing .UnsupportedLookAndFeelException ;
28
28
import jcs .JCS ;
29
+ import jcs .commandStation .esu .ecos .net .EcosConnectionFactory ;
29
30
import jcs .commandStation .marklin .cs .net .CSConnectionFactory ;
30
31
import jcs .entities .CommandStationBean ;
31
32
import jcs .persistence .PersistenceFactory ;
32
33
import org .tinylog .Logger ;
34
+ import java .beans .PropertyChangeEvent ;
35
+ import java .beans .PropertyChangeListener ;
36
+ import java .net .InetAddress ;
37
+ import javax .swing .JDialog ;
33
38
34
39
/**
35
40
*
@@ -48,10 +53,9 @@ public class CommandStationDialog1 extends javax.swing.JDialog {
48
53
49
54
private CommandStationBean emptyCS ;
50
55
private CommandStationBean emptyFB ;
51
-
56
+
52
57
private static final String MARKLIN_CS = "marklin.cs" ;
53
58
private static final String ESU_ECOS = "esu-ecos" ;
54
-
55
59
56
60
/**
57
61
* Creates new form CommandStationDialog1
@@ -138,7 +142,7 @@ private void initModels(CommandStationBean selected) {
138
142
networkRB .setSelected (true );
139
143
}
140
144
141
- if (CommandStationBean .ConnectionType .SERIAL == selectedFeedbackProvider .getConnectionType ()) {
145
+ if (selectedFeedbackProvider . getConnectionType () != null && CommandStationBean .ConnectionType .SERIAL == selectedFeedbackProvider .getConnectionType ()) {
142
146
String port = selectedFeedbackProvider .getSerialPort ();
143
147
fbpSerialCB .setSelectedItem (port );
144
148
}
@@ -434,6 +438,7 @@ private void persistCommandStation(final CommandStationBean commandStation) {
434
438
435
439
private void discoverBtnActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_discoverBtnActionPerformed
436
440
Logger .trace ("Try to discover " + selectedCommandStation .getDescription ());
441
+ executor .execute (() -> discover (selectedCommandStation ));
437
442
}//GEN-LAST:event_discoverBtnActionPerformed
438
443
439
444
private void feedbackCBActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_feedbackCBActionPerformed
@@ -472,7 +477,7 @@ private void networkRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
472
477
selectedCommandStation .setConnectionType (CommandStationBean .ConnectionType .NETWORK );
473
478
selectedCommandStation .setSerialPort (null );
474
479
} else {
475
- selectedCommandStation .setConnectionType (CommandStationBean .ConnectionType .SERIAL );
480
+ selectedCommandStation .setConnectionType (CommandStationBean .ConnectionType .SERIAL );
476
481
}
477
482
persistCommandStation (selectedCommandStation );
478
483
}//GEN-LAST:event_networkRBActionPerformed
@@ -487,36 +492,66 @@ private void serialRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
487
492
}//GEN-LAST:event_serialRBActionPerformed
488
493
489
494
private void ipTFActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_ipTFActionPerformed
490
- Logger .trace ("ip Address: " + this .ipTF .getText ());
495
+ Logger .trace ("ip Address: " + this .ipTF .getText ());
491
496
selectedCommandStation .setIpAddress (ipTF .getText ());
492
497
}//GEN-LAST:event_ipTFActionPerformed
493
498
494
499
private void ipTFFocusLost (java .awt .event .FocusEvent evt ) {//GEN-FIRST:event_ipTFFocusLost
495
- Logger .trace ("ip Address: " + this .ipTF .getText ());
500
+ Logger .trace ("ip Address: " + this .ipTF .getText ());
496
501
selectedCommandStation .setIpAddress (ipTF .getText ());
497
502
}//GEN-LAST:event_ipTFFocusLost
498
503
499
504
private void ipTFMouseExited (java .awt .event .MouseEvent evt ) {//GEN-FIRST:event_ipTFMouseExited
500
- Logger .trace ("ip Address: " + this .ipTF .getText ());
505
+ Logger .trace ("ip Address: " + this .ipTF .getText ());
501
506
selectedCommandStation .setIpAddress (ipTF .getText ());
502
507
}//GEN-LAST:event_ipTFMouseExited
503
508
504
509
private void connectBtnActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_connectBtnActionPerformed
505
510
// TODO add your handling code here:
506
511
}//GEN-LAST:event_connectBtnActionPerformed
507
512
508
-
509
- private void discover () {
510
- //Start een wacht dialog box...
511
- if (MARKLIN_CS .equals (selectedCommandStation .getId ())) {
512
- InetAddress csAddress = CSConnectionFactory .discoverCs ();
513
- } else if (ESU_ECOS .equals (selectedCommandStation .getId ())) {
514
-
513
+ private InetAddress discover (final CommandStationBean commandStation ) {
514
+ final JOptionPane optionPane = new JOptionPane ("Try to discovering a " + commandStation .getDescription (),
515
+ JOptionPane .INFORMATION_MESSAGE ,
516
+ JOptionPane .OK_OPTION );
517
+
518
+ final JDialog discoverDialog = new JDialog (this , "Discovering..." );
519
+ discoverDialog .setContentPane (optionPane );
520
+ discoverDialog .setDefaultCloseOperation (JDialog .DO_NOTHING_ON_CLOSE );
521
+ discoverDialog .pack ();
522
+ discoverDialog .setLocationRelativeTo (null );
523
+ discoverDialog .setVisible (true );
524
+
525
+ InetAddress inetAddress = null ;
526
+ if (MARKLIN_CS .equals (commandStation .getId ())) {
527
+ inetAddress = CSConnectionFactory .discoverCs ();
528
+ } else if (ESU_ECOS .equals (commandStation .getId ())) {
529
+ inetAddress = EcosConnectionFactory .discoverEcos ();
530
+ }
531
+
532
+ // optionPane.addPropertyChangeListener((PropertyChangeEvent e) -> {
533
+ // String prop = e.getPropertyName();
534
+ //
535
+ // if (discoverDialog.isVisible()
536
+ // && (e.getSource() == optionPane)
537
+ // && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
538
+ // discoverDialog.setVisible(false);
539
+ // }
540
+ // });
541
+ if (inetAddress != null ) {
542
+ Logger .trace ("Discovered host " + inetAddress .getHostAddress () + " for " + commandStation .getDescription ());
543
+ commandStation .setIpAddress (inetAddress .getHostAddress ());
515
544
}
545
+
546
+ java .awt .EventQueue .invokeLater (() -> {
547
+ setComponents ();
548
+ discoverDialog .setVisible (false );
549
+ discoverDialog .dispose ();
550
+ });
551
+
552
+ return inetAddress ;
516
553
}
517
-
518
-
519
-
554
+
520
555
/**
521
556
* @param args the command line arguments
522
557
*/
0 commit comments