source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/TagCellRenderer.java

Last change on this file was 19050, checked in by taylor.smock, 4 weeks ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Font;
8
9import javax.swing.JLabel;
10import javax.swing.JTable;
11import javax.swing.UIManager;
12import javax.swing.border.EmptyBorder;
13import javax.swing.table.TableCellRenderer;
14
15import org.openstreetmap.josm.tools.JosmRuntimeException;
16
17/**
18 * This is the table cell renderer for cells for the table of tags
19 * in the tag editor dialog.
20 *
21 *
22 */
23public class TagCellRenderer extends JLabel implements TableCellRenderer {
24 private final Font fontStandard;
25 private final Font fontItalic;
26
27 /**
28 * Constructs a new {@code TagCellRenderer}.
29 */
30 public TagCellRenderer() {
31 fontStandard = UIManager.getFont("Table.font");
32 fontItalic = fontStandard.deriveFont(Font.ITALIC);
33 setOpaque(true);
34 setBorder(new EmptyBorder(5, 5, 5, 5));
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) {
54 if (tag.getValueCount() > 1) {
55 setText(tr("multiple"));
56 setFont(fontItalic);
57 } else {
58 setText(tag.getValue());
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 */
83 @Override
84 public Component getTableCellRendererComponent(JTable table, Object value,
85 boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
86 resetRenderer();
87 if (value == null)
88 return this;
89
90 // set background color
91 //
92 if (isSelected) {
93 setBackground(UIManager.getColor("Table.selectionBackground"));
94 setForeground(UIManager.getColor("Table.selectionForeground"));
95 } else {
96 setBackground(UIManager.getColor("Table.background")); // standard color
97 setForeground(UIManager.getColor("Table.foreground"));
98 }
99
100 switch (vColIndex) {
101 case 0: renderTagName((TagModel) value); break;
102 case 1: renderTagValue((TagModel) value); break;
103 default: throw new JosmRuntimeException("unexpected index in switch statement");
104 }
105 if (hasFocus && isSelected && table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1
106 && table.getEditorComponent() != null) {
107 table.getEditorComponent().requestFocusInWindow();
108 }
109 return this;
110 }
111}
Note: See TracBrowser for help on using the repository browser.