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