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 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * The {@link TableCellRenderer} for a list of tags in {@link HistoryBrowser}
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class TagTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
19 | /**
|
---|
20 | * The background color for a selected row that has the focus.
|
---|
21 | */
|
---|
22 | public static final Color BGCOLOR_SELECTED_FOCUS = new Color(0xff8faaff);
|
---|
23 | /**
|
---|
24 | * The background color for a selected row while the table is not focused.
|
---|
25 | */
|
---|
26 | public static final Color BGCOLOR_SELECTED = new Color(0xffafc2ff);
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code TagTableCellRenderer}.
|
---|
30 | */
|
---|
31 | public TagTableCellRenderer() {
|
---|
32 | setOpaque(true);
|
---|
33 | }
|
---|
34 |
|
---|
35 | protected void setBackgroundReadable(String key, TagTableModel model, boolean isSelected, boolean hasFocus, boolean isValue) {
|
---|
36 | Color bgColor = UIManager.getColor("Table.background");
|
---|
37 | if ((!model.hasTag(key) && model.isCurrentPointInTime())
|
---|
38 | || (!model.oppositeHasTag(key) && model.isReferencePointInTime())) {
|
---|
39 | bgColor = TwoColumnDiff.Item.DiffItemType.DELETED.getColor();
|
---|
40 | } else if ((!model.oppositeHasTag(key) && model.isCurrentPointInTime())
|
---|
41 | || (!model.hasTag(key) && model.isReferencePointInTime())) {
|
---|
42 | bgColor = TwoColumnDiff.Item.DiffItemType.INSERTED.getColor();
|
---|
43 | } else if (isValue && model.hasTag(key) && model.oppositeHasTag(key) && !model.hasSameValueAsOpposite(key)) {
|
---|
44 | bgColor = TwoColumnDiff.Item.DiffItemType.CHANGED.getColor();
|
---|
45 | }
|
---|
46 | if (isSelected) {
|
---|
47 | if (hasFocus) {
|
---|
48 | bgColor = BGCOLOR_SELECTED_FOCUS;
|
---|
49 | } else {
|
---|
50 | bgColor = BGCOLOR_SELECTED;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | GuiHelper.setBackgroundReadable(this, bgColor);
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
59 | int row, int column) {
|
---|
60 |
|
---|
61 | if (value == null)
|
---|
62 | return this;
|
---|
63 |
|
---|
64 | String key = (String) value;
|
---|
65 | TagTableModel model = getTagTableModel(table);
|
---|
66 |
|
---|
67 | String text = "";
|
---|
68 | if (model.hasTag(key)) {
|
---|
69 | switch(column) {
|
---|
70 | case TagTableColumnModel.COLUMN_KEY:
|
---|
71 | // the name column
|
---|
72 | text = key;
|
---|
73 | break;
|
---|
74 | case TagTableColumnModel.COLUMN_VALUE:
|
---|
75 | // the value column
|
---|
76 | text = model.getValue(key);
|
---|
77 | break;
|
---|
78 | default: // Do nothing
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | setText(text);
|
---|
83 | setToolTipText(text);
|
---|
84 | setBackgroundReadable(key, model, isSelected, table.hasFocus(), column == TagTableColumnModel.COLUMN_VALUE);
|
---|
85 | return this;
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected TagTableModel getTagTableModel(JTable table) {
|
---|
89 | return (TagTableModel) table.getModel();
|
---|
90 | }
|
---|
91 | }
|
---|