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