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.BorderFactory;
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JTable;
|
---|
10 | import javax.swing.SwingConstants;
|
---|
11 | import javax.swing.table.TableCellRenderer;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
15 |
|
---|
16 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * The {@link TableCellRenderer} for a list of tags in {@link HistoryBrowser}
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class TagTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
23 | /**
|
---|
24 | * The background color for a selected row that has the focus.
|
---|
25 | */
|
---|
26 | public static final Color BGCOLOR_SELECTED_FOCUS = new Color(0xff8faaff);
|
---|
27 | /**
|
---|
28 | * The background color for a selected row while the table is not focused.
|
---|
29 | */
|
---|
30 | public static final Color BGCOLOR_SELECTED = new Color(0xffafc2ff);
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code TagTableCellRenderer}.
|
---|
34 | */
|
---|
35 | public TagTableCellRenderer() {
|
---|
36 | setOpaque(true);
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
41 | int row, int column) {
|
---|
42 |
|
---|
43 | if (value == null)
|
---|
44 | return this;
|
---|
45 |
|
---|
46 | String key = (String) value;
|
---|
47 | TagTableModel model = getTagTableModel(table);
|
---|
48 |
|
---|
49 | String text = "";
|
---|
50 | String tooltip = null;
|
---|
51 | setBorder(null);
|
---|
52 | if (model.hasTag(key)) {
|
---|
53 | switch(column) {
|
---|
54 | case TagTableColumnModel.COLUMN_KEY:
|
---|
55 | // the name column
|
---|
56 | text = key;
|
---|
57 | break;
|
---|
58 | case TagTableColumnModel.COLUMN_VALUE:
|
---|
59 | // the value column
|
---|
60 | text = model.getValue(key);
|
---|
61 | break;
|
---|
62 | case TagTableColumnModel.COLUMN_VERSION:
|
---|
63 | HistoryOsmPrimitive primitive = model.getWhichChangedTag(key);
|
---|
64 | if (primitive != null) {
|
---|
65 | text = model.getVersionString(primitive);
|
---|
66 | tooltip = tr("Key ''{0}'' was changed in version {1}", key, primitive.getVersion());
|
---|
67 | setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, model.getVersionColor(primitive)));
|
---|
68 | }
|
---|
69 | break;
|
---|
70 | default: // Do nothing
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | setText(text);
|
---|
75 | setToolTipText(tooltip != null ? tooltip : text);
|
---|
76 | setHorizontalAlignment(column == TagTableColumnModel.COLUMN_VERSION ? SwingConstants.TRAILING : SwingConstants.LEADING);
|
---|
77 | TwoColumnDiff.Item.DiffItemType diffItemType = model.getDiffItemType(key, column == TagTableColumnModel.COLUMN_VALUE);
|
---|
78 | GuiHelper.setBackgroundReadable(this, diffItemType.getColor(isSelected, table.hasFocus()));
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected TagTableModel getTagTableModel(JTable table) {
|
---|
83 | return (TagTableModel) table.getModel();
|
---|
84 | }
|
---|
85 | }
|
---|