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
018/**
019 * A {@link TableCellRenderer} for the {@link NodeListViewer}.
020 * 
021 * Renders information about a node when comparing the node list of two
022 * historical versions of a way.
023 */
024public class NodeListTableCellRenderer extends JLabel implements TableCellRenderer {
025
026    public static final Color BGCOLOR_SELECTED = new Color(143, 170, 255);
027
028    private final ImageIcon nodeIcon;
029
030    /**
031     * Constructs a new {@code NodeListTableCellRenderer}.
032     */
033    public NodeListTableCellRenderer() {
034        setOpaque(true);
035        nodeIcon = ImageProvider.get("data", "node");
036        setIcon(nodeIcon);
037    }
038
039    protected void renderNode(TwoColumnDiff.Item item, boolean isSelected) {
040        String text = "";
041        setIcon(nodeIcon);
042        if (item.value != null) {
043            text = tr("Node {0}", item.value.toString());
044        }
045        Color bgColor = item.state.getColor();
046        if (item.state == DiffItemType.EMPTY) {
047            text = "";
048            setIcon(null);
049        }
050        if (isSelected) {
051            bgColor = BGCOLOR_SELECTED;
052        }
053        setText(text);
054        GuiHelper.setBackgroundReadable(this, bgColor);
055    }
056
057    // Warning: The model pads with null-rows to match the size of the opposite table. 'value' could be null
058    @Override
059    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
060            int row, int column) {
061
062        if (value != null) {
063            renderNode((TwoColumnDiff.Item) value, isSelected);
064        }
065        return this;
066    }
067}