source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/relation/RelationMemberTableCellRenderer.java@ 1640

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

little bit more refactoring of coordinate access - patch by jttt

File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.relation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Component;
8import java.text.DecimalFormat;
9
10import javax.swing.ImageIcon;
11import javax.swing.JLabel;
12import javax.swing.JTable;
13import javax.swing.table.TableCellRenderer;
14
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.data.osm.RelationMember;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.tools.ImageProvider;
21
22/**
23 * This is the {@see TableCellRenderer} used in the tables of {@see RelationMemberMerger}.
24 *
25 *
26 */
27public class RelationMemberTableCellRenderer extends JLabel implements TableCellRenderer {
28 private static DecimalFormat COORD_FORMATTER = new DecimalFormat("###0.0000");
29 public final static Color BGCOLOR_SELECTED = new Color(143,170,255);
30
31 private ImageIcon nodeIcon;
32 private ImageIcon wayIcon;
33 private ImageIcon relationIcon;
34
35 /**
36 * Load the image icon for an OSM primitive of type node
37 *
38 * @return the icon; null, if not found
39 */
40 protected void loadIcons() {
41 nodeIcon = ImageProvider.get("data", "node");
42 wayIcon = ImageProvider.get("data", "way");
43 relationIcon = ImageProvider.get("data", "relation");
44 }
45
46 /**
47 * constructor
48 */
49 public RelationMemberTableCellRenderer() {
50 setIcon(null);
51 setOpaque(true);
52 loadIcons();
53 }
54
55 /**
56 * creates the display name for a node. The name is derived from the nodes id,
57 * its name (i.e. the value of the tag with key name) and its coordinates.
58 *
59 * @param node the node
60 * @return the display name
61 */
62 protected String getDisplayName(RelationMember member) {
63 StringBuilder sb = new StringBuilder();
64 OsmPrimitive primitive = member.member;
65 if (primitive instanceof Node) {
66 sb.append(tr("Node"));
67 } else if (primitive instanceof Way) {
68 sb.append(tr("Way"));
69 } else if (primitive instanceof Relation) {
70 sb.append(tr("Relation"));
71 }
72 sb.append(" ");
73 if (primitive.get("name") != null) {
74 sb.append(primitive.get("name"));
75 sb.append("/");
76 sb.append(primitive.id);
77 } else {
78 sb.append(primitive.id);
79 }
80
81 if (primitive instanceof Node) {
82 Node n = (Node)primitive;
83 sb.append(" (");
84 if (n.getCoor() != null) {
85 sb.append(COORD_FORMATTER.format(n.getCoor().lat()));
86 sb.append(",");
87 sb.append(COORD_FORMATTER.format(n.getCoor().lon()));
88 } else {
89 sb.append("?,?");
90 }
91 sb.append(")");
92 }
93 return sb.toString();
94 }
95
96 /**
97 * reset the renderer
98 */
99 protected void reset() {
100 setBackground(Color.WHITE);
101 setForeground(Color.BLACK);
102 }
103
104
105 protected void setBackground(boolean isSelected) {
106 Color bgc = isSelected ? BGCOLOR_SELECTED : Color.WHITE;
107 setBackground(bgc);
108 }
109
110 protected void renderRole(RelationMember member) {
111 setText(member.role == null ? "" : member.role);
112 setIcon(null);
113 }
114
115 protected void renderPrimitive(RelationMember member) {
116 String displayName = getDisplayName(member);
117 setText(displayName);
118 setToolTipText(displayName);
119 if (member.member instanceof Node) {
120 setIcon(nodeIcon);
121 } else if (member.member instanceof Way) {
122 setIcon(wayIcon);
123 } else if (member.member instanceof Relation) {
124 setIcon(relationIcon);
125 } else {
126 // should not happen
127 setIcon(null);
128 }
129 }
130
131 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
132 int row, int column) {
133
134 RelationMember member = (RelationMember)value;
135 reset();
136 setBackground(isSelected);
137 switch(column) {
138 case 0:
139 renderRole(member);
140 break;
141 case 1:
142 renderPrimitive(member);
143 break;
144 default:
145 // should not happen
146 }
147 return this;
148 }
149
150}
Note: See TracBrowser for help on using the repository browser.