1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.Color;
|
---|
9 | import java.awt.Component;
|
---|
10 | import java.awt.Font;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JTable;
|
---|
15 | import javax.swing.UIDefaults;
|
---|
16 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Cell renderer of tags table.
|
---|
23 | * @since 6314
|
---|
24 | */
|
---|
25 | public class PropertiesCellRenderer extends DefaultTableCellRenderer {
|
---|
26 |
|
---|
27 | private void setColors(Component c, String key, boolean isSelected) {
|
---|
28 | UIDefaults defaults = javax.swing.UIManager.getDefaults();
|
---|
29 | if (OsmPrimitive.getDiscardableKeys().contains(key)) {
|
---|
30 | if (isSelected) {
|
---|
31 | c.setForeground(Main.pref.getColor(marktr("Discardable key: selection Foreground"), Color.GRAY));
|
---|
32 | c.setBackground(Main.pref.getColor(marktr("Discardable key: selection Background"), defaults.getColor("Table.selectionBackground")));
|
---|
33 | } else {
|
---|
34 | c.setForeground(Main.pref.getColor(marktr("Discardable key: foreground"), Color.GRAY));
|
---|
35 | c.setBackground(Main.pref.getColor(marktr("Discardable key: background"), defaults.getColor("Table.background")));
|
---|
36 | }
|
---|
37 | } else {
|
---|
38 | c.setForeground(defaults.getColor("Table."+(isSelected ? "selectionF" : "f")+"oreground"));
|
---|
39 | c.setBackground(defaults.getColor("Table."+(isSelected ? "selectionB" : "b")+"ackground"));
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
45 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
46 | if (value == null)
|
---|
47 | return this;
|
---|
48 | if (c instanceof JLabel) {
|
---|
49 | String str = null;
|
---|
50 | if (value instanceof String) {
|
---|
51 | str = (String) value;
|
---|
52 | } else if (value instanceof Map<?, ?>) {
|
---|
53 | Map<?, ?> v = (Map<?, ?>) value;
|
---|
54 | if (v.size() != 1) { // Multiple values: give user a short summary of the values
|
---|
55 | Integer blankCount;
|
---|
56 | Integer otherCount;
|
---|
57 | if (v.get("") == null) {
|
---|
58 | blankCount = 0;
|
---|
59 | otherCount = v.size();
|
---|
60 | } else {
|
---|
61 | blankCount = (Integer)v.get("");
|
---|
62 | otherCount = v.size()-1;
|
---|
63 | }
|
---|
64 | StringBuilder sb = new StringBuilder("<");
|
---|
65 | if (otherCount == 1) {
|
---|
66 | for (Map.Entry<?, ?> entry : v.entrySet()) { // Find the non-blank value in the map
|
---|
67 | if ( entry.getKey() != "") {
|
---|
68 | /* I18n: properties display partial string joined with comma, frst is count, second is value */
|
---|
69 | sb.append(tr("{0} ''{1}''", entry.getValue().toString(), entry.getKey()));
|
---|
70 | }
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | /* I18n: properties display partial string joined with comma */
|
---|
74 | sb.append(trn("{0} different", "{0} different", otherCount, otherCount));
|
---|
75 | }
|
---|
76 | if(blankCount > 0) {
|
---|
77 | /* I18n: properties display partial string joined with comma */
|
---|
78 | sb.append(trn(", {0} unset", ", {0} unset", blankCount, blankCount));
|
---|
79 | }
|
---|
80 | sb.append(">");
|
---|
81 | str = sb.toString();
|
---|
82 | c.setFont(c.getFont().deriveFont(Font.ITALIC));
|
---|
83 |
|
---|
84 | } else { // One value: display the value
|
---|
85 | final Map.Entry<?, ?> entry = v.entrySet().iterator().next();
|
---|
86 | str = (String) entry.getKey();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | ((JLabel)c).putClientProperty("html.disable", Boolean.TRUE); // Fix #8730
|
---|
90 | ((JLabel)c).setText(str);
|
---|
91 | if (Main.pref.getBoolean("display.discardable-keys", false)) {
|
---|
92 | String key = null;
|
---|
93 | if (column == 0) {
|
---|
94 | key = str;
|
---|
95 | } else if (column == 1) {
|
---|
96 | Object value0 = table.getModel().getValueAt(row, 0);
|
---|
97 | if (value0 instanceof String) {
|
---|
98 | key = (String) value0;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | setColors(c, key, isSelected);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return c;
|
---|
105 | }
|
---|
106 | }
|
---|