001package jmri.jmrix.loconet.pr3.swing;
002
003import java.awt.FlowLayout;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006import javax.swing.JButton;
007import javax.swing.JLabel;
008import jmri.jmrix.loconet.LnConstants;
009import jmri.jmrix.loconet.LocoNetListener;
010import jmri.jmrix.loconet.LocoNetMessage;
011import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Pane for downloading software updates to PRICOM products
017 *
018 * @author Bob Jacobsen Copyright (C) 2005
019 */
020public class Pr3SelectPane extends jmri.jmrix.loconet.swing.LnPanel implements LocoNetListener {
021
022    @Override
023    public String getHelpTarget() {
024        return "package.jmri.jmrix.loconet.pr3.swing.Pr3Select"; // NOI18N
025    }
026
027    @Override
028    public String getTitle() {
029        return getTitle(Bundle.getMessage("MenuItemPr3ModeSelect"));
030    }
031
032    public Pr3SelectPane() {
033
034        // first build GUI
035        setLayout(new FlowLayout());
036
037        JButton b = new JButton(Bundle.getMessage("ButtonPr2Mode"));
038        b.addActionListener(new ActionListener() {
039            @Override
040            public void actionPerformed(ActionEvent event) {
041                selectPR2mode();
042            }
043        });
044        add(b);
045
046        b = new JButton(Bundle.getMessage("ButtonMs100Mode"));
047        b.addActionListener(new ActionListener() {
048            @Override
049            public void actionPerformed(ActionEvent event) {
050                selectMS100mode();
051            }
052        });
053        add(b);
054        add(status);
055
056    }
057
058    @Override
059    public void initComponents(LocoNetSystemConnectionMemo memo) {
060        super.initComponents(memo);
061
062        // listen for LocoNet messages
063        if (memo.getLnTrafficController() != null) {
064            memo.getLnTrafficController().addLocoNetListener(~0, this);
065        } else {
066            log.error("No LocoNet connection available, can't function");
067        }
068
069        // request status
070        LocoNetMessage msg = new LocoNetMessage(2);
071        msg.setOpCode(LnConstants.OPC_GPBUSY);
072        memo.getLnTrafficController().sendLocoNetMessage(msg);
073    }
074
075    JLabel status = new JLabel(Bundle.getMessage("StatusUnknown"));
076
077    void selectPR2mode() {
078        // set to PR2 mode
079        status.setText(Bundle.getMessage("StatusPr2"));
080        LocoNetMessage msg = new LocoNetMessage(6);
081        msg.setOpCode(0xD3);
082        msg.setElement(1, 0x10);
083        msg.setElement(2, 1);  // set PR2
084        msg.setElement(3, 0);
085        msg.setElement(4, 0);
086        memo.getLnTrafficController().sendLocoNetMessage(msg);
087    }
088
089    void selectMS100mode() {
090        // set to MS100 mode
091        status.setText(Bundle.getMessage("StatusMs100"));
092        LocoNetMessage msg = new LocoNetMessage(6);
093        msg.setOpCode(0xD3);
094        msg.setElement(1, 0x10);
095        msg.setElement(2, 0);  // set MS100
096        msg.setElement(3, 0);
097        msg.setElement(4, 0);
098        memo.getLnTrafficController().sendLocoNetMessage(msg);
099    }
100
101    @Override
102    public void message(LocoNetMessage msg) {
103        if ((msg.getOpCode() == LnConstants.OPC_PEER_XFER)
104                && (msg.getElement(1) == 0x10)
105                && (msg.getElement(2) == 0x22)
106                && (msg.getElement(3) == 0x22)
107                && (msg.getElement(4) == 0x01)) {  // Digitrax form, check PR2/PR3 or MS100/PR3 mode
108            int mode = msg.getElement(8) & 0x0C;
109            if (mode == 0x00) {
110                // PR2 format
111                status.setText(Bundle.getMessage("StatusPr2"));
112            } else {
113                // MS100 format
114                status.setText(Bundle.getMessage("StatusMs100"));
115            }
116        }
117    }
118
119    private final static Logger log = LoggerFactory.getLogger(Pr3SelectPane.class);
120
121}