001package jmri.jmrix.xpa.swing.xpaconfig;
002
003import java.awt.FlowLayout;
004import javax.swing.BoxLayout;
005import javax.swing.JLabel;
006import javax.swing.JPanel;
007import jmri.jmrix.xpa.XpaMessage;
008import jmri.jmrix.xpa.XpaSystemConnectionMemo;
009
010/**
011 * Frame for configuring an XPA using a modem.
012 *
013 * @author Paul Bender Copyright (C) 2004
014 */
015public class XpaConfigureFrame extends jmri.util.JmriJFrame implements jmri.jmrix.xpa.XpaListener {
016
017    // member declarations
018    final XpaSystemConnectionMemo memo;
019
020    // Drop down box and button to set XpressNet address
021    final javax.swing.JComboBox<String> addrBox = new javax.swing.JComboBox<>();
022    final javax.swing.JButton setAddr = new javax.swing.JButton();
023
024    // Buttons to set the function of the phone's zero button (is it 
025    // emergency stop or emenrgency off?
026    final javax.swing.JRadioButton zeroEmergencyOff = new javax.swing.JRadioButton();
027    final javax.swing.JRadioButton zeroEmergencyStop = new javax.swing.JRadioButton();
028    final javax.swing.JButton setZero = new javax.swing.JButton();
029
030    // Drop down box and button to set duration of a momentary 
031    // function
032    final javax.swing.JComboBox<String> functionBox = new javax.swing.JComboBox<>();
033    final javax.swing.JButton setFunction = new javax.swing.JButton();
034
035    protected final String[] validTimes = new String[]{"0.2s", "0.4s", "0.6s", "0.8s", "1.0s", "1.2s", "1.4s", "1.6s", "1.8s", "2.0s"};
036    protected final int[] validTimeValues = new int[]{50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60};
037
038    protected final String[] validXNetAddresses = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
039
040    // Button to send a reset to the XPA
041    final javax.swing.JButton xpaReset = new javax.swing.JButton();
042
043    public XpaConfigureFrame(XpaSystemConnectionMemo m) {
044        super();
045        setTitle(Bundle.getMessage("MenuItemXpaConfigTool"));
046        memo = m;
047    }
048
049    @Override
050    public void initComponents() {
051        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
052
053        // First set up the pane for address selection.
054        JPanel pane0 = new JPanel();
055        pane0.setLayout(new FlowLayout());
056        pane0.add(new JLabel(Bundle.getMessage("XpAddressLabel")));
057        pane0.add(addrBox);
058        pane0.add(setAddr);
059        pane0.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
060        getContentPane().add(pane0);
061
062        // Next, set up the pane that determines what the zero 
063        // button does
064        JPanel pane1 = new JPanel();
065        pane1.setLayout(new FlowLayout());
066        pane1.add(new JLabel(Bundle.getMessage("ZeroButtonLabel")));
067        pane1.add(zeroEmergencyOff);
068        pane1.add(zeroEmergencyStop);
069        pane1.add(setZero);
070        pane1.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
071        getContentPane().add(pane1);
072
073        // add the pane that sets the momentary function durration
074        JPanel pane2 = new JPanel();
075        pane2.setLayout(new FlowLayout());
076        pane2.add(new JLabel(Bundle.getMessage("MomentaryDurLabel")));
077        pane2.add(functionBox);
078        pane2.add(setFunction);
079        pane2.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
080        getContentPane().add(pane2);
081
082        // add the reset button.
083        getContentPane().add(xpaReset);
084
085        // Initialzie the components
086        setAddr.setText(Bundle.getMessage("ButtonSetAddress"));
087        setAddr.setVisible(true);
088
089        setAddr.addActionListener(this::setAddrActionPerformed);
090
091        // Initilize the Address Components
092        addrBox.setVisible(true);
093        addrBox.setToolTipText(Bundle.getMessage("SetAddressToolTip"));
094        for (String validXNetAddress : validXNetAddresses) {
095            addrBox.addItem(validXNetAddress);
096        }
097        addrBox.setSelectedIndex(0);
098
099        // Initilize the function buttons for the zero key 
100        // settings.
101        zeroEmergencyOff.setText(Bundle.getMessage("XNetCSStatusEmergencyOff"));
102        zeroEmergencyOff.setSelected(true);
103
104        zeroEmergencyOff.addActionListener(this::zeroEmergencyOffActionPerformed);
105
106        zeroEmergencyStop.setText(Bundle.getMessage("XNetCSStatusEmergencyStop"));
107        zeroEmergencyStop.setSelected(false);
108
109        zeroEmergencyStop.addActionListener(this::zeroEmergencyStopActionPerformed);
110
111        setZero.setText(Bundle.getMessage("ButtonSetZero"));
112
113        setZero.addActionListener(this::setZeroActionPerformed);
114
115        // Initilize the Function durration components
116        setFunction.setText(Bundle.getMessage("ButtonSetDuration"));
117        setFunction.setVisible(true);
118
119        setFunction.addActionListener(this::setFunctionActionPerformed);
120
121        functionBox.setVisible(true);
122        functionBox.setToolTipText(Bundle.getMessage("SetDurationToolTip"));
123        for (String validTime : validTimes) {
124            functionBox.addItem(validTime);
125        }
126        functionBox.setSelectedIndex(0);
127
128        // Initilize the reset button
129        xpaReset.setText(Bundle.getMessage("ButtonReset"));
130        xpaReset.setVisible(true);
131
132        xpaReset.addActionListener(this::xpaResetActionPerformed);
133
134        // pack for display
135        pack();
136    }
137
138    public void setAddrActionPerformed(java.awt.event.ActionEvent e) {
139        // The first address available for the XPA is 1, so we 
140        // have to add 1 to the selected index to get the correct value.
141        XpaMessage m = XpaMessage.getDeviceSettingMsg(addrBox.getSelectedIndex() + 1);
142        memo.getXpaTrafficController().sendXpaMessage(m, this);
143    }
144
145    public void setFunctionActionPerformed(java.awt.event.ActionEvent e) {
146        XpaMessage m = XpaMessage.getDeviceSettingMsg(validTimeValues[functionBox.getSelectedIndex()]);
147        memo.getXpaTrafficController().sendXpaMessage(m, this);
148    }
149
150    public void xpaResetActionPerformed(java.awt.event.ActionEvent e) {
151        XpaMessage m = XpaMessage.getDeviceSettingMsg(99);
152        memo.getXpaTrafficController().sendXpaMessage(m, this);
153    }
154
155    public void zeroEmergencyOffActionPerformed(java.awt.event.ActionEvent e) {
156        // toggle from Emergency Stop to Emergency Off
157        zeroEmergencyOff.setSelected(true);
158        zeroEmergencyStop.setSelected(false);
159    }
160
161    public void zeroEmergencyStopActionPerformed(java.awt.event.ActionEvent e) {
162        // toggle from Emergency Off to Emergency Stop
163        zeroEmergencyStop.setSelected(true);
164        zeroEmergencyOff.setSelected(false);
165    }
166
167    public void setZeroActionPerformed(java.awt.event.ActionEvent e) {
168        XpaMessage m;
169        if (zeroEmergencyOff.isSelected()) {
170            m = XpaMessage.getDeviceSettingMsg(40);
171        } else {
172            m = XpaMessage.getDeviceSettingMsg(41);
173        }
174        memo.getXpaTrafficController().sendXpaMessage(m, this);
175    }
176
177    @Override
178    public void message(XpaMessage m) {
179    }  // ignore replies
180
181    @Override
182    public void reply(XpaMessage r) {
183    } // ignore replies
184
185}