Skip to content

Commit c0709e5

Browse files
committed
Fixed some null pointers, readded icon. Added check for network
1 parent 437ef74 commit c0709e5

File tree

5 files changed

+75
-21
lines changed

5 files changed

+75
-21
lines changed

src/main/java/jcs/commandStation/JCSCommandStation.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,12 @@ public void switchPower(boolean on) {
396396
//Logger.trace("Switch Power " + (on ? "On" : "Off"));
397397
if (decoderController != null && !AWT_THREAD.equals(Thread.currentThread().getName())) {
398398
decoderController.power(on);
399-
} else if (decoderController != null) {
400-
executor.execute(() -> decoderController.power(on));
399+
} else {
400+
executor.execute(() -> {
401+
if (decoderController != null) {
402+
decoderController.power(on);
403+
}
404+
});
401405
}
402406
}
403407

src/main/java/jcs/commandStation/marklin/cs/MarklinCentralStationImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ CanDevice getGFP() {
261261

262262
InfoBean createInfoBean(Map<Integer, CanDevice> canDevices) {
263263
InfoBean ib = new InfoBean(commandStationBean);
264-
ib.setIpAddress(connection.getControllerAddress().getHostAddress());
264+
if (connection != null && connection.getControllerAddress() != null) {
265+
ib.setIpAddress(connection.getControllerAddress().getHostAddress());
266+
}
265267

266268
for (CanDevice d : canDevices.values()) {
267269
Logger.trace("Checking device: " + d);
@@ -560,8 +562,12 @@ public boolean power(boolean on) {
560562
public void disconnect() {
561563
Logger.trace("Start disconnecting...");
562564
//Stop all schedules
563-
measurementTimer.cancel();
564-
watchDogTimer.cancel();
565+
if (measurementTimer != null) {
566+
measurementTimer.cancel();
567+
}
568+
if (watchDogTimer != null) {
569+
watchDogTimer.cancel();
570+
}
565571
//Stop Threads
566572
if (executor != null) {
567573
executor.shutdown();

src/main/java/jcs/ui/settings/CommandStationDialog1.form

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
2323
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
2424
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
25-
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,88,0,0,4,59"/>
25+
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,88,0,0,4,89"/>
2626
</AuxValues>
2727

2828
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
@@ -133,6 +133,15 @@
133133
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="discoverBtnActionPerformed"/>
134134
</Events>
135135
</Component>
136+
<Component class="javax.swing.JButton" name="checkBtn">
137+
<Properties>
138+
<Property name="text" type="java.lang.String" value="Check"/>
139+
<Property name="toolTipText" type="java.lang.String" value="Check Connection"/>
140+
</Properties>
141+
<Events>
142+
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="checkBtnActionPerformed"/>
143+
</Events>
144+
</Component>
136145
<Component class="javax.swing.JRadioButton" name="networkRB">
137146
<Properties>
138147
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
@@ -466,7 +475,7 @@
466475
<Layout>
467476
<DimensionLayout dim="0">
468477
<Group type="103" groupAlignment="0" attributes="0">
469-
<EmptySpace min="0" pref="1083" max="32767" attributes="0"/>
478+
<EmptySpace min="0" pref="1113" max="32767" attributes="0"/>
470479
</Group>
471480
</DimensionLayout>
472481
<DimensionLayout dim="1">

src/main/java/jcs/ui/settings/CommandStationDialog1.java

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
import jcs.entities.CommandStationBean;
3232
import jcs.persistence.PersistenceFactory;
3333
import org.tinylog.Logger;
34-
import java.beans.PropertyChangeEvent;
35-
import java.beans.PropertyChangeListener;
3634
import java.net.InetAddress;
3735
import javax.swing.JDialog;
36+
import jcs.util.Ping;
3837

3938
/**
4039
*
@@ -148,6 +147,10 @@ private void initModels(CommandStationBean selected) {
148147
}
149148

150149
setComponents();
150+
if (CommandStationBean.ConnectionType.NETWORK == selectedCommandStation.getConnectionType() && selectedCommandStation.getIpAddress() != null && selectedCommandStation.getIpAddress().length() > 8) {
151+
executor.execute(() -> checkConnection(selectedCommandStation));
152+
}
153+
151154
//enableFields(false);
152155
//this.progressBar.setVisible(false);
153156
}
@@ -171,6 +174,7 @@ private void initComponents() {
171174
accessoryControllerLbl = new javax.swing.JLabel();
172175
feedbackProviderLbl = new javax.swing.JLabel();
173176
discoverBtn = new javax.swing.JButton();
177+
checkBtn = new javax.swing.JButton();
174178
networkRB = new javax.swing.JRadioButton();
175179
serialRB = new javax.swing.JRadioButton();
176180
feedbackCSPanel = new javax.swing.JPanel();
@@ -261,6 +265,15 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
261265
});
262266
mainCSPanel.add(discoverBtn);
263267

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+
264277
connectionTypeBG.add(networkRB);
265278
networkRB.setText("Network");
266279
networkRB.addActionListener(new java.awt.event.ActionListener() {
@@ -326,7 +339,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
326339
jPanel3.setLayout(jPanel3Layout);
327340
jPanel3Layout.setHorizontalGroup(
328341
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
329-
.addGap(0, 1083, Short.MAX_VALUE)
342+
.addGap(0, 1113, Short.MAX_VALUE)
330343
);
331344
jPanel3Layout.setVerticalGroup(
332345
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@@ -416,6 +429,12 @@ private void setComponents() {
416429
ipOrPortLbl.setText("ip Address:");
417430
}
418431

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+
419438
//no main controller feedback support, enable the secondary
420439
feedbackCB.setVisible(!selectedCommandStation.isFeedbackSupport());
421440
feedbackCB.setEnabled(!selectedCommandStation.isFeedbackSupport());
@@ -433,7 +452,9 @@ private void changeDefaultCommandStation(final CommandStationBean newDefault) {
433452

434453
private void persistCommandStation(final CommandStationBean commandStation) {
435454
PersistenceFactory.getService().persist(commandStation);
436-
setComponents();
455+
java.awt.EventQueue.invokeLater(() -> {
456+
setComponents();
457+
});
437458
}
438459

439460
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
494515
private void ipTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ipTFActionPerformed
495516
Logger.trace("ip Address: " + this.ipTF.getText());
496517
selectedCommandStation.setIpAddress(ipTF.getText());
518+
persistCommandStation(selectedCommandStation);
497519
}//GEN-LAST:event_ipTFActionPerformed
498520

499521
private void ipTFFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ipTFFocusLost
500522
Logger.trace("ip Address: " + this.ipTF.getText());
501523
selectedCommandStation.setIpAddress(ipTF.getText());
524+
persistCommandStation(selectedCommandStation);
502525
}//GEN-LAST:event_ipTFFocusLost
503526

504527
private void ipTFMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ipTFMouseExited
505528
Logger.trace("ip Address: " + this.ipTF.getText());
506529
selectedCommandStation.setIpAddress(ipTF.getText());
530+
persistCommandStation(selectedCommandStation);
507531
}//GEN-LAST:event_ipTFMouseExited
508532

509533
private void connectBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectBtnActionPerformed
510534
// TODO add your handling code here:
511535
}//GEN-LAST:event_connectBtnActionPerformed
512536

537+
private void checkBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBtnActionPerformed
538+
executor.execute(() -> checkConnection(selectedCommandStation));
539+
}//GEN-LAST:event_checkBtnActionPerformed
540+
513541
private InetAddress discover(final CommandStationBean commandStation) {
514542
final JOptionPane optionPane = new JOptionPane("Try to discovering a " + commandStation.getDescription(),
515543
JOptionPane.INFORMATION_MESSAGE,
@@ -529,15 +557,6 @@ private InetAddress discover(final CommandStationBean commandStation) {
529557
inetAddress = EcosConnectionFactory.discoverEcos();
530558
}
531559

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-
// });
541560
if (inetAddress != null) {
542561
Logger.trace("Discovered host " + inetAddress.getHostAddress() + " for " + commandStation.getDescription());
543562
commandStation.setIpAddress(inetAddress.getHostAddress());
@@ -546,12 +565,27 @@ private InetAddress discover(final CommandStationBean commandStation) {
546565
java.awt.EventQueue.invokeLater(() -> {
547566
setComponents();
548567
discoverDialog.setVisible(false);
549-
discoverDialog.dispose();
568+
discoverDialog.dispose();
550569
});
551570

552571
return inetAddress;
553572
}
554573

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+
555589
/**
556590
* @param args the command line arguments
557591
*/
@@ -585,6 +619,7 @@ public void windowClosing(java.awt.event.WindowEvent e) {
585619

586620
// Variables declaration - do not modify//GEN-BEGIN:variables
587621
private javax.swing.JLabel accessoryControllerLbl;
622+
private javax.swing.JButton checkBtn;
588623
private javax.swing.JComboBox<CommandStationBean> commandStationCB;
589624
private javax.swing.JLabel commandStationLbl;
590625
private javax.swing.JButton connectBtn;
309 Bytes
Loading

0 commit comments

Comments
 (0)