001package jmri.jmrix.can.cbus.swing.console;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005import javax.swing.BorderFactory;
006import javax.swing.BoxLayout;
007import javax.swing.JButton;
008import javax.swing.JCheckBox;
009import jmri.jmrix.can.cbus.swing.CbusEventHighlightFrame;
010import jmri.jmrix.can.cbus.swing.CbusFilterFrame;
011import jmri.jmrix.can.cbus.swing.configtool.ConfigToolPane;
012import jmri.util.JmriJFrame;
013
014/**
015 * Frame for CBUS Console
016 *
017 * @author Andrew Crosland Copyright (C) 2008
018 * @author Steve Young Copyright (C) 2018
019 */
020public class CbusConsoleDisplayOptionsPane extends javax.swing.JPanel {
021
022    private final CbusConsolePane _mainPane;
023
024    private final JCheckBox showLogCheckBox;
025    private final JCheckBox showStatsCheckBox;
026    private final JCheckBox showPacketCheckBox;
027    private final JCheckBox showSendEventCheckBox;
028    public JButton filterButton;
029    public JButton highlightButton;
030    protected JButton evCaptureButton;
031
032    protected CbusFilterFrame filterFrame;
033    protected CbusEventHighlightFrame highlightFrame;
034    private ConfigToolPane _evCapFrame;
035    private JmriJFrame _ecf;
036    private final jmri.UserPreferencesManager p;
037
038    private static final String SHOW_LOG = ".ShowLog";
039    private static final String SHOW_STATS = ".ShowStats";
040    private static final String SHOW_PACKET = ".ShowPacket";
041    private static final String SHOW_SEND_EVENT = ".ShowSendEvent";
042
043    public CbusConsoleDisplayOptionsPane(CbusConsolePane mainPane){
044        super();
045        _mainPane = mainPane;
046        p = jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class);
047        showLogCheckBox = new JCheckBox(Bundle.getMessage("Logging"));
048        showStatsCheckBox = new JCheckBox(Bundle.getMessage("StatisticsTitle"));
049        showPacketCheckBox = new JCheckBox(Bundle.getMessage("ButtonShowPackets"));
050        showSendEventCheckBox = new JCheckBox(Bundle.getMessage("ButtonSendEvent"));
051        filterButton = new JButton(Bundle.getMessage("ButtonFilter"));
052        highlightButton = new JButton(Bundle.getMessage("ButtonHighlight"));
053        evCaptureButton = new JButton(Bundle.getMessage("CapConfigTitle"));
054        makePane();
055    }
056
057    private void makePane() {
058
059        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
060        setBorder(BorderFactory.createTitledBorder(
061            BorderFactory.createEtchedBorder(), Bundle.getMessage("Display")));
062
063        add(showLogCheckBox);        
064        add(showStatsCheckBox);
065        add(showPacketCheckBox);
066        add(showSendEventCheckBox);
067        add(filterButton);
068        add(highlightButton);
069        add(evCaptureButton);
070
071        setDefaults();
072        setToolTipText();
073        addListeners();
074    }
075
076    private void setDefaults() {
077        showLogCheckBox.setSelected(p.getSimplePreferenceState(getClass().getName() + SHOW_LOG));
078        showStatsCheckBox.setSelected(p.getSimplePreferenceState(getClass().getName() + SHOW_STATS));
079        showPacketCheckBox.setSelected(p.getSimplePreferenceState(getClass().getName() + SHOW_PACKET));
080        showSendEventCheckBox.setSelected(p.getSimplePreferenceState(getClass().getName() + SHOW_SEND_EVENT));
081    }
082
083    private void setToolTipText() {
084        showLogCheckBox.setToolTipText(Bundle.getMessage("LoggingTip"));
085        showStatsCheckBox.setToolTipText(Bundle.getMessage("ButtonShowStats"));
086        showPacketCheckBox.setToolTipText(Bundle.getMessage("TooltipShowPackets"));
087        showSendEventCheckBox.setToolTipText(Bundle.getMessage("TooltipShowEvents"));
088
089        filterButton.setToolTipText(Bundle.getMessage("TooltipFilter"));
090        highlightButton.setToolTipText(Bundle.getMessage("TooltipHighlighter"));
091    }
092
093    private void addListeners(){
094        showLogCheckBox.addActionListener(this::matchVisisbleToCheckBoxes);        
095        showStatsCheckBox.addActionListener(this::matchVisisbleToCheckBoxes);        
096        showPacketCheckBox.addActionListener(this::matchVisisbleToCheckBoxes);
097        showSendEventCheckBox.addActionListener(this::matchVisisbleToCheckBoxes);
098
099        filterButton.addActionListener(this::filterButtonActionPerformed);
100        highlightButton.addActionListener(this::highlightButtonActionPerformed);
101        evCaptureButton.addActionListener(this::evCaptureButtonActionPerformed);
102    }
103
104    // triggered by CbusConsolePane when all panes are available
105    public void matchVisisbleToCheckBoxes(ActionEvent e){
106        _mainPane.logPane.setVisible(showLogCheckBox.isSelected());
107        _mainPane.statsPane.setVisible(showStatsCheckBox.isSelected());
108        _mainPane.packetPane.setVisible(showPacketCheckBox.isSelected());
109        _mainPane.sendPane.setVisible(showSendEventCheckBox.isSelected());
110        _mainPane.validate();
111    }
112
113    public void filterButtonActionPerformed(ActionEvent e) {
114        // log.debug("Cbus Console filter button action performed");
115        if (filterFrame == null) {
116            filterFrame = new CbusFilterFrame(_mainPane,_evCapFrame);
117            filterFrame.initComponents();
118            if (_evCapFrame != null ) {
119                _evCapFrame.setFilter(filterFrame);
120            }
121        } else {
122            filterFrame.setState(Frame.NORMAL);
123        }
124        filterFrame.setVisible(true);
125    }
126
127    public void highlightButtonActionPerformed(ActionEvent e) {
128        // log.debug("Cbus Console filter button action performed");
129        if (highlightFrame == null) {
130            highlightFrame = new CbusEventHighlightFrame(_mainPane,_evCapFrame);
131            highlightFrame.initComponents();
132            if (_evCapFrame != null ) {
133                _evCapFrame.setHighlighter(highlightFrame);
134            }
135        } else {
136            highlightFrame.setState(Frame.NORMAL);
137        }
138        highlightFrame.setVisible(true);
139    }
140
141    public void evCaptureButtonActionPerformed(ActionEvent e) {
142        // log.debug("Cbus Console filter button action performed");
143        if (_evCapFrame == null ) {
144            _ecf = new JmriJFrame("Event Capture paired to " + _mainPane.getTitle() + " Filter and Highlighter");
145            _evCapFrame = new ConfigToolPane(_mainPane, filterFrame, highlightFrame);
146            _ecf.add(_evCapFrame);
147            _evCapFrame.initComponents(_mainPane.getMemo());
148            _ecf.pack();
149            _ecf.setState(Frame.NORMAL);
150        } else {
151            _ecf.setState(Frame.NORMAL);
152        }
153        _ecf.setVisible(true);
154    }
155
156    public void dispose() {
157    
158        if (highlightFrame != null) {
159            highlightFrame.dispose();
160            highlightFrame=null;
161        }
162        if (filterFrame != null) {
163            filterFrame.dispose();
164            filterFrame=null;
165        }
166        if (_ecf != null) {
167            _ecf.dispose();
168            _ecf=null;
169        }
170        p.setSimplePreferenceState(getClass().getName() + SHOW_LOG, showLogCheckBox.isSelected());
171        p.setSimplePreferenceState(getClass().getName() + SHOW_STATS, showStatsCheckBox.isSelected());
172        p.setSimplePreferenceState(getClass().getName() + SHOW_SEND_EVENT, showSendEventCheckBox.isSelected());
173        p.setSimplePreferenceState(getClass().getName() + SHOW_PACKET, showPacketCheckBox.isSelected());
174    }
175
176    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CbusConsoleDisplayOptionsPane.class);
177}