001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.history;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.awt.Color;
007    import java.awt.Component;
008    
009    import javax.swing.ImageIcon;
010    import javax.swing.JLabel;
011    import javax.swing.JTable;
012    import javax.swing.table.TableCellRenderer;
013    
014    import org.openstreetmap.josm.tools.ImageProvider;
015    
016    public class NodeListTableCellRenderer extends JLabel implements TableCellRenderer {
017    
018        public final static Color BGCOLOR_EMPTY_ROW = new Color(234,234,234);
019        public final static Color BGCOLOR_DELETED = new Color(255,197,197);
020        public final static Color BGCOLOR_INSERTED = new Color(0xDD, 0xFF, 0xDD);
021        public final static Color BGCOLOR_CHANGED = new Color(255,234,213);
022        public final static Color BGCOLOR_SELECTED = new Color(143,170,255);
023    
024        private ImageIcon nodeIcon;
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            switch(item.state) {
040            case TwoColumnDiff.Item.EMPTY:
041                text = "";
042                bgColor = BGCOLOR_EMPTY_ROW;
043                setIcon(null);
044                break;
045            case TwoColumnDiff.Item.CHANGED:
046                bgColor = BGCOLOR_CHANGED;
047                break;
048            case TwoColumnDiff.Item.INSERTED:
049                bgColor = BGCOLOR_INSERTED;
050                break;
051            case TwoColumnDiff.Item.DELETED:
052                bgColor = BGCOLOR_DELETED;
053                break;
054            default:
055                bgColor = BGCOLOR_EMPTY_ROW;
056            }
057            if (isSelected) {
058                bgColor = BGCOLOR_SELECTED;
059            }
060            setText(text);
061            setBackground(bgColor);
062        }
063    
064        // Warning: The model pads with null-rows to match the size of the opposite table. 'value' could be null
065        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
066                int row, int column) {
067    
068            renderNode((TwoColumnDiff.Item)value, isSelected);
069            return this;
070        }
071    }