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

Last change on this file since 10378 was 10378, checked in by Don-vip, 8 years ago

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

  • 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
15/**
16 * This is the table cell renderer for cells for the table of tags
17 * in the tag editor dialog.
18 *
19 *
20 */
21public class TagCellRenderer extends JLabel implements TableCellRenderer {
22 private final Font fontStandard;
23 private final Font fontItalic;
24
25 /**
26 * Constructs a new {@code TagCellRenderer}.
27 */
28 public TagCellRenderer() {
29 fontStandard = UIManager.getFont("Table.font");
30 fontItalic = fontStandard.deriveFont(Font.ITALIC);
31 setOpaque(true);
32 setBorder(new EmptyBorder(5, 5, 5, 5));
33 }
34
35 /**
36 * renders the name of a tag in the second column of
37 * the table
38 *
39 * @param tag the tag
40 */
41 protected void renderTagName(TagModel tag) {
42 setText(tag.getName());
43 }
44
45 /**
46 * renders the value of a a tag in the third column of
47 * the table
48 *
49 * @param tag the tag
50 */
51 protected void renderTagValue(TagModel tag) {
52 if (tag.getValueCount() == 0) {
53 setText("");
54 } else if (tag.getValueCount() == 1) {
55 setText(tag.getValues().get(0));
56 } else if (tag.getValueCount() > 1) {
57 setText(tr("multiple"));
58 setFont(fontItalic);
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
104 default: throw new RuntimeException("unexpected index in switch statement");
105 }
106 if (hasFocus && isSelected) {
107 if (table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1) {
108 if (table.getEditorComponent() != null) {
109 table.getEditorComponent().requestFocusInWindow();
110 }
111 }
112 }
113 return this;
114 }
115}
Note: See TracBrowser for help on using the repository browser.