001package jmri.util.swing;
002
003import java.awt.Component;
004
005import javax.swing.JTable;
006import javax.swing.JTextArea;
007import javax.swing.table.TableCellRenderer;
008import javax.swing.UIManager;
009
010/**
011 * Renderer to display multiple lines in a JTable cell
012 */
013public class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
014
015    public MultiLineCellRenderer() {
016        setLineWrap(true);
017        setWrapStyleWord(true);
018        setOpaque(true);
019    }
020
021    @Override
022    public Component getTableCellRendererComponent(JTable table, Object value,
023        boolean isSelected, boolean hasFocus, int row, int column) {
024        if (isSelected) {
025            setForeground(table.getSelectionForeground());
026            setBackground(table.getSelectionBackground());
027        } else {
028            setForeground(table.getForeground());
029            setBackground(table.getBackground());
030        }
031        setFont(table.getFont());
032        if (hasFocus) {
033            if (table.isCellEditable(row, column)) {
034                setForeground(UIManager.getColor("Table.focusCellForeground"));
035                setBackground(UIManager.getColor("Table.focusCellBackground"));
036            }
037        }
038        setText((value == null) ? "" : value.toString());
039        return this;
040    }
041}