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