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.Collection;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Objects;
|
---|
14 | import java.util.Optional;
|
---|
15 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
16 |
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JTable;
|
---|
19 | import javax.swing.UIManager;
|
---|
20 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
21 | import javax.swing.table.TableCellRenderer;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.data.osm.AbstractPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
25 | import org.openstreetmap.josm.data.preferences.CachingProperty;
|
---|
26 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
27 | import org.openstreetmap.josm.tools.I18n;
|
---|
28 | import org.openstreetmap.josm.tools.Pair;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Cell renderer of tags table.
|
---|
32 | * @since 6314
|
---|
33 | */
|
---|
34 | public class PropertiesCellRenderer extends DefaultTableCellRenderer {
|
---|
35 |
|
---|
36 | private static final CachingProperty<Color> SELECTED_FG
|
---|
37 | = new NamedColorProperty(marktr("Discardable key: selection Foreground"), Color.GRAY).cached();
|
---|
38 | private static final CachingProperty<Color> SELECTED_BG;
|
---|
39 | private static final CachingProperty<Color> NORMAL_FG
|
---|
40 | = new NamedColorProperty(marktr("Discardable key: foreground"), Color.GRAY).cached();
|
---|
41 | private static final CachingProperty<Color> NORMAL_BG;
|
---|
42 | private static final CachingProperty<Boolean> DISCARDABLE
|
---|
43 | = new BooleanProperty("display.discardable-keys", false).cached();
|
---|
44 |
|
---|
45 | static {
|
---|
46 | SELECTED_BG = new NamedColorProperty(marktr("Discardable key: selection Background"),
|
---|
47 | Optional.ofNullable(UIManager.getColor("Table.selectionBackground")).orElse(Color.BLUE)).cached();
|
---|
48 | NORMAL_BG = new NamedColorProperty(marktr("Discardable key: background"),
|
---|
49 | Optional.ofNullable(UIManager.getColor("Table.background")).orElse(Color.WHITE)).cached();
|
---|
50 | }
|
---|
51 |
|
---|
52 | private final Collection<TableCellRenderer> customRenderer = new CopyOnWriteArrayList<>();
|
---|
53 |
|
---|
54 | private static void setColors(Component c, String key, boolean isSelected) {
|
---|
55 |
|
---|
56 | if (AbstractPrimitive.getDiscardableKeys().contains(key)) {
|
---|
57 | c.setForeground((isSelected ? SELECTED_FG : NORMAL_FG).get());
|
---|
58 | c.setBackground((isSelected ? SELECTED_BG : NORMAL_BG).get());
|
---|
59 | } else {
|
---|
60 | c.setForeground(UIManager.getColor("Table."+(isSelected ? "selectionF" : "f")+"oreground"));
|
---|
61 | c.setBackground(UIManager.getColor("Table."+(isSelected ? "selectionB" : "b")+"ackground"));
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
67 | for (TableCellRenderer renderer : customRenderer) {
|
---|
68 | final Component component = renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
---|
69 | if (component != null) {
|
---|
70 | return component;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | if (value == null)
|
---|
74 | return this;
|
---|
75 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
76 | if (c instanceof JLabel) {
|
---|
77 | String str = null;
|
---|
78 | if (value instanceof String) {
|
---|
79 | str = (String) value;
|
---|
80 | } else if (value instanceof Map<?, ?>) {
|
---|
81 | Map<?, ?> v = (Map<?, ?>) value;
|
---|
82 | if (v.size() != 1) { // Multiple values: give user a short summary of the values
|
---|
83 | Integer blankCount;
|
---|
84 | Integer otherCount;
|
---|
85 | if (v.get("") == null) {
|
---|
86 | blankCount = 0;
|
---|
87 | otherCount = v.size();
|
---|
88 | } else {
|
---|
89 | blankCount = (Integer) v.get("");
|
---|
90 | otherCount = v.size()-1;
|
---|
91 | }
|
---|
92 | StringBuilder sb = new StringBuilder("<");
|
---|
93 | if (otherCount == 1) {
|
---|
94 | // Find the non-blank value in the map
|
---|
95 | v.entrySet().stream().filter(entry -> !Objects.equals(entry.getKey(), ""))
|
---|
96 | /* I18n: properties display partial string joined with comma, first is count, second is value */
|
---|
97 | .findAny().ifPresent(entry -> sb.append(tr("{0} ''{1}''", entry.getValue().toString(), entry.getKey())));
|
---|
98 | } else {
|
---|
99 | /* I18n: properties display partial string joined with comma */
|
---|
100 | sb.append(trn("{0} different", "{0} different", otherCount, otherCount));
|
---|
101 | }
|
---|
102 | if (blankCount > 0) {
|
---|
103 | /* I18n: properties display partial string joined with comma */
|
---|
104 | sb.append(trn(", {0} unset", ", {0} unset", blankCount, blankCount));
|
---|
105 | }
|
---|
106 | sb.append('>');
|
---|
107 | str = sb.toString();
|
---|
108 | c.setFont(c.getFont().deriveFont(Font.ITALIC));
|
---|
109 |
|
---|
110 | } else { // One value: display the value
|
---|
111 | str = (String) v.entrySet().iterator().next().getKey();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | boolean knownNameKey = false;
|
---|
115 | if (column == 0 && str != null) {
|
---|
116 | Pair<String, Boolean> label = I18n.getLocalizedLanguageName(str);
|
---|
117 | if (label != null) {
|
---|
118 | knownNameKey = label.b;
|
---|
119 | if (knownNameKey) {
|
---|
120 | str = new StringBuilder("<html><body>").append(str)
|
---|
121 | .append(" <i><").append(label.a).append("></i></body></html>").toString();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | ((JLabel) c).putClientProperty("html.disable", knownNameKey ? null : Boolean.TRUE); // Fix #8730
|
---|
126 | ((JLabel) c).setText(str);
|
---|
127 | if (DISCARDABLE.get()) {
|
---|
128 | String key = null;
|
---|
129 | if (column == 0) {
|
---|
130 | key = str;
|
---|
131 | } else if (column == 1) {
|
---|
132 | Object value0 = table.getModel().getValueAt(row, 0);
|
---|
133 | if (value0 instanceof String) {
|
---|
134 | key = (String) value0;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | setColors(c, key, isSelected);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | return c;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Adds a custom table cell renderer to render cells of the tags table.
|
---|
145 | *
|
---|
146 | * If the renderer is not capable performing a {@link TableCellRenderer#getTableCellRendererComponent},
|
---|
147 | * it should return {@code null} to fall back to the
|
---|
148 | * {@link PropertiesCellRenderer#getTableCellRendererComponent default implementation}.
|
---|
149 | * @param renderer the renderer to add
|
---|
150 | * @since 9149
|
---|
151 | */
|
---|
152 | public void addCustomRenderer(TableCellRenderer renderer) {
|
---|
153 | customRenderer.add(renderer);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Removes a custom table cell renderer.
|
---|
158 | * @param renderer the renderer to remove
|
---|
159 | * @since 9149
|
---|
160 | */
|
---|
161 | public void removeCustomRenderer(TableCellRenderer renderer) {
|
---|
162 | customRenderer.remove(renderer);
|
---|
163 | }
|
---|
164 | }
|
---|