001package jmri.jmrix.can.cbus.swing.nodeconfig;
002
003import java.awt.BorderLayout;
004import java.awt.event.ActionListener;
005import javax.swing.JButton;
006import javax.swing.JPanel;
007
008import jmri.jmrix.can.cbus.node.CbusNode;
009import jmri.jmrix.can.cbus.node.CbusNodeEvent;
010import jmri.jmrix.can.cbus.node.CbusNodeEventTableDataModel;
011
012// import org.slf4j.Logger;
013// import org.slf4j.LoggerFactory;
014
015/**
016 *
017 * @author Steve Young Copyright (C) 2019
018 */
019public class CbusNodeEventVarPane extends CbusNodeConfigTab {
020    
021    private CbusNodeEventTableDataModel nodeEvModel;
022    private JButton newEvButton;
023    private CbusNodeEventTablePane genericEvTable;
024
025    /**
026     * Create a new instance of CbusNodeEventVarPane.
027     * @param main the main NodeConfigToolPane this is a pane of.
028     */
029    protected CbusNodeEventVarPane( NodeConfigToolPane main ) {
030        super(main);
031        initPane();
032    }
033    
034    /**
035     * {@inheritDoc}
036     */
037    @Override
038    public String getTitle(){
039        return "Node Events";
040    }
041    
042    /**
043     * {@inheritDoc}
044     */
045    @Override
046    protected void disposeOfNode(CbusNode node){
047        super.disposeOfNode(node);
048        nodeEvModel.dispose();
049    }
050    
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    public void changedNode( CbusNode node ) {
056        // nodeEvModel.setNode( node );
057        genericEvTable.setNode(node);
058        nodeEvModel.fireTableDataChanged();
059        newEvButton.setEnabled(nodeOfInterest.getNodeParamManager().getParameter(5) > -1 );
060        
061        validate();
062        repaint();
063    }
064    
065    private void initPane(){
066    
067        JPanel evMenuPane = new JPanel();
068      
069        newEvButton = new JButton(Bundle.getMessage("AddNodeEvent"));
070        newEvButton.setToolTipText(Bundle.getMessage("AddNodeEventTip"));
071        
072        addButtonListener(newEvButton);
073        
074        evMenuPane.add(newEvButton);
075        
076        nodeEvModel = new CbusNodeEventTableDataModel(  getMainPane(), memo, 10,
077            CbusNodeEventTableDataModel.MAX_COLUMN); // mainpane, controller, row, column
078        
079        genericEvTable = new CbusNodeEventTablePane(nodeEvModel);
080        genericEvTable.initComponents(memo);
081        genericEvTable.setVisible(true);
082        
083        add(evMenuPane, BorderLayout.PAGE_START);
084        add(genericEvTable, BorderLayout.CENTER);
085        
086    }
087    
088    private void addButtonListener(JButton button){
089        ActionListener newEvButtonClicked = ae -> {
090            CbusNodeEvent newevent = new CbusNodeEvent( memo,
091                -1,-1,nodeOfInterest.getNodeNumber(),-1,nodeOfInterest.getNodeParamManager().getParameter(5));
092            java.util.Arrays.fill(newevent.getEvVarArray(),0);
093            getMainPane().getEditEvFrame().initComponents(memo,newevent);
094        };
095        button.addActionListener(newEvButtonClicked);
096    }
097    
098    // private final static Logger log = LoggerFactory.getLogger(CbusNodeEventVarPane.class);
099    
100}