001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.history;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Color;
007import java.awt.Component;
008
009import javax.swing.ImageIcon;
010import javax.swing.JLabel;
011import javax.swing.JTable;
012import javax.swing.table.TableCellRenderer;
013
014import org.openstreetmap.josm.gui.history.TwoColumnDiff.Item.DiffItemType;
015import org.openstreetmap.josm.tools.ImageProvider;
016
017public class NodeListTableCellRenderer extends JLabel implements TableCellRenderer {
018
019    public static final Color BGCOLOR_SELECTED = new Color(143,170,255);
020
021    private ImageIcon nodeIcon;
022
023    /**
024     * Constructs a new {@code NodeListTableCellRenderer}.
025     */
026    public NodeListTableCellRenderer(){
027        setOpaque(true);
028        nodeIcon = ImageProvider.get("data", "node");
029        setIcon(nodeIcon);
030    }
031
032    protected void renderNode(TwoColumnDiff.Item item, boolean isSelected) {
033        String text = "";
034        Color bgColor = Color.WHITE;
035        setIcon(nodeIcon);
036        if (item.value != null) {
037            text = tr("Node {0}", item.value.toString());
038        }
039        bgColor = item.state.getColor();
040        if (item.state == DiffItemType.EMPTY) {
041            text = "";
042            setIcon(null);
043        }
044        if (isSelected) {
045            bgColor = BGCOLOR_SELECTED;
046        }
047        setText(text);
048        setBackground(bgColor);
049    }
050
051    // Warning: The model pads with null-rows to match the size of the opposite table. 'value' could be null
052    @Override
053    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
054            int row, int column) {
055
056        renderNode((TwoColumnDiff.Item)value, isSelected);
057        return this;
058    }
059}