001package jmri.jmrit.ussctc;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import javax.swing.BoxLayout;
006import javax.swing.JButton;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JTextField;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * User interface frame for creating and editing "OS Indicator" logic on USS CTC
015 * machines.
016 *
017 * @see jmri.jmrit.ussctc.OsIndicator
018 * @see jmri.jmrit.ussctc.OsIndicatorFrame
019 * @see jmri.jmrit.ussctc.OsIndicatorAction
020 * @author Bob Jacobsen Copyright (C) 2007
021 */
022public class OsIndicatorPanel extends BasePanel {
023
024    JTextField outputName;
025    JTextField sensorName;
026    JTextField lockName;
027
028    JButton viewButton;
029    JButton addButton;
030
031    public OsIndicatorPanel() {
032
033        JPanel p2xs = new JPanel();
034        JLabel label;
035        p2xs.setLayout(new BoxLayout(p2xs, BoxLayout.Y_AXIS));
036
037        // add output name field
038        JPanel p3 = new JPanel();
039
040        label = new JLabel(Bundle.getMessage("LabelOutputName"));  // NOI18N
041        label.setToolTipText(Bundle.getMessage("ToolTipOsIndicatorOutput"));  // NOI18N
042        p3.add(label);
043        outputName = new JTextField(12);
044        outputName.setToolTipText(Bundle.getMessage("ToolTipOsIndicatorOutput"));  // NOI18N
045        p3.add(outputName);
046        p2xs.add(p3);
047
048        // add sensor name field
049        p3 = new JPanel();
050        label = new JLabel(Bundle.getMessage("LabelSensorName"));  // NOI18N
051        label.setToolTipText(Bundle.getMessage("ToolTipOsIndicatorSensor"));  // NOI18N
052        p3.add(label);
053        sensorName = new JTextField(12);
054        sensorName.setToolTipText(Bundle.getMessage("ToolTipOsIndicatorSensor"));  // NOI18N
055        p3.add(sensorName);
056        p2xs.add(p3);
057
058        // add lock name field
059        p3 = new JPanel();
060        label = new JLabel(Bundle.getMessage("LabelLockName"));
061        label.setToolTipText(Bundle.getMessage("ToolTipOsIndicatorLock"));  // NOI18N
062        p3.add(label);
063        lockName = new JTextField(12);
064        lockName.setToolTipText(Bundle.getMessage("ToolTipOsIndicatorLock"));  // NOI18N
065        p3.add(lockName);
066        p2xs.add(p3);
067
068        add(p2xs);
069
070        // buttons
071        p2xs = new JPanel();
072        p2xs.setLayout(new BoxLayout(p2xs, BoxLayout.Y_AXIS));
073
074        viewButton = new JButton(Bundle.getMessage("ButtonView"));  // NOI18N
075        viewButton.addActionListener(new ActionListener() {
076            @Override
077            public void actionPerformed(ActionEvent e) {
078                viewPressed();
079            }
080        });
081        p2xs.add(viewButton);
082        addButton = new JButton(Bundle.getMessage("ButtonAddUpdate"));  // NOI18N
083        addButton.addActionListener(new ActionListener() {
084            @Override
085            public void actionPerformed(ActionEvent e) {
086                addPressed();
087            }
088        });
089        p2xs.add(addButton);
090        add(p2xs);
091    }
092
093    void addPressed() {
094        boolean ok = true;
095        // validate
096        ok &= validateTurnout(outputName.getText());
097        ok &= validateSensor(sensorName.getText());
098        if (!lockName.getText().isEmpty()) {
099            ok &= validateMemory(lockName.getText());
100        }
101
102        // no errors?
103        if (!ok) {
104            return;
105        }
106        // create
107        new OsIndicator(outputName.getText(), sensorName.getText(), lockName.getText())
108                .instantiate();
109    }
110
111    void viewPressed() {
112        try {
113            OsIndicator o = new OsIndicator(outputName.getText());
114            sensorName.setText(o.getOsSensorName());
115            lockName.setText(o.getLockName());
116        } catch (jmri.JmriException e) {
117            log.error("Exception trying to find existing OS Indicator", e);  // NOI18N
118        }
119    }
120
121    private final static Logger log = LoggerFactory.getLogger(OsIndicatorPanel.class);
122
123}