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