001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.properties;
003
004import static org.openstreetmap.josm.tools.I18n.marktr;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.Color;
008import java.awt.Component;
009import java.awt.Font;
010import java.util.Map;
011
012import javax.swing.JLabel;
013import javax.swing.JTable;
014import javax.swing.UIDefaults;
015import javax.swing.table.DefaultTableCellRenderer;
016
017import org.openstreetmap.josm.Main;
018import org.openstreetmap.josm.data.osm.OsmPrimitive;
019
020/**
021 * Cell renderer of tags table.
022 * @since 6314
023 */
024public class PropertiesCellRenderer extends DefaultTableCellRenderer {
025
026    private void setColors(Component c, String key, boolean isSelected) {
027        UIDefaults defaults = javax.swing.UIManager.getDefaults();
028        if (OsmPrimitive.getDiscardableKeys().contains(key)) {
029            if (isSelected) {
030                c.setForeground(Main.pref.getColor(marktr("Discardable key: selection Foreground"), Color.GRAY));
031                c.setBackground(Main.pref.getColor(marktr("Discardable key: selection Background"), defaults.getColor("Table.selectionBackground")));
032            } else {
033                c.setForeground(Main.pref.getColor(marktr("Discardable key: foreground"), Color.GRAY));
034                c.setBackground(Main.pref.getColor(marktr("Discardable key: background"), defaults.getColor("Table.background")));
035            }
036        } else {
037            c.setForeground(defaults.getColor("Table."+(isSelected ? "selectionF" : "f")+"oreground"));
038            c.setBackground(defaults.getColor("Table."+(isSelected ? "selectionB" : "b")+"ackground"));
039        }
040    }
041    
042    @Override 
043    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
044        Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
045        if (value == null)
046            return this;
047        if (c instanceof JLabel) {
048            String str = null;
049            if (value instanceof String) {
050                str = (String) value;
051            } else if (value instanceof Map<?, ?>) {
052                Map<?, ?> v = (Map<?, ?>) value;
053                if (v.size() != 1) {
054                    str=tr("<different>");
055                    c.setFont(c.getFont().deriveFont(Font.ITALIC));
056                } else {
057                    final Map.Entry<?, ?> entry = v.entrySet().iterator().next();
058                    str = (String) entry.getKey();
059                }
060            }
061            ((JLabel)c).putClientProperty("html.disable", Boolean.TRUE); // Fix #8730
062            ((JLabel)c).setText(str);
063            if (Main.pref.getBoolean("display.discardable-keys", false)) {
064                String key = null;
065                if (column == 0) {
066                    key = str;
067                } else if (column == 1) {
068                    Object value0 = table.getModel().getValueAt(row, 0);
069                    if (value0 instanceof String) {
070                        key = (String) value0;
071                    }
072                }
073                setColors(c, key, isSelected);
074            }
075        }
076        return c;
077    }
078}