1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.nodes;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.text.DecimalFormat;
|
---|
10 |
|
---|
11 | import javax.swing.ImageIcon;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JTable;
|
---|
14 | import javax.swing.table.TableCellRenderer;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This is the {@see TableCellRenderer} used in the node tables of {@see NodeListMerger}.
|
---|
20 | *
|
---|
21 | *
|
---|
22 | */
|
---|
23 | public class NodeListTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
24 | private static DecimalFormat COORD_FORMATTER = new DecimalFormat("###0.0000");
|
---|
25 | public final static Color BGCOLOR_SELECTED = new Color(143,170,255);
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Load the image icon for an OSM primitive of type node
|
---|
29 | *
|
---|
30 | * @return the icon; null, if not found
|
---|
31 | */
|
---|
32 | protected ImageIcon loadIcon() {
|
---|
33 | URL url = this.getClass().getResource("/images/data/node.png");;
|
---|
34 | if (url == null) {
|
---|
35 | System.out.println(tr("Failed to load resource /images/data/node.png"));
|
---|
36 | return null;
|
---|
37 | }
|
---|
38 | return new ImageIcon(url);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * constructor
|
---|
43 | */
|
---|
44 | public NodeListTableCellRenderer() {
|
---|
45 | setIcon(loadIcon());
|
---|
46 | setOpaque(true);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * creates the display name for a node. The name is derived from the nodes id,
|
---|
51 | * its name (i.e. the value of the tag with key name) and its coordinates.
|
---|
52 | *
|
---|
53 | * @param node the node
|
---|
54 | * @return the display name
|
---|
55 | */
|
---|
56 | protected String getDisplayName(Node node) {
|
---|
57 | StringBuilder sb = new StringBuilder();
|
---|
58 | if (node.get("name") != null) {
|
---|
59 | sb.append(node.get("name"));
|
---|
60 | sb.append("/");
|
---|
61 | sb.append(node.id);
|
---|
62 | } else {
|
---|
63 | sb.append(node.id);
|
---|
64 | }
|
---|
65 | sb.append(" (");
|
---|
66 |
|
---|
67 | if (node.getCoor() != null) {
|
---|
68 | sb.append(COORD_FORMATTER.format(node.getCoor().lat()));
|
---|
69 | sb.append(",");
|
---|
70 | sb.append(COORD_FORMATTER.format(node.getCoor().lon()));
|
---|
71 | } else {
|
---|
72 | sb.append("?,?");
|
---|
73 | }
|
---|
74 | sb.append(")");
|
---|
75 | return sb.toString();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * reset the renderer
|
---|
80 | */
|
---|
81 | protected void reset() {
|
---|
82 | setBackground(Color.WHITE);
|
---|
83 | setForeground(Color.BLACK);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * render a node
|
---|
88 | * @param node the node
|
---|
89 | * @param isSelected
|
---|
90 | */
|
---|
91 | protected void renderNode(Node node, boolean isSelected) {
|
---|
92 | if (isSelected) {
|
---|
93 | setBackground(BGCOLOR_SELECTED);
|
---|
94 | }
|
---|
95 | setText(getDisplayName(node));
|
---|
96 | }
|
---|
97 |
|
---|
98 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
99 | int row, int column) {
|
---|
100 |
|
---|
101 | Node node = (Node)value;
|
---|
102 | reset();
|
---|
103 | renderNode(node,isSelected);
|
---|
104 | return this;
|
---|
105 | }
|
---|
106 | }
|
---|