source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/nodes/NodeListTableCellRenderer.java@ 1640

Last change on this file since 1640 was 1640, checked in by stoecker, 15 years ago

little bit more refactoring of coordinate access - patch by jttt

File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.nodes;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Component;
8import java.net.URL;
9import java.text.DecimalFormat;
10
11import javax.swing.ImageIcon;
12import javax.swing.JLabel;
13import javax.swing.JTable;
14import javax.swing.table.TableCellRenderer;
15
16import 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 */
23public 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}
Note: See TracBrowser for help on using the repository browser.