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;
|
---|
11 | import javax.swing.UIManager;
|
---|
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() {
|
---|
26 | fontStandard = UIManager.getFont("Table.font");
|
---|
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) {
|
---|
54 | setText(tr("multiple"));
|
---|
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 | */
|
---|
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 | }
|
---|