001package jmri.jmrit.symbolicprog;
002
003import java.awt.Component;
004import javax.swing.JComponent;
005import javax.swing.JLabel;
006import javax.swing.JTable;
007
008/**
009 * Specialization of ValueRenderer to add CV-usage tooltips and handle Integer values
010 *
011 * @author Bob Jacobsen Copyright (C) 2023
012 */
013public class CvValueRenderer extends ValueRenderer {
014
015    public CvValueRenderer() {
016        super();
017    }
018
019    @Override
020    public Component getTableCellRendererComponent(JTable table, Object value,
021            boolean isSelected, boolean hasFocus,
022            int row, int column) {
023        // if (log.isDebugEnabled()) log.debug("getTableCellRendererComponent "
024        //       +" "+row+" "+column
025        //       +" "+isSelected+" "+hasFocus
026        //       +" "+value);
027
028        JComponent retval;
029
030        if (value instanceof Integer) {
031            retval = new JLabel(value.toString());
032        } else {
033            retval = (JComponent) super.getTableCellRendererComponent(table, value,
034                                                                      isSelected, hasFocus,
035                                                                      row, column);
036        }
037
038        // get the CV number
039        var model = (CvTableModel)table.getModel();
040        int modelRow = table.convertRowIndexToModel(row);
041        var cvNum = model.getValueAt(modelRow, CvTableModel.NUMCOLUMN).toString();
042        var nameSet = model.getCvToVariableMapping(cvNum);
043        if (nameSet != null ) {
044            var building = new StringBuilder();
045            boolean first = true;
046            for(String item : nameSet){
047                if (! first) building.append("; ");
048                first = false;
049                building.append(item);
050            }
051            retval.setToolTipText(building.toString());
052        }
053        return retval;
054    }
055}