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    }
026
027    protected Color getForegroundColor(int row) {
028        return _table.getForeground();
029    }
030    
031    public class MyTableCellRenderer extends DefaultTableCellRenderer {
032        @Override
033        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
034                int row, int column) {
035            Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
036            if (!isSelected) {
037                int modelRow = table.convertRowIndexToModel(row);
038                component.setForeground(getForegroundColor(modelRow));
039            }
040            return component;
041        }
042    }
043}