001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.Font;
008
009import javax.swing.JLabel;
010import javax.swing.JTable;
011import javax.swing.UIManager;
012import javax.swing.border.EmptyBorder;
013import javax.swing.table.TableCellRenderer;
014
015import org.openstreetmap.josm.tools.JosmRuntimeException;
016
017/**
018 * This is the table cell renderer for cells for the table of tags
019 * in the tag editor dialog.
020 *
021 *
022 */
023public class TagCellRenderer extends JLabel implements TableCellRenderer {
024    private final Font fontStandard;
025    private final Font fontItalic;
026
027    /**
028     * Constructs a new {@code TagCellRenderer}.
029     */
030    public TagCellRenderer() {
031        fontStandard = UIManager.getFont("Table.font");
032        fontItalic = fontStandard.deriveFont(Font.ITALIC);
033        setOpaque(true);
034        setBorder(new EmptyBorder(5, 5, 5, 5));
035    }
036
037    /**
038     * renders the name of a tag in the second column of
039     * the table
040     *
041     * @param tag  the tag
042     */
043    protected void renderTagName(TagModel tag) {
044        setText(tag.getName());
045    }
046
047    /**
048     * renders the value of a a tag in the third column of
049     * the table
050     *
051     * @param tag  the  tag
052     */
053    protected void renderTagValue(TagModel tag) {
054        if (tag.getValueCount() > 1) {
055            setText(tr("multiple"));
056            setFont(fontItalic);
057        } else {
058            setText(tag.getValue());
059        }
060    }
061
062    /**
063     * resets the renderer
064     */
065    protected void resetRenderer() {
066        setText("");
067        setIcon(null);
068        setFont(fontStandard);
069    }
070
071    /**
072     * replies the cell renderer component for a specific cell
073     *
074     * @param table  the table
075     * @param value the value to be rendered
076     * @param isSelected  true, if the value is selected
077     * @param hasFocus true, if the cell has focus
078     * @param rowIndex the row index
079     * @param vColIndex the column index
080     *
081     * @return the renderer component
082     */
083    @Override
084    public Component getTableCellRendererComponent(JTable table, Object value,
085            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
086        resetRenderer();
087        if (value == null)
088            return this;
089
090        // set background color
091        //
092        if (isSelected) {
093            setBackground(UIManager.getColor("Table.selectionBackground"));
094            setForeground(UIManager.getColor("Table.selectionForeground"));
095        } else {
096            setBackground(UIManager.getColor("Table.background")); // standard color
097            setForeground(UIManager.getColor("Table.foreground"));
098        }
099
100        switch(vColIndex) {
101            case 0: renderTagName((TagModel) value); break;
102            case 1: renderTagValue((TagModel) value); break;
103            default: throw new JosmRuntimeException("unexpected index in switch statement");
104        }
105        if (hasFocus && isSelected && table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1
106                && table.getEditorComponent() != null) {
107            table.getEditorComponent().requestFocusInWindow();
108        }
109        return this;
110    }
111}