001package jmri.jmrix.can.cbus.swing;
002
003import java.awt.event.ActionEvent;
004import javax.swing.*;
005import javax.swing.event.ChangeEvent;
006import javax.swing.text.DefaultFormatter;
007import jmri.jmrix.can.cbus.swing.eventrequestmonitor.CbusEventRequestTablePane;
008import jmri.jmrix.can.cbus.swing.eventtable.CbusEventTablePane;
009
010// import org.slf4j.Logger;
011// import org.slf4j.LoggerFactory;
012
013/**
014 * Creates Panel for displaying New Event JSpinners and Button.
015 * @author Steve Young Copyright (C) 2018, 2020
016 */
017public class CbusNewEventPane extends JPanel {
018    
019    private JSpinner newnodenumberSpinner;
020    private JSpinner newevnumberSpinner;
021    private JButton newevbutton;
022    
023    private final CbusEventTablePane _evPanel;
024    private final CbusEventRequestTablePane _evReqPanel;
025    
026    private jmri.UserPreferencesManager p;
027    private static final String EVENTNUM_PREF = "EventNum"; // NOI18N
028    private static final String NODENUM_PREF = "NodeNum"; // NOI18N
029    
030    public CbusNewEventPane(CbusEventTablePane evPanel){
031        super();
032        _evPanel = evPanel;
033        _evReqPanel = null;
034        init();
035        
036    }
037    
038    public CbusNewEventPane( CbusEventRequestTablePane evReqPanel ){
039        super();
040        _evPanel = null;
041        _evReqPanel = evReqPanel;
042        init();
043    }
044    
045    final void init() {
046        
047        p = jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class);
048        int nodenum = 0; // default node 0
049        int eventnum = 1; // default event 1
050        Object nn = p.getProperty(this.getClass().getName(), NODENUM_PREF);
051        if ( nn!=null ){
052            nodenum = (Integer) nn;
053        }
054        Object en = p.getProperty(this.getClass().getName(), EVENTNUM_PREF);
055        if ( en!=null ){
056            eventnum = (Integer) en;
057        }
058        
059        newnodenumberSpinner = new JSpinner(new SpinnerNumberModel(nodenum, 0, 65535, 1));
060        newevnumberSpinner = new JSpinner(new SpinnerNumberModel(eventnum, 0, 65535, 1));
061    
062        this.setBorder(BorderFactory.createTitledBorder(
063        BorderFactory.createEtchedBorder(), (Bundle.getMessage("NewEvent"))));
064        
065        newevbutton = new JButton((Bundle.getMessage("NewEvent")));
066        newevbutton.addActionListener((ActionEvent e) -> newEvent());
067        
068        this.add(newSpinnerPanel(newnodenumberSpinner, 
069                Bundle.getMessage("CbusNode"), Bundle.getMessage("NewNodeTip")));
070        this.add(newSpinnerPanel(newevnumberSpinner, 
071                Bundle.getMessage("CbusEvent"), null)); 
072        this.add(newevbutton);  
073        
074    }
075    
076    private JPanel newSpinnerPanel( final JSpinner spinner, String label, String tip  ){
077        JPanel newnode = new JPanel();
078        newnode.add(new JLabel(label));
079        JComponent comp = spinner.getEditor();
080        JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
081        DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
082        formatter.setCommitsOnValidEdit(true);
083        spinner.addChangeListener((ChangeEvent e) ->  eventChanged() );
084        newnode.add(spinner);
085        newnode.setToolTipText(tip);
086        spinner.setToolTipText(tip);
087        return newnode;
088    }
089
090    private void eventChanged(){
091        if (_evPanel!=null){
092            _evPanel.cbEvTable.tableChanged(null);
093        }
094        else if (_evReqPanel!=null){
095            _evReqPanel.tableChanged(null);
096        }
097        p.setProperty(this.getClass().getName(), NODENUM_PREF, newnodenumberSpinner.getValue());
098        p.setProperty(this.getClass().getName(), EVENTNUM_PREF, newevnumberSpinner.getValue());
099    }
100    
101    public void setNewButtonActive(boolean newState){
102        newevbutton.setEnabled(newState);
103    }
104    
105    public int getNn(){
106        return (Integer) newnodenumberSpinner.getValue();
107    }
108    
109    public int getEn(){
110        return (Integer) newevnumberSpinner.getValue();
111    }
112    
113    private void newEvent(){
114        if (_evPanel!=null){
115            _evPanel.cbEvTable.addEvent(getNn(),getEn());
116        }
117        else if (_evReqPanel!=null){
118            _evReqPanel.addEvent(getNn(),getEn());
119        }
120    }
121    
122    // private final static Logger log = LoggerFactory.getLogger(CbusNewEventPane.class);
123}