001package jmri.jmrit.simpleturnoutctrl; 002 003import java.awt.GridBagConstraints; 004import java.awt.GridBagLayout; 005import javax.swing.BorderFactory; 006import javax.swing.BoxLayout; 007import javax.swing.JPanel; 008import jmri.InstanceManager; 009import jmri.Turnout; 010 011/** 012 * Frame to control a single turnout. 013 * 014 * @author Bob Jacobsen Copyright (C) 2001 015 */ 016public class SimpleTurnoutCtrlFrame extends jmri.util.JmriJFrame implements java.beans.PropertyChangeListener { 017 018 private static final String LOCKED = Bundle.getMessage("Locked"); 019 private static final String UNLOCKED = Bundle.getMessage("Normal"); 020 021 // GUI member declarations 022 javax.swing.JTextField adrTextField = new javax.swing.JTextField(8); 023 024 javax.swing.JButton throwButton = new javax.swing.JButton(); 025 javax.swing.JButton closeButton = new javax.swing.JButton(); 026 027 javax.swing.JLabel nowStateLabel = new javax.swing.JLabel(); 028 029 javax.swing.JLabel nowFeedbackLabel = new javax.swing.JLabel(); 030 031 javax.swing.JLabel lockButtonLabel = new javax.swing.JLabel(); 032 javax.swing.JButton lockButton = new javax.swing.JButton(); 033 034 javax.swing.JLabel lockPushButtonLabel = new javax.swing.JLabel(); 035 javax.swing.JButton lockPushButton = new javax.swing.JButton(); 036 037 public SimpleTurnoutCtrlFrame() { 038 super(); 039 040 // configure items for GUI 041 adrTextField.setText(""); 042 adrTextField.setVisible(true); 043 adrTextField.setToolTipText(Bundle.getMessage("AddressToolTip")); 044 045 throwButton.setText(InstanceManager.turnoutManagerInstance().getThrownText()); 046 throwButton.setVisible(true); 047 throwButton.setToolTipText(Bundle.getMessage("ThrowButtonToolTip", 048 InstanceManager.turnoutManagerInstance().getThrownText())); 049 throwButton.addActionListener(new java.awt.event.ActionListener() { 050 @Override 051 public void actionPerformed(java.awt.event.ActionEvent e) { 052 throwButtonActionPerformed(e); 053 } 054 }); 055 056 closeButton.setText(InstanceManager.turnoutManagerInstance().getClosedText()); 057 closeButton.setVisible(true); 058 closeButton.setToolTipText(Bundle.getMessage("ThrowButtonToolTip", 059 InstanceManager.turnoutManagerInstance().getClosedText())); 060 closeButton.addActionListener(new java.awt.event.ActionListener() { 061 @Override 062 public void actionPerformed(java.awt.event.ActionEvent e) { 063 closeButtonActionPerformed(e); 064 } 065 }); 066 067 nowStateLabel.setText(Bundle.getMessage("BeanStateUnknown")); 068 nowStateLabel.setVisible(true); 069 070 nowFeedbackLabel.setText(Bundle.getMessage("BeanStateUnknown")); 071 nowFeedbackLabel.setVisible(true); 072 073 lockButtonLabel.setText(Bundle.getMessage("LockButtonLabel") + " "); 074 lockButtonLabel.setVisible(true); 075 076 lockButton.setText(UNLOCKED); 077 lockButton.setVisible(true); 078 lockButton.setEnabled(false); 079 lockButton.setToolTipText(Bundle.getMessage("LockButtonToolTip")); 080 lockButton.addActionListener(new java.awt.event.ActionListener() { 081 @Override 082 public void actionPerformed(java.awt.event.ActionEvent e) { 083 lockButtonActionPerformed(e); 084 } 085 }); 086 087 lockPushButtonLabel.setText(Bundle.getMessage("PushButtonLabel")); 088 lockPushButtonLabel.setVisible(true); 089 090 lockPushButton.setText(UNLOCKED); 091 lockPushButton.setVisible(true); 092 lockPushButton.setEnabled(false); 093 lockPushButton.setToolTipText(Bundle.getMessage("PushButtonToolTip")); 094 lockPushButton.addActionListener(new java.awt.event.ActionListener() { 095 @Override 096 public void actionPerformed(java.awt.event.ActionEvent e) { 097 lockPushButtonActionPerformed(e); 098 } 099 }); 100 // general GUI config 101 setTitle(Bundle.getMessage("FrameTitle")); 102 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 103 GridBagConstraints c = new GridBagConstraints(); 104 c.gridwidth = 1; 105 c.gridheight = 1; 106 c.weightx = 1.0; 107 108 // install items in GUI 109 JPanel tPanel = new JPanel(); 110 tPanel.setLayout(new GridBagLayout()); 111 tPanel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BeanNameTurnout") + " ")); 112 113 c.gridx = 0; 114 c.gridy = 0; 115 c.anchor = GridBagConstraints.EAST; 116 tPanel.add(adrTextField, c); 117 118 c.gridx = 0; 119 c.gridy = 1; 120 c.anchor = GridBagConstraints.EAST; 121 tPanel.add(throwButton, c); 122 c.gridx = 2; 123 c.anchor = GridBagConstraints.WEST; 124 tPanel.add(closeButton, c); 125 126 JPanel sPanel = new JPanel(); 127 sPanel.setLayout(new GridBagLayout()); 128 sPanel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("CurrentStateTitle") + " ")); 129 130 sPanel.add(nowStateLabel); 131 132 JPanel fPanel = new JPanel(); 133 fPanel.setLayout(new GridBagLayout()); 134 fPanel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("FeedbackModeTitle") + " ")); 135 136 fPanel.add(nowFeedbackLabel); 137 138 JPanel avPanel = new JPanel(); 139 avPanel.setLayout(new GridBagLayout()); 140 avPanel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("AdvancedFeaturesTitle") + " ")); 141 142 c.gridx = 0; 143 c.gridy = 5; 144 c.anchor = GridBagConstraints.EAST; 145 avPanel.add(lockButtonLabel, c); 146 c.gridx = 1; 147 c.anchor = GridBagConstraints.WEST; 148 avPanel.add(lockButton, c); 149 150 c.gridx = 0; 151 c.gridy = 6; 152 c.anchor = GridBagConstraints.EAST; 153 avPanel.add(lockPushButtonLabel, c); 154 c.gridx = 1; 155 c.anchor = GridBagConstraints.WEST; 156 avPanel.add(lockPushButton, c); 157 158 getContentPane().add(tPanel); 159 getContentPane().add(sPanel); 160 getContentPane().add(fPanel); 161 getContentPane().add(avPanel); 162 163 // add help menu to window 164 addHelpMenu("package.jmri.jmrit.simpleturnoutctrl.SimpleTurnoutCtrl", true); 165 166 pack(); 167 } 168 169 public void closeButtonActionPerformed(java.awt.event.ActionEvent e) { 170 // load address from switchAddrTextField 171 if (adrTextField.getText().length() < 1) { 172 nowStateLabel.setText(Bundle.getMessage("NoAddressHint")); 173 return; 174 } 175 try { 176 if (turnout != null) { 177 turnout.removePropertyChangeListener(this); 178 } 179 turnout = InstanceManager.turnoutManagerInstance().provideTurnout( 180 adrTextField.getText()); 181 182 turnout.addPropertyChangeListener(this); 183 184 log.debug("about to command CLOSED"); 185 // and set commanded state to CLOSED 186 turnout.setCommandedState(Turnout.CLOSED); 187 188 updateTurnoutStatusFields(); 189 190 } catch (IllegalArgumentException ex1) { 191 invalidTurnout(adrTextField.getText(), ex1); 192 } catch (Exception ex2) { 193 log.error("exception during closeButtonActionPerformed", ex2); // NOI18N 194 nowStateLabel.setText(Bundle.getMessage("ErrorTitle")); 195 nowFeedbackLabel.setText(Bundle.getMessage("BeanStateUnknown")); 196 } 197 } 198 199 public void throwButtonActionPerformed(java.awt.event.ActionEvent e) { 200 // load address from switchAddrTextField 201 if (adrTextField.getText().length() < 1) { 202 nowStateLabel.setText(Bundle.getMessage("NoAddressHint")); 203 return; 204 } 205 try { 206 if (turnout != null) { 207 turnout.removePropertyChangeListener(this); 208 } 209 turnout = InstanceManager.turnoutManagerInstance().provideTurnout( 210 adrTextField.getText()); 211 212 turnout.addPropertyChangeListener(this); 213 214 log.debug("about to command THROWN"); 215 // and set commanded state to THROWN 216 turnout.setCommandedState(Turnout.THROWN); 217 218 updateTurnoutStatusFields(); 219 220 } catch (IllegalArgumentException ex1) { 221 invalidTurnout(adrTextField.getText(), ex1); 222 } catch (Exception ex2) { 223 log.error("exception during throwButtonActionPerformed", ex2); // NOI18N 224 nowStateLabel.setText(Bundle.getMessage("ErrorTitle")); 225 nowFeedbackLabel.setText(Bundle.getMessage("BeanStateUnknown")); 226 } 227 } 228 229 public void lockButtonActionPerformed(java.awt.event.ActionEvent e) { 230 // load address from switchAddrTextField 231 try { 232 if (turnout != null) { 233 turnout.removePropertyChangeListener(this); 234 } 235 turnout = InstanceManager.turnoutManagerInstance().provideTurnout( 236 adrTextField.getText()); 237 238 turnout.addPropertyChangeListener(this); 239 updateTurnoutStatusFields(); 240 241 if (lockButton.getText().equals(LOCKED)) { 242 turnout.setLocked(Turnout.CABLOCKOUT, false); 243 } else if (turnout.canLock(Turnout.CABLOCKOUT)) { 244 turnout.setLocked(Turnout.CABLOCKOUT, true); 245 } 246 } catch (IllegalArgumentException ex1) { 247 invalidTurnout(adrTextField.getText(), ex1); 248 } catch (Exception ex2) { 249 log.error("exception during lockButtonActionPerformed", ex2); // NOI18N 250 nowStateLabel.setText(Bundle.getMessage("ErrorTitle")); 251 nowFeedbackLabel.setText(Bundle.getMessage("BeanStateUnknown")); 252 } 253 } 254 255 public void lockPushButtonActionPerformed(java.awt.event.ActionEvent e) { 256 // load address from switchAddrTextField 257 try { 258 if (turnout != null) { 259 turnout.removePropertyChangeListener(this); 260 } 261 262 turnout = InstanceManager.turnoutManagerInstance().provideTurnout( 263 adrTextField.getText()); 264 turnout.addPropertyChangeListener(this); 265 updateTurnoutStatusFields(); 266 267 if (lockPushButton.getText().equals(LOCKED)) { 268 turnout.setLocked(Turnout.PUSHBUTTONLOCKOUT, false); 269 } else if (turnout.canLock(Turnout.PUSHBUTTONLOCKOUT)) { 270 turnout.setLocked(Turnout.PUSHBUTTONLOCKOUT, true); 271 } 272 } catch (IllegalArgumentException ex1) { 273 invalidTurnout(adrTextField.getText(), ex1); 274 } catch (Exception ex2) { 275 log.error("exception during lockPushButtonActionPerformed", ex2); // NOI18N 276 nowStateLabel.setText(Bundle.getMessage("ErrorTitle")); 277 nowFeedbackLabel.setText(Bundle.getMessage("BeanStateUnknown")); 278 } 279 } 280 281 /** 282 * Update state field in GUI as state of turnout changes. 283 */ 284 @Override 285 public void propertyChange(java.beans.PropertyChangeEvent e) { 286 // If the Commanded State changes, show transition state as "<inconsistent>" 287 if (e.getPropertyName().equals("CommandedState")) { // NOI18N 288 nowStateLabel.setText(Bundle.getMessage("BeanStateInconsistent")); 289 } 290 if (e.getPropertyName().equals("KnownState")) { // NOI18N 291 int now = ((Integer) e.getNewValue()).intValue(); 292 switch (now) { 293 case Turnout.UNKNOWN: 294 nowStateLabel.setText(Bundle.getMessage("BeanStateUnknown")); 295 return; 296 case Turnout.CLOSED: 297 nowStateLabel.setText(InstanceManager.turnoutManagerInstance().getClosedText()); 298 return; 299 case Turnout.THROWN: 300 nowStateLabel.setText(InstanceManager.turnoutManagerInstance().getThrownText()); 301 return; 302 default: 303 nowStateLabel.setText(Bundle.getMessage("BeanStateInconsistent")); 304 return; 305 } 306 } 307 if (e.getPropertyName().equals("locked")) { // NOI18N 308 if (turnout.canLock(Turnout.CABLOCKOUT)) { 309 if (turnout.getLocked(Turnout.CABLOCKOUT)) { 310 lockButton.setText(LOCKED); 311 } else { 312 lockButton.setText(UNLOCKED); 313 } 314 lockButton.setEnabled(true); 315 } else { 316 lockButton.setText(UNLOCKED); 317 lockButton.setEnabled(false); 318 } 319 if (turnout.canLock(Turnout.PUSHBUTTONLOCKOUT)) { 320 if (turnout.getLocked(Turnout.PUSHBUTTONLOCKOUT)) { 321 lockPushButton.setText(LOCKED); 322 } else { 323 lockPushButton.setText(UNLOCKED); 324 } 325 lockPushButton.setEnabled(true); 326 } else { 327 lockPushButton.setText(UNLOCKED); 328 lockPushButton.setEnabled(false); 329 } 330 } 331 if (e.getPropertyName().equals("feedbackchange")) { // NOI18N 332 updateTurnoutStatusFields(); 333 } 334 } 335 336 private void updateTurnoutStatusFields() { 337 338 nowFeedbackLabel.setText(turnout.getFeedbackModeName()); 339 if (turnout.canLock(Turnout.CABLOCKOUT)) { 340 if (turnout.getLocked(Turnout.CABLOCKOUT)) { 341 lockButton.setText(LOCKED); 342 } else { 343 lockButton.setText(UNLOCKED); 344 } 345 lockButton.setEnabled(true); 346 } else { 347 lockButton.setText(UNLOCKED); 348 lockButton.setEnabled(false); 349 } 350 if (turnout.canLock(Turnout.PUSHBUTTONLOCKOUT)) { 351 if (turnout.getLocked(Turnout.PUSHBUTTONLOCKOUT)) { 352 lockPushButton.setText(LOCKED); 353 } else { 354 lockPushButton.setText(UNLOCKED); 355 } 356 lockPushButton.setEnabled(true); 357 } else { 358 lockPushButton.setText(UNLOCKED); 359 lockPushButton.setEnabled(false); 360 } 361 int knownState = turnout.getKnownState(); 362 switch (knownState) { 363 case Turnout.UNKNOWN: 364 nowStateLabel.setText(Bundle.getMessage("BeanStateUnknown")); 365 return; 366 case Turnout.CLOSED: 367 nowStateLabel.setText(InstanceManager.turnoutManagerInstance().getClosedText()); 368 return; 369 case Turnout.THROWN: 370 nowStateLabel.setText(InstanceManager.turnoutManagerInstance().getThrownText()); 371 return; 372 default: 373 nowStateLabel.setText(Bundle.getMessage("BeanStateInconsistent")); 374 return; 375 } 376 } 377 378 void invalidTurnout(String name, Exception ex) { 379 jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class) 380 .showErrorMessage(this, Bundle.getMessage("ErrorTitle"), 381 (Bundle.getMessage("ErrorConvertHW", name)), 382 ex.toString(), "", true, false); 383 } 384 385 Turnout turnout = null; 386 String newState = ""; 387 388 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SimpleTurnoutCtrlFrame.class); 389 390}