001package jmri.jmrix.rps.swing.polling;
002
003import java.awt.Dimension;
004import java.awt.FlowLayout;
005import java.awt.event.ActionEvent;
006import java.awt.event.ActionListener;
007import java.io.File;
008import javax.swing.Box;
009import javax.swing.BoxLayout;
010import javax.swing.ButtonGroup;
011import javax.swing.JButton;
012import javax.swing.JCheckBox;
013import javax.swing.JComboBox;
014import javax.swing.JLabel;
015import javax.swing.JPanel;
016import javax.swing.JRadioButton;
017import javax.swing.JScrollPane;
018import javax.swing.JTable;
019import javax.swing.JTextField;
020import javax.swing.SortOrder;
021import javax.swing.table.TableCellEditor;
022import javax.swing.table.TableRowSorter;
023import jmri.jmrix.rps.Engine;
024import jmri.jmrix.rps.PollingFile;
025import jmri.swing.RowSorterUtil;
026import jmri.util.table.ButtonEditor;
027import jmri.util.table.ButtonRenderer;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Pane for user management of RPS polling.
033 *
034 * @author Bob Jacobsen Copyright (C) 2008
035 */
036public class PollTablePane extends javax.swing.JPanel {
037
038    PollDataModel pollModel = null;
039    jmri.ModifiedFlag modifiedFlag;
040
041    /**
042     * Constructor method
043     * @param flag the ModifiedFlag tag.
044     */
045    public PollTablePane(jmri.ModifiedFlag flag) {
046        super();
047
048        this.modifiedFlag = flag;
049
050        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
051
052        pollModel = new PollDataModel(modifiedFlag);
053
054        JTable pollTable = new JTable(pollModel);
055
056        // install a button renderer & editor
057        ButtonRenderer buttonRenderer = new ButtonRenderer();
058        pollTable.setDefaultRenderer(JButton.class, buttonRenderer);
059        TableCellEditor buttonEditor = new ButtonEditor(new JButton());
060        pollTable.setDefaultEditor(JButton.class, buttonEditor);
061        pollTable.setDefaultRenderer(JComboBox.class, new jmri.jmrit.symbolicprog.ValueRenderer());
062        pollTable.setDefaultEditor(JComboBox.class, new jmri.jmrit.symbolicprog.ValueEditor());
063
064        TableRowSorter<PollDataModel> sorter = new TableRowSorter<>(pollModel);
065        RowSorterUtil.setSortOrder(sorter, PollDataModel.ADDRCOL, SortOrder.ASCENDING);
066        pollTable.setRowSorter(sorter);
067        pollTable.setRowSelectionAllowed(false);
068        pollTable.setPreferredScrollableViewportSize(new java.awt.Dimension(580, 80));
069
070        JScrollPane scrollPane = new JScrollPane(pollTable);
071        add(scrollPane);
072
073        // status info on bottom
074        JPanel p = new JPanel() {
075            @Override
076            public Dimension getMaximumSize() {
077                int height = getPreferredSize().height;
078                int width = super.getMaximumSize().width;
079                return new Dimension(width, height);
080            }
081        };
082        p.setLayout(new FlowLayout());
083
084        polling = new JCheckBox(Bundle.getMessage("LabelPoll"));
085        polling.setSelected(Engine.instance().getPolling());
086        polling.setToolTipText(Bundle.getMessage("PollToolTip"));
087        p.add(polling);
088        polling.addActionListener(new ActionListener() {
089            @Override
090            public void actionPerformed(ActionEvent event) {
091                modifiedFlag.setModifiedFlag(true);
092                checkPolling();
093            }
094        });
095
096        JPanel m = new JPanel();
097        m.setLayout(new BoxLayout(m, BoxLayout.Y_AXIS));
098        ButtonGroup g = new ButtonGroup();
099        bscMode = new JRadioButton(Bundle.getMessage("LabelBscMode"));
100        bscMode.setSelected(Engine.instance().getBscPollMode());
101        m.add(bscMode);
102        g.add(bscMode);
103        bscMode.addActionListener(new ActionListener() {
104            @Override
105            public void actionPerformed(ActionEvent event) {
106                modifiedFlag.setModifiedFlag(true);
107                checkMode();
108            }
109        });
110        directMode = new JRadioButton(Bundle.getMessage("LabelDirectMode"));
111        directMode.setSelected(Engine.instance().getDirectPollMode());
112        m.add(directMode);
113        g.add(directMode);
114        directMode.addActionListener(new ActionListener() {
115            @Override
116            public void actionPerformed(ActionEvent event) {
117                modifiedFlag.setModifiedFlag(true);
118                checkMode();
119            }
120        });
121        throttleMode = new JRadioButton(Bundle.getMessage("LabelThrottleMode"));
122        throttleMode.setSelected(Engine.instance().getThrottlePollMode());
123        m.add(throttleMode);
124        g.add(throttleMode);
125        throttleMode.addActionListener(new ActionListener() {
126            @Override
127            public void actionPerformed(ActionEvent event) {
128                modifiedFlag.setModifiedFlag(true);
129                checkMode();
130            }
131        });
132        p.add(m);
133
134        p.add(Box.createHorizontalGlue());
135        p.add(new JLabel(Bundle.getMessage("LabelDelay")));
136        delay = new JTextField(5);
137        delay.setText("" + Engine.instance().getPollingInterval());
138        delay.setToolTipText(Bundle.getMessage("IntervalToolTip"));
139        p.add(delay);
140        delay.addActionListener(new ActionListener() {
141            @Override
142            public void actionPerformed(ActionEvent event) {
143                modifiedFlag.setModifiedFlag(true);
144                updateInterval();
145            }
146        });
147
148        JButton b = new JButton(Bundle.getMessage("LabelSetDefault"));
149        b.addActionListener(new ActionListener() {
150            @Override
151            public void actionPerformed(ActionEvent event) {
152                modifiedFlag.setModifiedFlag(true);
153                setDefaults();
154            }
155        });
156        p.add(b);
157
158        add(p);
159    }
160
161    JTextField delay;
162    JCheckBox polling;
163    JRadioButton bscMode;
164    JRadioButton directMode;
165    JRadioButton throttleMode;
166
167    /**
168     * Save the default value file
169     */
170    void setDefaults() {
171        try {
172            File file = new File(PollingFile.defaultFilename());
173            log.info("located file {} for store", file);
174            // handle the file
175            Engine.instance().storePollConfig(file);
176            modifiedFlag.setModifiedFlag(false);
177        } catch (Exception e) {
178            log.error("exception during storeDefault: ", e);
179        }
180    }
181
182    /**
183     * Start or stop the polling
184     */
185    void checkPolling() {
186        Engine.instance().setPolling(polling.isSelected());
187    }
188
189    /**
190     * Change the polling mode
191     */
192    void checkMode() {
193        if (bscMode.isSelected()) {
194            Engine.instance().setBscPollMode();
195        } else if (throttleMode.isSelected()) {
196            Engine.instance().setThrottlePollMode();
197        } else {
198            Engine.instance().setDirectPollMode();
199        }
200    }
201
202    /**
203     * The requested interval has changed, update it
204     */
205    void updateInterval() {
206        int interval = Integer.parseInt(delay.getText());
207        log.debug("set interval to {}", interval);
208        Engine.instance().setPollingInterval(interval);
209    }
210
211    public void dispose() {
212        pollModel.dispose();
213    }
214
215    private final static Logger log = LoggerFactory.getLogger(PollTablePane.class);
216
217}