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
015/**
016 * This is the table cell renderer for cells for the table of tags
017 * in the tag editor dialog.
018 *
019 *
020 */
021public class TagCellRenderer extends JLabel implements TableCellRenderer  {
022    private Font fontStandard;
023    private Font fontItalic;
024
025    /**
026     * Constructs a new {@code TagCellRenderer}.
027     */
028    public TagCellRenderer() {
029        fontStandard = UIManager.getFont("Table.font");
030        fontItalic = fontStandard.deriveFont(Font.ITALIC);
031        setOpaque(true);
032        setBorder(new EmptyBorder(5, 5, 5, 5));
033    }
034
035    /**
036     * renders the name of a tag in the second column of
037     * the table
038     *
039     * @param tag  the tag
040     */
041    protected void renderTagName(TagModel tag) {
042        setText(tag.getName());
043    }
044
045    /**
046     * renders the value of a a tag in the third column of
047     * the table
048     *
049     * @param tag  the  tag
050     */
051    protected void renderTagValue(TagModel tag) {
052        if (tag.getValueCount() == 0) {
053            setText("");
054        } else if (tag.getValueCount() == 1) {
055            setText(tag.getValues().get(0));
056        } else if (tag.getValueCount() >  1) {
057            setText(tr("multiple"));
058            setFont(fontItalic);
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
104        default: throw new RuntimeException("unexpected index in switch statement");
105        }
106        if (hasFocus && isSelected) {
107            if (table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1) {
108                if (table.getEditorComponent() != null) {
109                    table.getEditorComponent().requestFocusInWindow();
110                }
111            }
112        }
113        return this;
114    }
115}