source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesCellRenderer.java@ 8170

Last change on this file since 8170 was 8170, checked in by stoecker, 10 years ago

fix #11275 - improve display of multiple values - original patch by Bryce Nesbitt

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.Color;
9import java.awt.Component;
10import java.awt.Font;
11import java.util.Map;
12
13import javax.swing.JLabel;
14import javax.swing.JTable;
15import javax.swing.UIDefaults;
16import javax.swing.table.DefaultTableCellRenderer;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20
21/**
22 * Cell renderer of tags table.
23 * @since 6314
24 */
25public 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 blank_count;
56 Integer other_count;
57 if (v.get("") == null) {
58 blank_count = 0;
59 other_count = v.size();
60 } else {
61 blank_count = (Integer)v.get("");
62 other_count = v.size()-1;
63 }
64 str = "<";
65 if (other_count == 1) {
66 for (Map.Entry<?, ?> entry : v.entrySet()) { // Find the non-blank value in the map
67 if ( entry.getKey() != "") {
68 str += entry.getValue().toString() + " '" + entry.getKey() + "'";
69 }
70 }
71 } else {
72 str += tr ("{0} different", other_count);
73 }
74 if(blank_count > 0) {
75 str += trn(", {0} unset", ", {0} unset", blank_count, blank_count);
76 }
77 str += ">";
78 c.setFont(c.getFont().deriveFont(Font.ITALIC));
79
80 } else { // One value: display the value
81 final Map.Entry<?, ?> entry = v.entrySet().iterator().next();
82 str = (String) entry.getKey();
83 }
84 }
85 ((JLabel)c).putClientProperty("html.disable", Boolean.TRUE); // Fix #8730
86 ((JLabel)c).setText(str);
87 if (Main.pref.getBoolean("display.discardable-keys", false)) {
88 String key = null;
89 if (column == 0) {
90 key = str;
91 } else if (column == 1) {
92 Object value0 = table.getModel().getValueAt(row, 0);
93 if (value0 instanceof String) {
94 key = (String) value0;
95 }
96 }
97 setColors(c, key, isSelected);
98 }
99 }
100 return c;
101 }
102}
Note: See TracBrowser for help on using the repository browser.