001package jmri.jmrit.operations;
002
003import java.awt.Color;
004import java.awt.Component;
005
006import javax.swing.JComboBox;
007import javax.swing.JTable;
008import javax.swing.table.DefaultTableCellRenderer;
009
010/**
011 * Common table model methods for operations.
012 * 
013 * @author Daniel Boudreau Copyright (C) 2023
014 *
015 */
016public abstract class OperationsTableModel extends javax.swing.table.AbstractTableModel {
017    
018    private JTable _table;
019    
020    public void initTable(JTable table) {
021        _table = table;
022        table.setDefaultRenderer(JComboBox.class, new jmri.jmrit.symbolicprog.ValueRenderer());
023        table.setDefaultEditor(JComboBox.class, new jmri.jmrit.symbolicprog.ValueEditor());
024        table.setDefaultRenderer(Object.class, new MyTableCellRenderer());
025        table.setDefaultRenderer(Integer.class, new MyTableCellRenderer());
026    }
027
028    protected Color getForegroundColor(int row) {
029        return _table.getForeground();
030    }
031    
032    public class MyTableCellRenderer extends DefaultTableCellRenderer {
033        @Override
034        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
035                int row, int column) {
036            Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
037            if (!isSelected) {
038                int modelRow = table.convertRowIndexToModel(row);
039                component.setForeground(getForegroundColor(modelRow));
040            }
041            return component;
042        }
043    }
044}