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.gui.util.GuiHelper;
016import org.openstreetmap.josm.tools.ImageProvider;
017
018public class NodeListTableCellRenderer extends JLabel implements TableCellRenderer {
019
020    public static final Color BGCOLOR_SELECTED = new Color(143, 170, 255);
021
022    private final ImageIcon nodeIcon;
023
024    /**
025     * Constructs a new {@code NodeListTableCellRenderer}.
026     */
027    public NodeListTableCellRenderer() {
028        setOpaque(true);
029        nodeIcon = ImageProvider.get("data", "node");
030        setIcon(nodeIcon);
031    }
032
033    protected void renderNode(TwoColumnDiff.Item item, boolean isSelected) {
034        String text = "";
035        setIcon(nodeIcon);
036        if (item.value != null) {
037            text = tr("Node {0}", item.value.toString());
038        }
039        Color 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        GuiHelper.setBackgroundReadable(this, 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        if (value != null) {
057            renderNode((TwoColumnDiff.Item) value, isSelected);
058        }
059        return this;
060    }
061}