001package jmri.jmrix.can.cbus.swing.modeswitcher;
002
003import java.awt.BorderLayout;
004import java.awt.event.ActionListener;
005
006import javax.swing.*;
007
008import jmri.jmrix.can.*;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Mode Switcher to switch programming track mode.
015 *
016 * @author Andrew Crosland Copyright (C) 2020
017 */
018public class SprogCbusSprog3PlusModeSwitcherFrame extends SprogCbusModeSwitcherFrame {
019    
020    public static final int PROG_OFF_MODE = 0; // Prog track off when not programming
021    public static final int PROG_ON_MODE = 1;  // Prog track follows main when not programming
022    public static final int PROG_AR_MODE = 2;  // Prog track is auto-reverse power district, no programming on progtrack
023
024    private JRadioButton progOffButton;
025    private JRadioButton progOnButton;
026    private JRadioButton progArButton;
027    
028    public SprogCbusSprog3PlusModeSwitcherFrame(CanSystemConnectionMemo memo) {
029        super(memo, Bundle.getMessage("SprogCbusPlusModeSwitcher"));
030    }
031    
032    
033    /**
034     * Display radio buttons to select programming track mode).
035     * <p>
036     * 
037     * {@inheritDoc}
038     */
039    @Override
040    public void initComponents() {
041        if (initSetup()) {
042            // Create selection buttons and set initial state
043            progOffButton = new JRadioButton(Bundle.getMessage("ProgOffMode"));
044            progOnButton = new JRadioButton(Bundle.getMessage("ProgOnMode"));
045            progArButton = new JRadioButton(Bundle.getMessage("ProgArMode"));
046            ButtonGroup buttons = new ButtonGroup();
047            buttons.add(progOffButton);
048            buttons.add(progOnButton);
049            buttons.add(progArButton);
050            
051            // Get current preferences
052            // It is expected that the saved preferences will usually match the hardware.
053            mode = PROG_OFF_MODE;
054            if (preferences.getProgTrackMode() == PROG_OFF_MODE) {
055                progOffButton.setSelected(true);
056                pm.setGlobalProgrammerAvailable(true);
057            } else if (preferences.getProgTrackMode() == PROG_ON_MODE) {
058                progOnButton.setSelected(true);
059                mode = PROG_ON_MODE;
060                pm.setGlobalProgrammerAvailable(true);
061            } else if (preferences.getProgTrackMode() == PROG_AR_MODE) {
062                progArButton.setSelected(true);
063                mode = PROG_AR_MODE;
064                pm.setGlobalProgrammerAvailable(false);
065            } else {
066                // Default if inconsistent preference
067                progOffButton.setSelected(true);
068                pm.setGlobalProgrammerAvailable(true);
069            }
070            // Reset hardware mode and preferences in case there was any inconsistency
071            setHardwareMode(mode);
072            preferences.setProgTrackMode(mode);
073
074            // Handle Programming track mode button activity
075            ActionListener listener = ae -> {
076                if (progOnButton.isSelected()) {
077                    log.info("Setting prog track on when not programming");
078                    mode = PROG_ON_MODE;
079                    setHardwareMode(mode);
080                    pm.setGlobalProgrammerAvailable(true);
081                } else if (progArButton.isSelected()) {
082                    log.info("Setting prog track to auto-reverse");
083                    mode = PROG_AR_MODE;
084                    setHardwareMode(mode);
085                    pm.setGlobalProgrammerAvailable(false);
086                } else {
087                    log.info("Setting prog track off when not programming");
088                    mode = PROG_OFF_MODE;
089                    setHardwareMode(mode);
090                    pm.setGlobalProgrammerAvailable(true);
091                }
092                preferences.setProgTrackMode(mode);
093            };
094
095            progOffButton.addActionListener(listener);
096            progOnButton.addActionListener(listener);
097            progArButton.addActionListener(listener);
098            modePane.add(progOffButton);
099            modePane.add(progOnButton);
100            modePane.add(progArButton);
101
102            panel.add(label, BorderLayout.NORTH);
103            panel.add(modePane, BorderLayout.CENTER);
104        }
105        
106        // add help menu to window
107        setHelp();
108
109        this.add(panel);
110        pack();
111        setVisible(true);
112    }
113    
114    
115    /**
116     * Define help menu for this window.
117     */
118    protected void setHelp() {
119        addHelpMenu("package.jmri.jmrix.can.cbus.swing.modeswitcher.SprogCbusSprog3PlusModeSwitcherFrame", true); // NOI18N
120    }
121
122    
123    /**
124     * disconnect from the CBUS
125     */
126    @Override
127    public void dispose() {
128        super.dispose();
129    }
130
131    
132    private final static Logger log = LoggerFactory.getLogger(SprogCbusSprog3PlusModeSwitcherFrame.class);
133    
134}