[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.tagging;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Component;
|
---|
| 7 | import java.awt.Font;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.JLabel;
|
---|
| 10 | import javax.swing.JTable;
|
---|
[2974] | 11 | import javax.swing.UIManager;
|
---|
[2512] | 12 | import javax.swing.border.EmptyBorder;
|
---|
| 13 | import javax.swing.table.TableCellRenderer;
|
---|
| 14 |
|
---|
[11374] | 15 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
| 16 |
|
---|
[2512] | 17 | /**
|
---|
| 18 | * This is the table cell renderer for cells for the table of tags
|
---|
| 19 | * in the tag editor dialog.
|
---|
| 20 | *
|
---|
| 21 | *
|
---|
| 22 | */
|
---|
[10378] | 23 | public class TagCellRenderer extends JLabel implements TableCellRenderer {
|
---|
[9078] | 24 | private final Font fontStandard;
|
---|
| 25 | private final Font fontItalic;
|
---|
[2512] | 26 |
|
---|
[8510] | 27 | /**
|
---|
| 28 | * Constructs a new {@code TagCellRenderer}.
|
---|
| 29 | */
|
---|
[2512] | 30 | public TagCellRenderer() {
|
---|
[2974] | 31 | fontStandard = UIManager.getFont("Table.font");
|
---|
[2512] | 32 | fontItalic = fontStandard.deriveFont(Font.ITALIC);
|
---|
| 33 | setOpaque(true);
|
---|
[8510] | 34 | setBorder(new EmptyBorder(5, 5, 5, 5));
|
---|
[2512] | 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * renders the name of a tag in the second column of
|
---|
| 39 | * the table
|
---|
| 40 | *
|
---|
| 41 | * @param tag the tag
|
---|
| 42 | */
|
---|
| 43 | protected void renderTagName(TagModel tag) {
|
---|
| 44 | setText(tag.getName());
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * renders the value of a a tag in the third column of
|
---|
| 49 | * the table
|
---|
| 50 | *
|
---|
| 51 | * @param tag the tag
|
---|
| 52 | */
|
---|
| 53 | protected void renderTagValue(TagModel tag) {
|
---|
[17595] | 54 | if (tag.getValueCount() > 1) {
|
---|
[2974] | 55 | setText(tr("multiple"));
|
---|
[2512] | 56 | setFont(fontItalic);
|
---|
[17595] | 57 | } else {
|
---|
| 58 | setText(tag.getValue());
|
---|
[2512] | 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * resets the renderer
|
---|
| 64 | */
|
---|
| 65 | protected void resetRenderer() {
|
---|
| 66 | setText("");
|
---|
| 67 | setIcon(null);
|
---|
| 68 | setFont(fontStandard);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * replies the cell renderer component for a specific cell
|
---|
| 73 | *
|
---|
| 74 | * @param table the table
|
---|
| 75 | * @param value the value to be rendered
|
---|
| 76 | * @param isSelected true, if the value is selected
|
---|
| 77 | * @param hasFocus true, if the cell has focus
|
---|
| 78 | * @param rowIndex the row index
|
---|
| 79 | * @param vColIndex the column index
|
---|
| 80 | *
|
---|
| 81 | * @return the renderer component
|
---|
| 82 | */
|
---|
[6084] | 83 | @Override
|
---|
[2512] | 84 | public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
| 85 | boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
|
---|
| 86 | resetRenderer();
|
---|
[4072] | 87 | if (value == null)
|
---|
| 88 | return this;
|
---|
[2512] | 89 |
|
---|
| 90 | // set background color
|
---|
| 91 | //
|
---|
[8510] | 92 | if (isSelected) {
|
---|
[2974] | 93 | setBackground(UIManager.getColor("Table.selectionBackground"));
|
---|
| 94 | setForeground(UIManager.getColor("Table.selectionForeground"));
|
---|
[2512] | 95 | } else {
|
---|
[3022] | 96 | setBackground(UIManager.getColor("Table.background")); // standard color
|
---|
| 97 | setForeground(UIManager.getColor("Table.foreground"));
|
---|
[2512] | 98 | }
|
---|
| 99 |
|
---|
[19050] | 100 | switch (vColIndex) {
|
---|
[11386] | 101 | case 0: renderTagName((TagModel) value); break;
|
---|
| 102 | case 1: renderTagValue((TagModel) value); break;
|
---|
| 103 | default: throw new JosmRuntimeException("unexpected index in switch statement");
|
---|
[2512] | 104 | }
|
---|
[11386] | 105 | if (hasFocus && isSelected && table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1
|
---|
| 106 | && table.getEditorComponent() != null) {
|
---|
| 107 | table.getEditorComponent().requestFocusInWindow();
|
---|
[2512] | 108 | }
|
---|
| 109 | return this;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|