001package jmri.jmrix.can.cbus.swing.nodeconfig;
002
003import java.awt.BorderLayout;
004import java.awt.Color;
005import java.awt.Component;
006import java.awt.Dimension;
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.TableRowSorter;
013import jmri.jmrix.can.CanSystemConnectionMemo;
014import jmri.jmrix.can.cbus.node.CbusNodeFromFcuTableDataModel;
015import jmri.jmrix.can.cbus.swing.CbusCommonSwing;
016import jmri.util.swing.XTableColumnModel;
017import jmri.util.table.JTableWithColumnToolTips;
018
019// import org.slf4j.Logger;
020// import org.slf4j.LoggerFactory;
021
022/**
023 * Pane providing a Cbus node table.
024 *
025 * @author Steve Young (C) 2019
026 * @see CbusNodeFromFcuTableDataModel
027 *
028 * @since 4.15.5
029 */
030public class CbusNodeFcuTablePane extends JPanel {
031
032    private CbusNodeFromFcuTableDataModel nodeModel=null;
033    protected JTable nodeTable=null;
034    private TableRowSorter<CbusNodeFromFcuTableDataModel> sorter;
035
036    public static final Color WHITE_GREEN = new Color(0xf5,0xf5,0xf5);
037    
038    public CbusNodeFcuTablePane() {
039        super();
040    }
041    
042    public void initComponents(CanSystemConnectionMemo memo, CbusNodeFromFcuTableDataModel model) {
043        
044        nodeModel = model;
045        init();
046        
047    }
048
049    public void init() {  
050        
051        nodeTable = new JTableWithColumnToolTips(nodeModel,CbusNodeFromFcuTableDataModel.FCUTABLETIPS);
052
053        // Use XTableColumnModel so we can control which columns are visible
054        XTableColumnModel tcm = new XTableColumnModel();
055        nodeTable.setColumnModel(tcm);
056        
057        sorter = new TableRowSorter<>(nodeModel);
058        nodeTable.setRowSorter(sorter);
059        
060        // configure items for GUI
061        CbusCommonSwing.configureTable(nodeTable);
062        
063        tcm.getColumn(CbusNodeFromFcuTableDataModel.FCU_NODE_NUMBER_COLUMN).setCellRenderer(getRenderer());
064        tcm.getColumn(CbusNodeFromFcuTableDataModel.FCU_NODE_TYPE_NAME_COLUMN).setCellRenderer(getRenderer());
065        tcm.getColumn(CbusNodeFromFcuTableDataModel.FCU_NODE_USER_NAME_COLUMN).setCellRenderer(getRenderer());
066        tcm.getColumn(CbusNodeFromFcuTableDataModel.FCU_NODE_EVENTS_COLUMN).setCellRenderer(getRenderer());
067        tcm.getColumn(CbusNodeFromFcuTableDataModel.NODE_NV_TOTAL_COLUMN).setCellRenderer(getRenderer());
068        tcm.getColumn(CbusNodeFromFcuTableDataModel.FCU_NODE_TOTAL_BYTES_COLUMN).setCellRenderer(getRenderer());
069        
070        setLayout(new BorderLayout());
071        JScrollPane eventScroll = new JScrollPane(nodeTable);
072        eventScroll.setVisible(true);
073        setPreferredSize(new Dimension(650, 100));
074        add(eventScroll);
075
076    }
077
078    /**
079     * Cell Renderer for string table columns, highlights any text in filter input
080     */    
081    private TableCellRenderer getRenderer() {
082        return new TableCellRenderer() {
083            
084            JTextField f = new JTextField();
085            
086            @Override
087            public Component getTableCellRendererComponent(
088                JTable table, Object arg1, boolean isSelected, boolean hasFocus, 
089                int row, int col) {
090                
091                f.setHorizontalAlignment(JTextField.CENTER);
092                
093                String string;
094                if(arg1 != null){
095                    string = arg1.toString();
096                    f.setText(string);
097                    CbusCommonSwing.hideNumbersLessThan(0, string, f);
098                    
099                } else {
100                    f.setText("");
101                }
102
103                CbusCommonSwing.setCellBackground(isSelected, f, table,row);
104                CbusCommonSwing.setCellFocus(hasFocus, f, table);
105                
106                return f;
107            }
108        };
109    }
110    
111    public void dispose() {
112        nodeTable = null;
113    }
114
115    // private final static Logger log = LoggerFactory.getLogger(CbusNodeFcuTablePane.class);
116
117}