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 | import java.util.logging.Logger;
|
---|
9 |
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JTable;
|
---|
12 | import javax.swing.UIManager;
|
---|
13 | import javax.swing.border.EmptyBorder;
|
---|
14 | import javax.swing.table.TableCellRenderer;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This is the table cell renderer for cells for the table of tags
|
---|
18 | * in the tag editor dialog.
|
---|
19 | *
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class TagCellRenderer extends JLabel implements TableCellRenderer {
|
---|
23 | @SuppressWarnings("unused")
|
---|
24 | private static final Logger logger = Logger.getLogger(TagCellRenderer.class.getName());
|
---|
25 |
|
---|
26 | private Font fontStandard = null;
|
---|
27 | private Font fontItalic = null;
|
---|
28 |
|
---|
29 | public TagCellRenderer() {
|
---|
30 | fontStandard = UIManager.getFont("Table.font");
|
---|
31 | fontItalic = fontStandard.deriveFont(Font.ITALIC);
|
---|
32 | setOpaque(true);
|
---|
33 | setBorder(new EmptyBorder(5,5,5,5));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * renders the name of a tag in the second column of
|
---|
38 | * the table
|
---|
39 | *
|
---|
40 | * @param tag the tag
|
---|
41 | */
|
---|
42 | protected void renderTagName(TagModel tag) {
|
---|
43 | setText(tag.getName());
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * renders the value of a a tag in the third column of
|
---|
48 | * the table
|
---|
49 | *
|
---|
50 | * @param tag the tag
|
---|
51 | */
|
---|
52 | protected void renderTagValue(TagModel tag) {
|
---|
53 | if (tag.getValueCount() == 0) {
|
---|
54 | setText("");
|
---|
55 | } else if (tag.getValueCount() == 1) {
|
---|
56 | setText(tag.getValues().get(0));
|
---|
57 | } else if (tag.getValueCount() > 1) {
|
---|
58 | setText(tr("multiple"));
|
---|
59 | setFont(fontItalic);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * resets the renderer
|
---|
65 | */
|
---|
66 | protected void resetRenderer() {
|
---|
67 | setText("");
|
---|
68 | setIcon(null);
|
---|
69 | setFont(fontStandard);
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected TagEditorModel getModel(JTable table) {
|
---|
73 | return (TagEditorModel)table.getModel();
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * renders the background color.
|
---|
78 | *
|
---|
79 | * @param tagModel the tag model
|
---|
80 | * @param model the tag editor model
|
---|
81 | */
|
---|
82 | protected void renderBackgroundColor(TagModel tagModel, TagEditorModel model) {
|
---|
83 | setBackground(UIManager.getColor("Table.background")); // standard color
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * replies the cell renderer component for a specific cell
|
---|
88 | *
|
---|
89 | * @param table the table
|
---|
90 | * @param value the value to be rendered
|
---|
91 | * @param isSelected true, if the value is selected
|
---|
92 | * @param hasFocus true, if the cell has focus
|
---|
93 | * @param rowIndex the row index
|
---|
94 | * @param vColIndex the column index
|
---|
95 | *
|
---|
96 | * @return the renderer component
|
---|
97 | */
|
---|
98 | public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
99 | boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
|
---|
100 | resetRenderer();
|
---|
101 |
|
---|
102 | // set background color
|
---|
103 | //
|
---|
104 | if (isSelected){
|
---|
105 | setBackground(UIManager.getColor("Table.selectionBackground"));
|
---|
106 | setForeground(UIManager.getColor("Table.selectionForeground"));
|
---|
107 | } else {
|
---|
108 | renderBackgroundColor(getModel(table).get(rowIndex), getModel(table));
|
---|
109 | }
|
---|
110 |
|
---|
111 | switch(vColIndex) {
|
---|
112 | case 0: renderTagName((TagModel)value); break;
|
---|
113 | case 1: renderTagValue((TagModel)value); break;
|
---|
114 |
|
---|
115 | default: throw new RuntimeException("unexpected index in switch statement");
|
---|
116 | }
|
---|
117 | if (hasFocus && isSelected) {
|
---|
118 | if (table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1) {
|
---|
119 | if (table.getEditorComponent() != null) {
|
---|
120 | table.getEditorComponent().requestFocusInWindow();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return this;
|
---|
125 | }
|
---|
126 | }
|
---|