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