001package jmri.jmrix.can.cbus.swing.eventtable;
002
003import java.awt.BorderLayout;
004import java.awt.Component;
005import java.awt.GridBagLayout;
006import javax.annotation.Nonnull;
007import javax.swing.BorderFactory;
008import javax.swing.JLabel;
009import javax.swing.JTable;
010import javax.swing.JPanel;
011import javax.swing.JTextField;
012import javax.swing.table.TableCellRenderer;
013import jmri.NamedBean;
014import jmri.jmrix.can.cbus.eventtable.CbusEventBeanData;
015import jmri.jmrix.can.cbus.swing.CbusCommonSwing;
016
017// import org.slf4j.Logger;
018// import org.slf4j.LoggerFactory;
019
020/**
021 * Cell Renderer for 
022 */ 
023public class CbusEventBeanCellRenderer implements TableCellRenderer {
024
025    private final JTextField _filterText;
026    private final CbusBeanRenderer _beanImages;
027    
028    public CbusEventBeanCellRenderer( @Nonnull JTextField filterText, int iconHeight ){
029        super();
030        _filterText = filterText;
031        _beanImages = new CbusBeanRenderer(iconHeight);
032    }
033    
034    private JPanel f;
035    
036    @Override
037    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
038        boolean hasFocus, int row, int column) {
039        
040        f = new JPanel();
041        f.setLayout(new BorderLayout());
042        
043        if (value instanceof CbusEventBeanData && !value.toString().isEmpty()) {
044            
045            JTextField tf = new JTextField(value.toString());
046            CbusCommonSwing.setCellTextHighlighter(_filterText.getText(), value.toString(), tf);
047            
048            tf.setBorder(null);
049            tf.setOpaque(false);
050            tf.setHorizontalAlignment(JTextField.CENTER);
051            
052            f.add(tf,BorderLayout.CENTER);
053            
054            JPanel yy = new JPanel();
055            yy.setLayout(new GridBagLayout());
056            yy.setOpaque(false);
057        
058            ((CbusEventBeanData) value).getActionA().forEach((n) -> appendImgToPanel( yy, n, true) );
059            ((CbusEventBeanData) value).getActionB().forEach((n) -> appendImgToPanel( yy, n, false) );
060        
061            f.add(yy, BorderLayout.LINE_START);
062            
063        }
064        
065        CbusCommonSwing.setCellBackground(isSelected, f, table,row);
066        CbusCommonSwing.setCellFocus(hasFocus, f, table);
067        
068        return f;
069    }
070    
071    private void appendImgToPanel(JPanel pnl, NamedBean bean, boolean beanOn){
072        JLabel lbl;
073         if (bean instanceof jmri.Turnout) {
074            lbl = new JLabel(_beanImages.getBeanIcon("T",beanOn? jmri.DigitalIO.ON : jmri.DigitalIO.OFF));
075        }
076        else if (bean instanceof jmri.Sensor) {
077            lbl = new JLabel(_beanImages.getBeanIcon("S",beanOn? jmri.DigitalIO.ON : jmri.DigitalIO.OFF));
078        }
079        else if (bean instanceof jmri.Light) {
080            lbl = new JLabel(_beanImages.getBeanIcon("L",beanOn? jmri.DigitalIO.ON : jmri.DigitalIO.OFF));
081        }
082        else {
083            return;
084        }
085        lbl.setOpaque(false); 
086           
087        JPanel tmp = new JPanel();
088        tmp.setLayout(new GridBagLayout());
089        tmp.setOpaque(false);
090        tmp.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
091        tmp.add(lbl);
092        
093        pnl.add(tmp);
094    }
095    
096    // private final static Logger log = LoggerFactory.getLogger(CbusEventBeanCellRenderer.class);
097    
098}