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

Last change on this file since 8540 was 8540, checked in by Don-vip, 10 years ago

fix remaining checkstyle issues

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