31
31
import jcs .entities .CommandStationBean ;
32
32
import jcs .persistence .PersistenceFactory ;
33
33
import org .tinylog .Logger ;
34
- import java .beans .PropertyChangeEvent ;
35
- import java .beans .PropertyChangeListener ;
36
34
import java .net .InetAddress ;
37
35
import javax .swing .JDialog ;
36
+ import jcs .util .Ping ;
38
37
39
38
/**
40
39
*
@@ -148,6 +147,10 @@ private void initModels(CommandStationBean selected) {
148
147
}
149
148
150
149
setComponents ();
150
+ if (CommandStationBean .ConnectionType .NETWORK == selectedCommandStation .getConnectionType () && selectedCommandStation .getIpAddress () != null && selectedCommandStation .getIpAddress ().length () > 8 ) {
151
+ executor .execute (() -> checkConnection (selectedCommandStation ));
152
+ }
153
+
151
154
//enableFields(false);
152
155
//this.progressBar.setVisible(false);
153
156
}
@@ -171,6 +174,7 @@ private void initComponents() {
171
174
accessoryControllerLbl = new javax .swing .JLabel ();
172
175
feedbackProviderLbl = new javax .swing .JLabel ();
173
176
discoverBtn = new javax .swing .JButton ();
177
+ checkBtn = new javax .swing .JButton ();
174
178
networkRB = new javax .swing .JRadioButton ();
175
179
serialRB = new javax .swing .JRadioButton ();
176
180
feedbackCSPanel = new javax .swing .JPanel ();
@@ -261,6 +265,15 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
261
265
});
262
266
mainCSPanel .add (discoverBtn );
263
267
268
+ checkBtn .setText ("Check" );
269
+ checkBtn .setToolTipText ("Check Connection" );
270
+ checkBtn .addActionListener (new java .awt .event .ActionListener () {
271
+ public void actionPerformed (java .awt .event .ActionEvent evt ) {
272
+ checkBtnActionPerformed (evt );
273
+ }
274
+ });
275
+ mainCSPanel .add (checkBtn );
276
+
264
277
connectionTypeBG .add (networkRB );
265
278
networkRB .setText ("Network" );
266
279
networkRB .addActionListener (new java .awt .event .ActionListener () {
@@ -326,7 +339,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
326
339
jPanel3 .setLayout (jPanel3Layout );
327
340
jPanel3Layout .setHorizontalGroup (
328
341
jPanel3Layout .createParallelGroup (javax .swing .GroupLayout .Alignment .LEADING )
329
- .addGap (0 , 1083 , Short .MAX_VALUE )
342
+ .addGap (0 , 1113 , Short .MAX_VALUE )
330
343
);
331
344
jPanel3Layout .setVerticalGroup (
332
345
jPanel3Layout .createParallelGroup (javax .swing .GroupLayout .Alignment .LEADING )
@@ -416,6 +429,12 @@ private void setComponents() {
416
429
ipOrPortLbl .setText ("ip Address:" );
417
430
}
418
431
432
+ checkBtn .setVisible (CommandStationBean .ConnectionType .NETWORK == selectedCommandStation .getConnectionType () && selectedCommandStation .getIpAddress () != null && selectedCommandStation .getIpAddress ().length () > 8 );
433
+
434
+ if (selectedCommandStation .getIpAddress () == null || selectedCommandStation .getIpAddress ().length () > 8 ) {
435
+ ipTF .setBackground (new java .awt .Color (255 , 255 , 255 ));
436
+ }
437
+
419
438
//no main controller feedback support, enable the secondary
420
439
feedbackCB .setVisible (!selectedCommandStation .isFeedbackSupport ());
421
440
feedbackCB .setEnabled (!selectedCommandStation .isFeedbackSupport ());
@@ -433,7 +452,9 @@ private void changeDefaultCommandStation(final CommandStationBean newDefault) {
433
452
434
453
private void persistCommandStation (final CommandStationBean commandStation ) {
435
454
PersistenceFactory .getService ().persist (commandStation );
436
- setComponents ();
455
+ java .awt .EventQueue .invokeLater (() -> {
456
+ setComponents ();
457
+ });
437
458
}
438
459
439
460
private void discoverBtnActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_discoverBtnActionPerformed
@@ -494,22 +515,29 @@ private void serialRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
494
515
private void ipTFActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_ipTFActionPerformed
495
516
Logger .trace ("ip Address: " + this .ipTF .getText ());
496
517
selectedCommandStation .setIpAddress (ipTF .getText ());
518
+ persistCommandStation (selectedCommandStation );
497
519
}//GEN-LAST:event_ipTFActionPerformed
498
520
499
521
private void ipTFFocusLost (java .awt .event .FocusEvent evt ) {//GEN-FIRST:event_ipTFFocusLost
500
522
Logger .trace ("ip Address: " + this .ipTF .getText ());
501
523
selectedCommandStation .setIpAddress (ipTF .getText ());
524
+ persistCommandStation (selectedCommandStation );
502
525
}//GEN-LAST:event_ipTFFocusLost
503
526
504
527
private void ipTFMouseExited (java .awt .event .MouseEvent evt ) {//GEN-FIRST:event_ipTFMouseExited
505
528
Logger .trace ("ip Address: " + this .ipTF .getText ());
506
529
selectedCommandStation .setIpAddress (ipTF .getText ());
530
+ persistCommandStation (selectedCommandStation );
507
531
}//GEN-LAST:event_ipTFMouseExited
508
532
509
533
private void connectBtnActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_connectBtnActionPerformed
510
534
// TODO add your handling code here:
511
535
}//GEN-LAST:event_connectBtnActionPerformed
512
536
537
+ private void checkBtnActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_checkBtnActionPerformed
538
+ executor .execute (() -> checkConnection (selectedCommandStation ));
539
+ }//GEN-LAST:event_checkBtnActionPerformed
540
+
513
541
private InetAddress discover (final CommandStationBean commandStation ) {
514
542
final JOptionPane optionPane = new JOptionPane ("Try to discovering a " + commandStation .getDescription (),
515
543
JOptionPane .INFORMATION_MESSAGE ,
@@ -529,15 +557,6 @@ private InetAddress discover(final CommandStationBean commandStation) {
529
557
inetAddress = EcosConnectionFactory .discoverEcos ();
530
558
}
531
559
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
560
if (inetAddress != null ) {
542
561
Logger .trace ("Discovered host " + inetAddress .getHostAddress () + " for " + commandStation .getDescription ());
543
562
commandStation .setIpAddress (inetAddress .getHostAddress ());
@@ -546,12 +565,27 @@ private InetAddress discover(final CommandStationBean commandStation) {
546
565
java .awt .EventQueue .invokeLater (() -> {
547
566
setComponents ();
548
567
discoverDialog .setVisible (false );
549
- discoverDialog .dispose ();
568
+ discoverDialog .dispose ();
550
569
});
551
570
552
571
return inetAddress ;
553
572
}
554
573
574
+ private void checkConnection (final CommandStationBean commandStation ) {
575
+ String ip = commandStation .getIpAddress ();
576
+ boolean canConnect = Ping .IsReachable (ip );
577
+
578
+ java .awt .EventQueue .invokeLater (() -> {
579
+ if (canConnect ) {
580
+ ipTF .setBackground (new java .awt .Color (204 , 255 , 204 ));
581
+ } else {
582
+ ipTF .setBackground (new java .awt .Color (255 , 255 , 255 ));
583
+
584
+ JOptionPane .showMessageDialog (this , "Can't connect with host " + ip , "Can't Connect" , JOptionPane .WARNING_MESSAGE );
585
+ }
586
+ });
587
+ }
588
+
555
589
/**
556
590
* @param args the command line arguments
557
591
*/
@@ -585,6 +619,7 @@ public void windowClosing(java.awt.event.WindowEvent e) {
585
619
586
620
// Variables declaration - do not modify//GEN-BEGIN:variables
587
621
private javax .swing .JLabel accessoryControllerLbl ;
622
+ private javax .swing .JButton checkBtn ;
588
623
private javax .swing .JComboBox <CommandStationBean > commandStationCB ;
589
624
private javax .swing .JLabel commandStationLbl ;
590
625
private javax .swing .JButton connectBtn ;
0 commit comments