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 = null;
023    private Font fontItalic = null;
024
025    public TagCellRenderer() {
026        fontStandard = UIManager.getFont("Table.font");
027        fontItalic = fontStandard.deriveFont(Font.ITALIC);
028        setOpaque(true);
029        setBorder(new EmptyBorder(5,5,5,5));
030    }
031
032    /**
033     * renders the name of a tag in the second column of
034     * the table
035     *
036     * @param tag  the tag
037     */
038    protected void renderTagName(TagModel tag) {
039        setText(tag.getName());
040    }
041
042    /**
043     * renders the value of a a tag in the third column of
044     * the table
045     *
046     * @param tag  the  tag
047     */
048    protected void renderTagValue(TagModel tag) {
049        if (tag.getValueCount() == 0) {
050            setText("");
051        } else if (tag.getValueCount() == 1) {
052            setText(tag.getValues().get(0));
053        } else if (tag.getValueCount() >  1) {
054            setText(tr("multiple"));
055            setFont(fontItalic);
056        }
057    }
058
059    /**
060     * resets the renderer
061     */
062    protected void resetRenderer() {
063        setText("");
064        setIcon(null);
065        setFont(fontStandard);
066    }
067
068    protected TagEditorModel getModel(JTable table) {
069        return (TagEditorModel)table.getModel();
070    }
071
072    /**
073     * replies the cell renderer component for a specific cell
074     *
075     * @param table  the table
076     * @param value the value to be rendered
077     * @param isSelected  true, if the value is selected
078     * @param hasFocus true, if the cell has focus
079     * @param rowIndex the row index
080     * @param vColIndex the column index
081     *
082     * @return the renderer component
083     */
084    @Override
085    public Component getTableCellRendererComponent(JTable table, Object value,
086            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
087        resetRenderer();
088        if (value == null)
089            return this;
090
091        // set background color
092        //
093        if (isSelected){
094            setBackground(UIManager.getColor("Table.selectionBackground"));
095            setForeground(UIManager.getColor("Table.selectionForeground"));
096        } else {
097            setBackground(UIManager.getColor("Table.background")); // standard color
098            setForeground(UIManager.getColor("Table.foreground"));
099        }
100
101        switch(vColIndex) {
102        case 0: renderTagName((TagModel)value); break;
103        case 1: renderTagValue((TagModel)value); break;
104
105        default: throw new RuntimeException("unexpected index in switch statement");
106        }
107        if (hasFocus && isSelected) {
108            if (table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1) {
109                if (table.getEditorComponent() != null) {
110                    table.getEditorComponent().requestFocusInWindow();
111                }
112            }
113        }
114        return this;
115    }
116}