[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 |
|
---|
| 15 | /**
|
---|
| 16 | * This is the table cell renderer for cells for the table of tags
|
---|
| 17 | * in the tag editor dialog.
|
---|
| 18 | *
|
---|
| 19 | *
|
---|
| 20 | */
|
---|
| 21 | public class TagCellRenderer extends JLabel implements TableCellRenderer {
|
---|
| 22 | private Font fontStandard = null;
|
---|
| 23 | private Font fontItalic = null;
|
---|
| 24 |
|
---|
| 25 | public TagCellRenderer() {
|
---|
[2974] | 26 | fontStandard = UIManager.getFont("Table.font");
|
---|
[2512] | 27 | fontItalic = fontStandard.deriveFont(Font.ITALIC);
|
---|
| 28 | setOpaque(true);
|
---|
| 29 | setBorder(new EmptyBorder(5,5,5,5));
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * renders the name of a tag in the second column of
|
---|
| 34 | * the table
|
---|
| 35 | *
|
---|
| 36 | * @param tag the tag
|
---|
| 37 | */
|
---|
| 38 | protected void renderTagName(TagModel tag) {
|
---|
| 39 | setText(tag.getName());
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * renders the value of a a tag in the third column of
|
---|
| 44 | * the table
|
---|
| 45 | *
|
---|
| 46 | * @param tag the tag
|
---|
| 47 | */
|
---|
| 48 | protected void renderTagValue(TagModel tag) {
|
---|
| 49 | if (tag.getValueCount() == 0) {
|
---|
| 50 | setText("");
|
---|
| 51 | } else if (tag.getValueCount() == 1) {
|
---|
| 52 | setText(tag.getValues().get(0));
|
---|
| 53 | } else if (tag.getValueCount() > 1) {
|
---|
[2974] | 54 | setText(tr("multiple"));
|
---|
[2512] | 55 | setFont(fontItalic);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * resets the renderer
|
---|
| 61 | */
|
---|
| 62 | protected void resetRenderer() {
|
---|
| 63 | setText("");
|
---|
| 64 | setIcon(null);
|
---|
| 65 | setFont(fontStandard);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | protected TagEditorModel getModel(JTable table) {
|
---|
| 69 | return (TagEditorModel)table.getModel();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * replies the cell renderer component for a specific cell
|
---|
| 74 | *
|
---|
| 75 | * @param table the table
|
---|
| 76 | * @param value the value to be rendered
|
---|
| 77 | * @param isSelected true, if the value is selected
|
---|
| 78 | * @param hasFocus true, if the cell has focus
|
---|
| 79 | * @param rowIndex the row index
|
---|
| 80 | * @param vColIndex the column index
|
---|
| 81 | *
|
---|
| 82 | * @return the renderer component
|
---|
| 83 | */
|
---|
[6084] | 84 | @Override
|
---|
[2512] | 85 | public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
| 86 | boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
|
---|
| 87 | resetRenderer();
|
---|
[4072] | 88 | if (value == null)
|
---|
| 89 | return this;
|
---|
[2512] | 90 |
|
---|
| 91 | // set background color
|
---|
| 92 | //
|
---|
| 93 | if (isSelected){
|
---|
[2974] | 94 | setBackground(UIManager.getColor("Table.selectionBackground"));
|
---|
| 95 | setForeground(UIManager.getColor("Table.selectionForeground"));
|
---|
[2512] | 96 | } else {
|
---|
[3022] | 97 | setBackground(UIManager.getColor("Table.background")); // standard color
|
---|
| 98 | setForeground(UIManager.getColor("Table.foreground"));
|
---|
[2512] | 99 | }
|
---|
| 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 | }
|
---|