001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.tagging;
003
004 import static org.openstreetmap.josm.tools.I18n.tr;
005
006 import java.awt.Component;
007 import java.awt.Font;
008
009 import javax.swing.JLabel;
010 import javax.swing.JTable;
011 import javax.swing.UIManager;
012 import javax.swing.border.EmptyBorder;
013 import 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 */
021 public 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 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 }