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() == 0) { 055 setText(""); 056 } else if (tag.getValueCount() == 1) { 057 setText(tag.getValues().get(0)); 058 } else if (tag.getValueCount() > 1) { 059 setText(tr("multiple")); 060 setFont(fontItalic); 061 } 062 } 063 064 /** 065 * resets the renderer 066 */ 067 protected void resetRenderer() { 068 setText(""); 069 setIcon(null); 070 setFont(fontStandard); 071 } 072 073 /** 074 * replies the cell renderer component for a specific cell 075 * 076 * @param table the table 077 * @param value the value to be rendered 078 * @param isSelected true, if the value is selected 079 * @param hasFocus true, if the cell has focus 080 * @param rowIndex the row index 081 * @param vColIndex the column index 082 * 083 * @return the renderer component 084 */ 085 @Override 086 public Component getTableCellRendererComponent(JTable table, Object value, 087 boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) { 088 resetRenderer(); 089 if (value == null) 090 return this; 091 092 // set background color 093 // 094 if (isSelected) { 095 setBackground(UIManager.getColor("Table.selectionBackground")); 096 setForeground(UIManager.getColor("Table.selectionForeground")); 097 } else { 098 setBackground(UIManager.getColor("Table.background")); // standard color 099 setForeground(UIManager.getColor("Table.foreground")); 100 } 101 102 switch(vColIndex) { 103 case 0: renderTagName((TagModel) value); break; 104 case 1: renderTagValue((TagModel) value); break; 105 default: throw new JosmRuntimeException("unexpected index in switch statement"); 106 } 107 if (hasFocus && isSelected && table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1 108 && table.getEditorComponent() != null) { 109 table.getEditorComponent().requestFocusInWindow(); 110 } 111 return this; 112 } 113}