Skip to content

Commit 1179e1b

Browse files
committed
Locomotives are now refreshed in the main screen when added via locomotives dialog
1 parent f5b0313 commit 1179e1b

13 files changed

+200
-165
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ public final boolean connectInBackground() {
122122
executor.execute(() -> connect());
123123

124124
long now = System.currentTimeMillis();
125-
long timemax = now + 2000;
125+
long timemax = now + 3000;
126126

127-
boolean con;
127+
boolean con = false;
128128
synchronized (this) {
129-
con = decoderController.isConnected();
129+
if (decoderController != null) {
130+
con = decoderController.isConnected();
131+
}
130132
while (!con && timemax < now) {
131133
try {
132134
wait(500);

src/main/java/jcs/entities/SensorBean.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,11 @@ public boolean equalsId(Object obj) {
371371

372372
@Override
373373
public String toString() {
374-
//return name;
375-
return toLogString();
374+
return name;
375+
//return toLogString();
376376
}
377377

378378
public String toLogString() {
379-
// String ids;
380-
// if (id == null) {
381-
// ids = "(" + generateId() + ")";
382-
// } else {
383-
// ids = id;
384-
// }
385-
386379
return "SensorBean{"
387380
+ "id="
388381
+ id

src/main/java/jcs/persistence/H2PersistenceService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public FunctionBean getLocomotiveFunction(LocomotiveBean locomotive, Integer num
251251

252252
@Override
253253
public FunctionBean getLocomotiveFunction(Long locomotiveId, Integer number) {
254-
String commandStationId = getDefaultCommandStation().getId();
254+
//String commandStationId = getDefaultCommandStation().getId();
255255

256256
FunctionBean fb = database.where("locomotive_id=? and f_number=?", locomotiveId, number).first(FunctionBean.class);
257257
if (fb != null) {
@@ -970,15 +970,15 @@ public synchronized BlockBean persist(BlockBean block) {
970970
database.insert(block);
971971
}
972972

973-
changeSupport.firePropertyChange("data.blockr", prev, block);
973+
changeSupport.firePropertyChange("data.block", prev, block);
974974
return block;
975975
}
976976

977977
@Override
978978
public synchronized void remove(BlockBean block) {
979-
int rows = this.database.delete(block).getRowsAffected();
979+
int rows = database.delete(block).getRowsAffected();
980980
Logger.trace(rows + " rows deleted");
981-
changeSupport.firePropertyChange("data.sblock.deleted", block, null);
981+
changeSupport.firePropertyChange("data.block.deleted", block, null);
982982
}
983983

984984
@Override
@@ -990,7 +990,6 @@ public synchronized void removeAllBlocks() {
990990

991991
@Override
992992
public List<CommandStationBean> getCommandStations() {
993-
//List<CommandStationBean> commandStationBeans = database.where("enabled=true").results(CommandStationBean.class);
994993
List<CommandStationBean> commandStationBeans = database.results(CommandStationBean.class);
995994
return commandStationBeans;
996995
}

src/main/java/jcs/ui/DispatcherStatusPanel.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717

1818
import javax.swing.JPanel;
1919
import jcs.commandStation.events.RefreshEvent;
20-
import jcs.commandStation.events.RefreshEventListener;
2120
import jcs.ui.util.LocomotiveSelectionChangedListener;
2221

2322
/**
2423
*
2524
*/
26-
public class DispatcherStatusPanel extends JPanel implements RefreshEventListener {
25+
public class DispatcherStatusPanel extends JPanel { //implements RefreshEventListener {
2726

2827
private static final long serialVersionUID = 6158244271104499799L;
2928

@@ -43,15 +42,14 @@ public void refresh() {
4342
locomotiveTablePanel.refresh();
4443
}
4544

46-
@Override
47-
public void onChange(RefreshEvent event) {
48-
locomotiveTablePanel.onChange(event);
49-
}
45+
// @Override
46+
// public void onChange(RefreshEvent event) {
47+
// locomotiveTablePanel.onChange(event);
48+
// }
5049

5150
public void addLocomotiveSelectionChangeListener(LocomotiveSelectionChangedListener listener) {
5251
locomotiveTablePanel.addLocomotiveSelectionChangeListener(listener);
5352
dispatcherTablePanel.addLocomotiveSelectionChangeListener(listener);
54-
5553
}
5654

5755
public void removeLocomotiveSelectionChangeListener(LocomotiveSelectionChangedListener listener) {

src/main/java/jcs/ui/JCSFrame.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ private void showLocomotives() {
220220
locomotiveDialog.setLocationRelativeTo(null);
221221
}
222222
locomotiveDialog.setVisible(true);
223+
224+
//Should add a listener here?
225+
226+
223227
}
224228

225229
private void showAccessories() {
@@ -969,20 +973,16 @@ public void connect(boolean connect) {
969973
if (JCS.getJcsCommandStation() != null) {
970974
if (connect) {
971975
String ip = JCS.getJcsCommandStation().getCommandStationBean().getIpAddress();
976+
String name = JCS.getJcsCommandStation().getCommandStationBean().getDescription();
972977
if (Ping.IsReachable(ip)) {
973978
if ("AWT-EventQueue-0".equals(Thread.currentThread().getName())) {
974979
JCS.getJcsCommandStation().connectInBackground();
975980
} else {
976-
977981
JCS.getJcsCommandStation().connect();
978982
}
979983
} else {
980984
Logger.debug("Can't reach ip " + ip + "...");
981-
982-
983-
JOptionPane.showMessageDialog(this, "Can't connect. "+ip+" is not reachable.", "Can't Connect", JOptionPane.ERROR_MESSAGE, null);
984-
985-
985+
JOptionPane.showMessageDialog(this, "Can't connect to " + name + ", " + ip + " is not reachable.", "Can't Connect", JOptionPane.ERROR_MESSAGE, null);
986986
}
987987

988988
InfoBean info = JCS.getJcsCommandStation().getCommandStationInfo();
@@ -1207,6 +1207,14 @@ public void powerChanged(PowerEvent event) {
12071207
powerButton.setSelected(event.isPower());
12081208
}
12091209

1210+
public void refreshData() {
1211+
Logger.trace("Refresh data due to settings change...");
1212+
}
1213+
1214+
public void refreshLocomotives() {
1215+
this.dispatcherStatusPanel.refresh();
1216+
}
1217+
12101218

12111219
// Variables declaration - do not modify//GEN-BEGIN:variables
12121220
private JMenuItem aboutMI;

src/main/java/jcs/ui/KeyboardSensorPanel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javax.swing.BorderFactory;
2525
import javax.swing.ImageIcon;
2626
import javax.swing.JFrame;
27-
import javax.swing.JLabel;
2827
import javax.swing.JPanel;
2928
import javax.swing.UIManager;
3029
import javax.swing.UnsupportedLookAndFeelException;

src/main/java/jcs/ui/VNCPanel.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,20 @@
5353
* In hind site I think is a welcome addition, hance this is added into the main frame
5454
*
5555
*/
56-
public class VNCPanel extends javax.swing.JPanel {
57-
56+
public class VNCPanel extends JPanel {
57+
58+
private static final long serialVersionUID = -2967801064769591519L;
59+
5860
private Image lastFrame;
5961
private VernacularConfig config;
6062
private VernacularClient client;
6163
private static final int DEFAULT_VNC_PORT = 5900;
62-
64+
6365
public VNCPanel() {
6466
initComponents();
6567
initVnc();
6668
}
67-
69+
6870
private void initVnc() {
6971
try {
7072
if (JCS.getJcsCommandStation() != null) {
@@ -75,22 +77,23 @@ private void initVnc() {
7577
if (JCS.getJcsCommandStation().isConnected()) {
7678
String ip = JCS.getJcsCommandStation().getCommandStationInfo().getIpAddress();
7779
int port = DEFAULT_VNC_PORT;
78-
79-
if(Ping.IsReachable(ip)) {
80+
81+
if (Ping.IsReachable(ip)) {
8082
connect(ip, port);
8183
} else {
82-
Logger.trace("Can't reach "+ip+" ...");
83-
}
84+
Logger.trace("Can't reach " + ip + " ...");
85+
}
8486
}
8587
}
8688
}
8789
} catch (Exception e) {
8890
Logger.warn("Error during init " + e.getMessage());
8991
}
9092
}
91-
93+
9294
private void addDrawingSurface() {
9395
this.viewerPanel.add(new JPanel() {
96+
private static final long serialVersionUID = -1852494391952623917L;
9497
@Override
9598
public void paintComponent(Graphics g) {
9699
super.paintComponent(g);
@@ -112,7 +115,7 @@ public void paintComponent(Graphics g) {
112115
}
113116
}, CENTER);
114117
}
115-
118+
116119
private void initialiseVernacularClient() {
117120
config = new VernacularConfig();
118121
config.setColorDepth(BPP_24_TRUE);
@@ -128,27 +131,27 @@ private void initialiseVernacularClient() {
128131
config.setRemoteClipboardListener(t -> getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(t), null));
129132
//config.setUseLocalMousePointer(localCursorMenuItem.isSelected());
130133
config.setUseLocalMousePointer(true);
131-
134+
132135
config.setEnableCopyrectEncoding(true);
133136
config.setEnableRreEncoding(true);
134137
config.setEnableHextileEncoding(true);
135138
config.setEnableZLibEncoding(false);
136-
139+
137140
client = new VernacularClient(config);
138141
}
139-
142+
140143
private boolean resizeRequired(Image frame) {
141144
return lastFrame == null || lastFrame.getWidth(null) != frame.getWidth(null) || lastFrame.getHeight(null) != frame.getHeight(null);
142145
}
143-
146+
144147
private void renderFrame(Image frame) {
145148
if (resizeRequired(frame)) {
146149
resizeWindow(frame);
147150
}
148151
lastFrame = frame;
149152
repaint();
150153
}
151-
154+
152155
private void resizeWindow(Image frame) {
153156
int remoteWidth = frame.getWidth(null);
154157
int remoteHeight = frame.getHeight(null);
@@ -160,7 +163,7 @@ private void resizeWindow(Image frame) {
160163
int paddingTop = getHeight() - this.viewerPanel.getHeight();
161164
int paddingSides = getWidth() - this.viewerPanel.getWidth();
162165
int maxWidth = (int) screenSize.getWidth() - paddingSides;
163-
166+
164167
int maxHeight = (int) screenSize.getHeight() - paddingTop;
165168
if (remoteWidth <= maxWidth && remoteHeight < maxHeight) {
166169
setWindowSize(remoteWidth, remoteHeight);
@@ -171,40 +174,40 @@ private void resizeWindow(Image frame) {
171174
setWindowSize(scaledWidth, scaledHeight);
172175
}
173176
}
174-
177+
175178
private void setWindowSize(int width, int height) {
176179
this.viewerPanel.setPreferredSize(new Dimension(width, height));
177180
}
178-
181+
179182
private void resetUI() {
180183
setCursor(getDefaultCursor());
181184
lastFrame = null;
182185
repaint();
183186
}
184-
187+
185188
private boolean connected() {
186189
return client != null && client.isRunning();
187190
}
188-
191+
189192
private void connect(String host, int port) {
190193
lastFrame = null;
191194
client.start(host, port);
192195
}
193-
196+
194197
private void disconnect() {
195198
if (connected()) {
196199
client.stop();
197200
}
198201
resetUI();
199202
}
200-
203+
201204
private int scaleMouseX(int x) {
202205
if (lastFrame == null) {
203206
return x;
204207
}
205208
return (int) (x * ((double) lastFrame.getWidth(null) / this.viewerPanel.getWidth()));
206209
}
207-
210+
208211
private int scaleMouseY(int y) {
209212
if (lastFrame == null) {
210213
return y;
@@ -375,19 +378,19 @@ public static void main(String args[]) {
375378
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
376379
Logger.error("Can't set the LookAndFeel: " + ex);
377380
}
378-
381+
379382
java.awt.EventQueue.invokeLater(() -> {
380383
VNCPanel vncPanel = new VNCPanel();
381384
JFrame testFrame = new JFrame("VNCPanel Tester");
382-
385+
383386
URL iconUrl = KeyboardSensorPanel.class.getResource("/media/jcs-train-64.png");
384387
if (iconUrl != null) {
385388
testFrame.setIconImage(new ImageIcon(iconUrl).getImage());
386389
}
387-
390+
388391
JFrame.setDefaultLookAndFeelDecorated(true);
389392
testFrame.add(vncPanel);
390-
393+
391394
testFrame.addWindowListener(new java.awt.event.WindowAdapter() {
392395
@Override
393396
public void windowClosing(java.awt.event.WindowEvent e) {
@@ -402,5 +405,5 @@ public void windowClosing(java.awt.event.WindowEvent e) {
402405
testFrame.setVisible(true);
403406
});
404407
}
405-
408+
406409
}

src/main/java/jcs/ui/panel/DispatcherTablePanel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
9393
}
9494
}
9595

96+
public void refresh() {
97+
locomotiveDispatcherTableModel.refresh();
98+
}
99+
96100
/**
97101
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
98102
*/

0 commit comments

Comments
 (0)