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