001package jmri.jmrix.can.cbus.swing.nodeconfig;
002
003import java.awt.BorderLayout;
004import java.awt.Color;
005import java.awt.Component;
006import java.awt.Font;
007import javax.swing.JPanel;
008import javax.swing.JScrollPane;
009import javax.swing.JTable;
010import javax.swing.JTextField;
011import javax.swing.table.TableCellRenderer;
012import javax.swing.table.TableColumnModel;
013import jmri.jmrix.can.cbus.node.CbusNodeSingleEventTableDataModel;
014import jmri.jmrix.can.CanSystemConnectionMemo;
015
016// import org.slf4j.Logger;
017// import org.slf4j.LoggerFactory;
018
019/**
020 * Pane providing a Cbus Node Event Edit table.
021 * @author Steve Young (C) 2019
022 *
023 * @since 4.15.5
024 */
025public class CbusNodeSingleEventEditTablePane extends jmri.jmrix.can.swing.CanPanel {
026
027    private final CbusNodeSingleEventTableDataModel singleEVModel;
028    private JScrollPane eventScroll;
029    private JPanel pane1;
030    private int largerFont;
031    private JTable singleEvTable;
032    
033    // private NodeConfigToolPane mainpane;
034
035    protected CbusNodeSingleEventEditTablePane( CbusNodeSingleEventTableDataModel eVModel ) {
036        super();
037        singleEVModel = eVModel;
038        singleEvTable = new JTable(singleEVModel);
039    }
040
041    public void initComponents(CanSystemConnectionMemo memo, NodeConfigToolPane pane ) {
042        super.initComponents(memo);
043        // mainpane = pane;
044        singleEvTable = new JTable(singleEVModel);
045        init();
046        
047    }
048
049    private void init() {
050        
051        pane1 = null;
052        pane1 = new JPanel();
053        
054        TableColumnModel tableModel = singleEvTable.getColumnModel();
055        
056        singleEvTable.setRowSelectionAllowed(true);
057        singleEvTable.setColumnSelectionAllowed(false);
058        singleEvTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
059        
060        singleEvTable.setRowHeight(27);
061        
062        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_NUMBER_COLUMN).setCellRenderer(getRenderer());
063        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_CURRENT_VAL_COLUMN).setCellRenderer( getRenderer() );
064        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_CURRENT_HEX_COLUMN).setCellRenderer(getRenderer());
065        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_CURRENT_BIT_COLUMN).setCellRenderer( getRenderer() );        
066        
067        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_SELECT_COLUMN).setCellRenderer(
068            new CbusNodeNVEditTablePane.SpinnerRenderer() );
069        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_SELECT_COLUMN).setCellEditor(
070            new CbusNodeNVEditTablePane.NvSpinnerEditor() );
071        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_SELECT_HEX_COLUMN).setCellRenderer(getRenderer());
072        tableModel.getColumn(CbusNodeSingleEventTableDataModel.EV_SELECT_BIT_COLUMN).setCellRenderer( getRenderer() );        
073        
074        JTextField f = new JTextField();
075        largerFont = f.getFont().getSize()+2;
076        
077        // configure items for GUI
078        singleEVModel.configureTable(singleEvTable);   
079        
080        setLayout(new BorderLayout() );
081        
082        pane1.setLayout(new BorderLayout());
083        
084        // scroller for main table
085        eventScroll = new JScrollPane(singleEvTable);
086
087        pane1.add(eventScroll);
088        
089        add(pane1);
090        pane1.setVisible(true);
091        
092    }
093    
094    public static final Color WHITE_GREEN = new Color(0xf5,0xf5,0xf5);
095    
096    /**
097     * Cell Renderer for string table columns, highlights any text in filter input
098     */    
099    private TableCellRenderer getRenderer() {
100        return new TableCellRenderer() {
101            
102            JTextField f = new JTextField();
103            
104            @Override
105            public Component getTableCellRendererComponent(
106                JTable table, Object arg1, boolean isSelected, boolean hasFocus, int row, int col) {
107                
108                f.setHorizontalAlignment(JTextField.CENTER);
109                f.setBorder( table.getBorder() );
110                
111                int tablecol = singleEvTable.convertColumnIndexToModel(col);
112
113                if ( tablecol == CbusNodeSingleEventTableDataModel.EV_NUMBER_COLUMN ){
114                    f.setFont(f.getFont().deriveFont(Font.BOLD, largerFont ));
115                }
116                
117                int oldval = (int) singleEVModel.getValueAt(row, CbusNodeSingleEventTableDataModel.EV_CURRENT_VAL_COLUMN);
118                int newval = (int) singleEVModel.getValueAt(row, CbusNodeSingleEventTableDataModel.EV_SELECT_COLUMN);
119                
120                String string;
121                if(arg1 != null){
122                    string = arg1.toString();
123                    if (string.equals("0000 0000")) {
124                        string = "";
125                    }
126                    f.setText(string.toUpperCase() );
127                } else {
128                    f.setText("");
129                }
130                if (isSelected) {
131                    if ( oldval != newval ) {
132                        f.setBackground( Color.orange );
133                    }
134                    else {
135                        f.setBackground( table.getSelectionBackground() );
136                    }
137                } else {
138                    if ( oldval != newval ) {
139                        f.setBackground( Color.yellow );
140                    }
141                    else {
142                        if ( row % 2 == 0 ) {
143                            f.setBackground( table.getBackground() );
144                        }
145                        else {
146                            f.setBackground( WHITE_GREEN );
147                        }
148                    }
149                }
150                
151                return f;
152            }
153        };
154    }
155
156    @Override
157    public void dispose() {
158        //   nodeTable = null;
159        eventScroll = null;
160        super.dispose();
161    }
162
163    // private final static Logger log = LoggerFactory.getLogger(CbusNodeSingleEventEditTablePane.class);
164
165}