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

Last change on this file since 12042 was 11386, checked in by Don-vip, 7 years ago

sonar - squid:S1066 - Collapsible "if" statements should be merged

  • Property svn:eol-style set to native
File size: 3.3 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() == 0) {
55 setText("");
56 } else if (tag.getValueCount() == 1) {
57 setText(tag.getValues().get(0));
58 } else if (tag.getValueCount() > 1) {
59 setText(tr("multiple"));
60 setFont(fontItalic);
61 }
62 }
63
64 /**
65 * resets the renderer
66 */
67 protected void resetRenderer() {
68 setText("");
69 setIcon(null);
70 setFont(fontStandard);
71 }
72
73 /**
74 * replies the cell renderer component for a specific cell
75 *
76 * @param table the table
77 * @param value the value to be rendered
78 * @param isSelected true, if the value is selected
79 * @param hasFocus true, if the cell has focus
80 * @param rowIndex the row index
81 * @param vColIndex the column index
82 *
83 * @return the renderer component
84 */
85 @Override
86 public Component getTableCellRendererComponent(JTable table, Object value,
87 boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
88 resetRenderer();
89 if (value == null)
90 return this;
91
92 // set background color
93 //
94 if (isSelected) {
95 setBackground(UIManager.getColor("Table.selectionBackground"));
96 setForeground(UIManager.getColor("Table.selectionForeground"));
97 } else {
98 setBackground(UIManager.getColor("Table.background")); // standard color
99 setForeground(UIManager.getColor("Table.foreground"));
100 }
101
102 switch(vColIndex) {
103 case 0: renderTagName((TagModel) value); break;
104 case 1: renderTagValue((TagModel) value); break;
105 default: throw new JosmRuntimeException("unexpected index in switch statement");
106 }
107 if (hasFocus && isSelected && table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1
108 && table.getEditorComponent() != null) {
109 table.getEditorComponent().requestFocusInWindow();
110 }
111 return this;
112 }
113}
Note: See TracBrowser for help on using the repository browser.