001package jmri.jmrix.can.cbus.swing.modules;
002
003import java.awt.*;
004import java.awt.event.ActionEvent;
005
006import javax.swing.*;
007import javax.swing.border.*;
008
009
010/**
011 * Class to display CBUS command station flag settings
012 *
013 * @author Andrew Crosland Copyright (C) 2022
014 */
015public class CmdStaFlags extends JPanel {
016        
017    protected int _index;
018    protected int _nv;
019    protected String _title;
020    protected int flags;
021    protected JRadioButton [] buttons;
022    protected UpdateNV _flagUpdateFn;
023
024    /**
025     * 
026     * @param index of the flags, not the NV array index which may be offset
027     * @param nv index of the associated NV
028     * @param title of the flags object
029     * @param flagStrings array of strings to name each flag bit
030     * @param flagTtStrings array of tooltip strings for each flag bit
031     * @param update the callback function to update the table data model
032     */
033    public CmdStaFlags(int index, int nv, String title, String [] flagStrings, String [] flagTtStrings, UpdateNV update) {
034        super();
035
036        _index = index;
037        _nv = nv;
038        _title = title;
039        _flagUpdateFn = update;
040
041        buttons = new JRadioButton[8];
042        for (int i = 0; i < 8; i++) {
043            buttons[i] = new JRadioButton(flagStrings[i]);
044            buttons[i].setToolTipText(flagTtStrings[i]);
045            buttons[i].addActionListener((ActionEvent e) -> {
046                flagActionListener();
047            });
048        }
049    }
050
051    /**
052     * Get the panel to display the flags
053     * 
054     * @return JPanel displaying the flags
055     */
056    public JPanel getContents() {
057
058        JPanel gridPane = new JPanel(new GridBagLayout());
059        GridBagConstraints c = new GridBagConstraints();
060        c.fill = GridBagConstraints.HORIZONTAL;
061        c.weightx = 1;
062        c.weighty = 1;
063        c.gridx = 0;
064        c.gridy = 0;
065
066        Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
067        TitledBorder t = BorderFactory.createTitledBorder(border, _title);
068        gridPane.setBorder(t);
069
070        for (int i = 0; i < 8; i++) {
071            gridPane.add(buttons[i], c);
072            c.gridy++;
073        }
074        setButtons();
075
076        return gridPane;
077    }
078
079    /**
080     * Call the callback to update from flags state.
081     */
082    protected void flagActionListener() {
083        int value = buttons[7].isSelected() ? 1 : 0;
084        for (int i = 6; i >= 0; i--) {
085            value = (value << 1) + (buttons[i].isSelected() ? 1 : 0);
086        }
087        setFlags(value);
088        _flagUpdateFn.setNewVal(_index);
089    }
090
091    /**
092     * Update the flags settings
093     * 
094     * @param value settings
095     */
096    public void setFlags(int value) {
097        flags = value;
098        setButtons();
099    }
100
101    /**
102     * Get the flags settings
103     * 
104     * @return flags as an int
105     */
106    public int getFlags() {
107        return flags;
108    }
109
110    /**
111     * Get the NV index associated with this object
112     * 
113     * @return the NV index
114     */
115    public int getNv() {
116        return _nv;
117    }
118    
119    /**
120     * Set the buttons to the state of the flags
121     */
122    protected void setButtons() {
123        for (int i = 0; i < 8; i++) {
124            if ((flags & (1<<i)) > 0) {
125                buttons[i].setSelected(true);
126            } else {
127                buttons[i].setSelected(false);
128            }
129        }
130    }
131
132}