001package jmri.jmrix.lenz.swing.liusb;
002
003import java.awt.FlowLayout;
004import javax.swing.BoxLayout;
005import javax.swing.JButton;
006import javax.swing.JComboBox;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JToggleButton;
010import jmri.jmrix.lenz.XNetConstants;
011import jmri.jmrix.lenz.XNetListener;
012import jmri.jmrix.lenz.XNetMessage;
013import jmri.jmrix.lenz.XNetReply;
014import jmri.jmrix.lenz.XNetTrafficController;
015import org.slf4j.Logger;
016import org.slf4j.LoggerFactory;
017
018/**
019 * Frame displaying the LIUSB configuration utility.
020 * <p>
021 * This is a configuration utility for the LIUSB. It allows the user to set the
022 * XpressNet Address and the port speed used to communicate with the LIUSB.
023 *
024 * @author Paul Bender Copyright (C) 2009-2010
025 */
026public class LIUSBConfigFrame extends jmri.util.JmriJFrame implements XNetListener {
027
028    protected XNetTrafficController tc;
029
030    public LIUSBConfigFrame(jmri.jmrix.lenz.XNetSystemConnectionMemo memo) {
031        super(Bundle.getMessage("MenuItemLIUSBConfigurationManager"));
032        tc = memo.getXNetTrafficController();
033        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
034
035        JPanel pane0 = new JPanel();
036        pane0.setLayout(new FlowLayout());
037        pane0.add(new JLabel(Bundle.getMessage("XNetAddressLabel")));
038        pane0.add(addrBox);
039        pane0.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
040        getContentPane().add(pane0);
041
042        JPanel pane2 = new JPanel();
043        pane2.add(readSettingsButton);
044        pane2.add(writeSettingsButton);
045        pane2.add(resetButton);
046        resetButton.setToolTipText(Bundle.getMessage("ResetDefaultsToolTip"));
047        pane2.add(closeButton);
048        getContentPane().add(pane2);
049
050        // Initilize the Combo Boxes
051        addrBox.setVisible(true);
052        addrBox.setToolTipText(Bundle.getMessage("XNetAddressToolTip"));
053        for (String validXNetAddress : validXNetAddresses) {
054            addrBox.addItem(validXNetAddress);
055        }
056        addrBox.setSelectedIndex(32);
057
058        status.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
059        getContentPane().add(status);
060
061        // and prep for display
062        pack();
063
064        // install read settings, write settings button handlers
065        readSettingsButton.addActionListener(a -> readLIUSBSettings());
066
067        writeSettingsButton.addActionListener(a -> writeLIUSBSettings());
068
069        // install close button handler
070        closeButton.addActionListener(a -> {
071            setVisible(false);
072            dispose();
073        });
074
075        // install reset button handler
076        resetButton.addActionListener(a -> resetLIUSBSettings());
077
078        if (tc != null) {
079            tc.addXNetListener(~0, this);
080        } else {
081            log.warn("No XpressNet connection, so panel won't function");
082        }
083    }
084
085    boolean read = false;
086
087    final JComboBox<String> addrBox = new javax.swing.JComboBox<>();
088
089    final JLabel status = new JLabel("    ");
090
091    final JToggleButton readSettingsButton = new JToggleButton(Bundle.getMessage("LIUSBReadButton"));
092    final JToggleButton writeSettingsButton = new JToggleButton(Bundle.getMessage("LIUSBWriteButton"));
093    final JButton closeButton = new JButton(Bundle.getMessage("ButtonClose"));
094    final JButton resetButton = new JButton(Bundle.getMessage("ButtonResetDefaults"));
095
096    protected final String[] validXNetAddresses = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8",
097            "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
098            "24", "25", "26", "27", "28", "29", "30", "31", ""};
099
100    /**
101     * Send new address to LIUSB.
102     */
103    void writeLIUSBSettings() {
104        if (!(addrBox.getSelectedItem().equals(""))
105                && addrBox.getSelectedItem() != null) {
106            /* we take care of generating an address request */
107            XNetMessage msg = XNetMessage.getLIAddressRequestMsg(
108                    addrBox.getSelectedIndex());
109            //Then send to the controller
110            tc.sendXNetMessage(msg, this);
111        }
112    }
113
114    /**
115     * Send Information request to LI-USB.
116     */
117    void readLIUSBSettings() {
118        /* we request setting an out of range address 
119         to get the current value. */
120        XNetMessage msg = XNetMessage.getLIAddressRequestMsg(32);
121        //Then send to the controller
122        tc.sendXNetMessage(msg, this);
123    }
124
125    /**
126     * Listen for responces from the LI-USB.
127     */
128    @Override
129    public void message(XNetReply l) {
130        // Check to see if this is an LI101 info request messgage, if it
131        //is, determine if it's the baud rate setting, or the address
132        //setting
133        if (l.getElement(0) == XNetConstants.LI101_REQUEST) {
134            if (l.getElement(1) == XNetConstants.LI101_REQUEST_ADDRESS) {
135                // The third element is the address
136                addrBox.setSelectedIndex(l.getElement(2));
137                status.setText(Bundle.getMessage("LIUSBReceivedStatus", l.getElement(2)));
138            }
139        }
140    }
141
142    /**
143     * Listen for the messages to the LI-USB.
144     */
145    @Override
146    public void message(XNetMessage l) {
147    }
148
149    /**
150     * Handle a timeout notification.
151     */
152    @Override
153    public void notifyTimeout(XNetMessage msg) {
154        log.debug("Notified of timeout on message {}", msg.toString());
155    }
156
157    /**
158     * For now, reset just reset the screen to factory defaults.
159     * Default values is 30 for the address
160     */
161    void resetLIUSBSettings() {
162        addrBox.setSelectedIndex(30);
163    }
164
165    private static final Logger log = LoggerFactory.getLogger(LIUSBConfigFrame.class);
166
167}