001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation;
003
004import java.awt.Color;
005import java.awt.Component;
006
007import javax.swing.JLabel;
008import javax.swing.JTable;
009import javax.swing.UIManager;
010import javax.swing.table.TableCellRenderer;
011
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013import org.openstreetmap.josm.gui.util.GuiHelper;
014
015/**
016 * This is the {@link TableCellRenderer} used in the tables of
017 * {@link org.openstreetmap.josm.gui.conflict.pair.relation.RelationMemberMerger}.
018 *
019 */
020public abstract class MemberTableCellRenderer extends JLabel implements TableCellRenderer {
021    public static final Color BGCOLOR_EMPTY_ROW = new Color(234, 234, 234);
022    public static final Color BGCOLOR_IN_JOSM_SELECTION = new Color(235, 255, 177);
023
024    public static final Color BGCOLOR_NOT_IN_OPPOSITE = new Color(255, 197, 197);
025    public static final Color BGCOLOR_DOUBLE_ENTRY = new Color(254, 226, 214);
026
027    /**
028     * constructor
029     */
030    public MemberTableCellRenderer() {
031        setIcon(null);
032        setOpaque(true);
033    }
034
035    /**
036     * reset the renderer
037     */
038    protected void reset() {
039        setBackground(UIManager.getColor("Table.background"));
040        setForeground(UIManager.getColor("Table.foreground"));
041        setBorder(null);
042        setIcon(null);
043        setToolTipText(null);
044    }
045
046    protected void renderBackgroundForeground(MemberTableModel model, OsmPrimitive primitive, boolean isSelected) {
047        Color bgc = UIManager.getColor("Table.background");
048        if (isSelected) {
049            bgc = UIManager.getColor("Table.selectionBackground");
050        } else if (primitive != null && model.isInJosmSelection(primitive)) {
051            bgc = BGCOLOR_IN_JOSM_SELECTION;
052        } else if (primitive != null && model.getNumMembersWithPrimitive(primitive) > 1) {
053            bgc = BGCOLOR_DOUBLE_ENTRY;
054        }
055        GuiHelper.setBackgroundReadable(this, bgc);
056    }
057
058    @Override
059    public abstract Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
060            boolean hasFocus, int row, int column);
061
062    /**
063     * replies the model
064     * @param table the table
065     * @return the table model
066     */
067    protected MemberTableModel getModel(JTable table) {
068        return (MemberTableModel) table.getModel();
069    }
070}