001package jmri.jmrix.can.cbus.swing;
002
003import javax.swing.BorderFactory;
004import javax.swing.BoxLayout;
005import javax.swing.event.ChangeEvent;
006import javax.swing.JCheckBox;
007import javax.swing.JFormattedTextField;
008import javax.swing.JPanel;
009import javax.swing.JSpinner;
010import javax.swing.SpinnerNumberModel;
011import javax.swing.text.DefaultFormatter;
012import jmri.jmrix.can.cbus.CbusConstants;
013
014/**
015 *
016 * @author Andrew Crosland
017 * @author Steve Young Copyright (C) 2018
018 */
019public class CbusEventHighlightPanel extends JPanel {
020    
021    private int _index;
022    private CbusEventHighlightFrame _highlightFrame;
023    private Boolean _isActive;
024    
025    protected JCheckBox evEnButton = new JCheckBox();
026    protected JCheckBox nnEnButton = new JCheckBox();
027    protected JSpinner eventnumberspinner = new JSpinner(new SpinnerNumberModel(1,1, 65535, 1));
028    protected JSpinner nodenumberspinner = new JSpinner(new SpinnerNumberModel(0,0, 65535, 1));
029
030    // Buttons to select event type
031    protected JCheckBox onButton = new JCheckBox();
032    protected JCheckBox offButton = new JCheckBox();
033    
034    protected JCheckBox inButton = new JCheckBox();
035    protected JCheckBox outButton = new JCheckBox();
036
037    /**
038     * Create a new instance of CbusEventHighlightPanel.
039     * @param highlightFrame Parent Frame
040     * @param index Index position in parent Frame
041     */
042    public CbusEventHighlightPanel(CbusEventHighlightFrame highlightFrame, int index) {
043        super();
044        _index = index;
045        _highlightFrame = highlightFrame;
046    }
047
048    protected CbusEventHighlightPanel() {
049        super();
050    }
051
052    public void initComponents(int index) {
053        _isActive = false;
054        // Panels will be added across
055        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
056
057        // Pane to hold Event
058        JPanel evPane = new JPanel();
059        evPane.setLayout(new BoxLayout(evPane, BoxLayout.X_AXIS));
060        evPane.setBorder(BorderFactory.createTitledBorder(
061                BorderFactory.createEtchedBorder(), Bundle.getMessage("EventCol")));
062
063        // define contents
064        evEnButton.setText(Bundle.getMessage("ButtonEnable"));
065        evEnButton.setVisible(true);
066        evEnButton.setSelected(true);
067        evEnButton.setToolTipText(Bundle.getMessage("EnableEventTooltip"));
068        evPane.add(evEnButton);
069        
070        eventnumberspinner.setToolTipText(Bundle.getMessage("EvLowfieldTooltip"));
071        evPane.add(eventnumberspinner);
072        JSpinner.NumberEditor editor = new JSpinner.NumberEditor(eventnumberspinner, "#");
073        eventnumberspinner.setEditor(editor);
074        JFormattedTextField fieldEv = (JFormattedTextField) editor.getComponent(0);
075        DefaultFormatter formatterEv = (DefaultFormatter) fieldEv.getFormatter();
076        fieldEv.setColumns(2);
077        formatterEv.setCommitsOnValidEdit(true);
078        
079        this.add(evPane);
080
081        // Pane to hold node number
082        JPanel nnPane = new JPanel();
083        nnPane.setLayout(new BoxLayout(nnPane, BoxLayout.X_AXIS));
084        nnPane.setBorder(BorderFactory.createTitledBorder(
085                BorderFactory.createEtchedBorder(), Bundle.getMessage("NodeNumberTitle")));
086
087        // define contents
088        nnEnButton.setText(Bundle.getMessage("ButtonEnable"));
089        nnEnButton.setVisible(true);
090        nnEnButton.setToolTipText(Bundle.getMessage("EnableNodeTooltip"));
091        nnPane.add(nnEnButton);
092
093        JSpinner.NumberEditor neditor = new JSpinner.NumberEditor(nodenumberspinner, "#");
094        nodenumberspinner.setEditor(neditor);
095        JFormattedTextField fieldNd = (JFormattedTextField) neditor.getComponent(0);
096        DefaultFormatter formatterNd = (DefaultFormatter) fieldNd.getFormatter();
097        fieldNd.setColumns(2);
098        formatterNd.setCommitsOnValidEdit(true); 
099        
100        nodenumberspinner.setToolTipText(Bundle.getMessage("NnLowfieldTooltip"));
101        nnPane.add(nodenumberspinner);
102
103        this.add(nnPane);
104
105        // Pane to hold event type
106        JPanel eventPane = new JPanel();
107        eventPane.setLayout(new BoxLayout(eventPane, BoxLayout.X_AXIS));
108        eventPane.setBorder(BorderFactory.createTitledBorder(
109                BorderFactory.createEtchedBorder(), Bundle.getMessage("EventTypeTitle")));
110
111        // define contents
112        onButton.setText(Bundle.getMessage("CbusEventOn"));
113        onButton.setVisible(true);
114        onButton.setToolTipText(Bundle.getMessage("OnEventsTooltip"));
115        eventPane.add(onButton);
116
117        offButton.setText(Bundle.getMessage("CbusEventOff"));
118        offButton.setVisible(true);
119        offButton.setToolTipText(Bundle.getMessage("OffEventsTooltip"));
120        eventPane.add(offButton);
121
122        this.add(eventPane);
123
124        // Pane to hold event type
125        JPanel directionPane = new JPanel();
126        directionPane.setLayout(new BoxLayout(directionPane, BoxLayout.X_AXIS));
127        directionPane.setBorder(BorderFactory.createTitledBorder(
128                BorderFactory.createEtchedBorder(), Bundle.getMessage("EventdirectionTitle")));
129
130        // define contents
131        inButton.setText(Bundle.getMessage("InEventsButton"));
132        inButton.setVisible(true);
133        inButton.setToolTipText(Bundle.getMessage("InEventsTooltip"));
134        directionPane.add(inButton);
135
136        outButton.setText(Bundle.getMessage("OutEventsButton"));
137        outButton.setVisible(true);
138        outButton.setToolTipText(Bundle.getMessage("OutEventsTooltip"));
139        directionPane.add(outButton);
140
141        this.add(directionPane);
142        
143        eventnumberspinner.addChangeListener((ChangeEvent e) -> {
144            if ( evEnButton.isSelected() ) {
145                setoptions();
146            }
147        });
148        
149        nodenumberspinner.addChangeListener((ChangeEvent e) -> {
150            if ( nnEnButton.isSelected() ) {
151                setoptions();
152            }
153        });
154        
155        // update if already active
156        nnEnButton.addActionListener((java.awt.event.ActionEvent e) -> {
157            setoptions();
158        });
159        
160        // update if already active
161        evEnButton.addActionListener((java.awt.event.ActionEvent e) -> {
162            setoptions();
163        });
164
165        // update if already active
166        onButton.addActionListener((java.awt.event.ActionEvent e) -> {
167            setoptions();
168        });
169        
170        // update if already active
171        offButton.addActionListener((java.awt.event.ActionEvent e) -> {
172            setoptions();
173        });
174        
175        // update if already active
176        inButton.addActionListener((java.awt.event.ActionEvent e) -> {
177            setoptions();
178        });
179        
180        // update if already active
181        outButton.addActionListener((java.awt.event.ActionEvent e) -> {
182            setoptions();
183        });
184
185    }
186        
187    private void setoptions(){    
188        
189        int nn = 0;
190        int ev = 0;
191        int ty = CbusConstants.EVENT_NEITHER;
192        int dr = CbusConstants.EVENT_DIR_UNSET;
193        if (nnEnButton.isSelected()) {
194            nn = (Integer) nodenumberspinner.getValue();
195        }
196
197        if (evEnButton.isSelected()) {
198            ev = (Integer) eventnumberspinner.getValue();
199        }
200
201        if ( onButton.isSelected() ) {
202            ty = CbusConstants.EVENT_ON;
203        }
204        if ( offButton.isSelected() ) {
205            ty = CbusConstants.EVENT_OFF;
206        }
207        
208        if ( onButton.isSelected() && offButton.isSelected() ) {
209            ty = CbusConstants.EVENT_EITHER;
210        }
211        
212        if ( inButton.isSelected()) {
213            dr = dr + CbusConstants.EVENT_DIR_IN;
214        }
215        if ( outButton.isSelected()) {
216            dr = dr + CbusConstants.EVENT_DIR_OUT;
217        }
218        
219        if (( dr > 0 ) && ( _isActive ) && ( ty !=CbusConstants.EVENT_NEITHER ) ) {
220            _highlightFrame.enable(_index, nn, nnEnButton.isSelected(), ev, evEnButton.isSelected(), ty, dr);
221        }
222        if (( dr > 0 ) && ( !_isActive ) && ( ty !=CbusConstants.EVENT_NEITHER ) ) {
223            _highlightFrame.enable(_index, nn, nnEnButton.isSelected(), ev, evEnButton.isSelected(), ty, dr);
224            _isActive=true;
225        }        
226        
227        if ( ( ( dr == 0 ) || ( ty == CbusConstants.EVENT_NEITHER ) ) && ( _isActive ) ) {
228            _highlightFrame.disable(_index);
229            _isActive=false;
230        }
231    }
232}