1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Font;
|
---|
9 |
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JTable;
|
---|
12 | import javax.swing.UIManager;
|
---|
13 | import javax.swing.table.TableCellRenderer;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * The {@link TableCellRenderer} for a list of tags in {@link HistoryBrowser}
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class TagTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
20 | public static final Color BGCOLOR_DIFFERENCE = new Color(255,197,197);
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Constructs a new {@code TagTableCellRenderer}.
|
---|
24 | */
|
---|
25 | public TagTableCellRenderer() {
|
---|
26 | setOpaque(true);
|
---|
27 | }
|
---|
28 |
|
---|
29 | protected void renderName(String key, HistoryBrowserModel.TagTableModel model, boolean isSelected) {
|
---|
30 | String text = key;
|
---|
31 | Color bgColor = UIManager.getColor("Table.background");
|
---|
32 | Color fgColor = UIManager.getColor("Table.foreground");
|
---|
33 | Font font = UIManager.getFont("Table.font");
|
---|
34 | if (! model.hasTag(key)) {
|
---|
35 | text = tr("not present");
|
---|
36 | bgColor = BGCOLOR_DIFFERENCE;
|
---|
37 | font = font.deriveFont(Font.ITALIC);
|
---|
38 | } else if (!model.oppositeHasTag(key)) {
|
---|
39 | bgColor = BGCOLOR_DIFFERENCE;
|
---|
40 | }
|
---|
41 | if (isSelected) {
|
---|
42 | bgColor = UIManager.getColor("Table.backgroundSelected");
|
---|
43 | fgColor = UIManager.getColor("Table.foregroundSelected");
|
---|
44 | }
|
---|
45 |
|
---|
46 | setText(text);
|
---|
47 | setToolTipText(text);
|
---|
48 | setBackground(bgColor);
|
---|
49 | setForeground(fgColor);
|
---|
50 | setFont(font);
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected void renderValue(String key, HistoryBrowserModel.TagTableModel model, boolean isSelected) {
|
---|
54 | String text = "";
|
---|
55 | Color bgColor = UIManager.getColor("Table.background");
|
---|
56 | Color fgColor = UIManager.getColor("Table.foreground");
|
---|
57 | Font font = UIManager.getFont("Table.font");
|
---|
58 | if (! model.hasTag(key)) {
|
---|
59 | text = tr("not present");
|
---|
60 | bgColor = BGCOLOR_DIFFERENCE;
|
---|
61 | font = font.deriveFont(Font.ITALIC);
|
---|
62 | } else {
|
---|
63 | text = model.getValue(key);
|
---|
64 | if (!model.hasSameValueAsOpposite(key)) {
|
---|
65 | bgColor = BGCOLOR_DIFFERENCE;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | if (isSelected) {
|
---|
69 | bgColor = UIManager.getColor("Table.backgroundSelected");
|
---|
70 | fgColor = UIManager.getColor("Table.foregroundSelected");
|
---|
71 | }
|
---|
72 |
|
---|
73 | setText(text);
|
---|
74 | setToolTipText(text);
|
---|
75 | setBackground(bgColor);
|
---|
76 | setForeground(fgColor);
|
---|
77 | setFont(font);
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
82 | int row, int column) {
|
---|
83 |
|
---|
84 | if (value == null)
|
---|
85 | return this;
|
---|
86 |
|
---|
87 | String key = (String)value;
|
---|
88 | HistoryBrowserModel.TagTableModel model = getTagTableModel(table);
|
---|
89 |
|
---|
90 | switch(column) {
|
---|
91 | case 0:
|
---|
92 | // the name column
|
---|
93 | renderName(key, model, isSelected);
|
---|
94 | break;
|
---|
95 | case 1:
|
---|
96 | // the value column
|
---|
97 | renderValue(key, model, isSelected);
|
---|
98 | break;
|
---|
99 | }
|
---|
100 |
|
---|
101 | return this;
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected HistoryBrowserModel.TagTableModel getTagTableModel(JTable table) {
|
---|
105 | return (HistoryBrowserModel.TagTableModel) table.getModel();
|
---|
106 | }
|
---|
107 | }
|
---|