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

Last change on this file since 1954 was 1954, checked in by Gubaer, 15 years ago

Moved package org.openstreetmap.josm.gui.conflict to org.openstreetmap.josm.gui.conflict.pair

File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.pair.nodes;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Component;
8import java.text.DecimalFormat;
9import java.util.ArrayList;
10import java.util.Collections;
11import java.util.logging.Logger;
12
13import javax.swing.BorderFactory;
14import javax.swing.ImageIcon;
15import javax.swing.JLabel;
16import javax.swing.JTable;
17import javax.swing.border.Border;
18import javax.swing.table.TableCellRenderer;
19
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.gui.conflict.pair.ListMergeModel;
23import org.openstreetmap.josm.tools.ImageProvider;
24
25/**
26 * This is the {@see TableCellRenderer} used in the node tables of {@see NodeListMerger}.
27 *
28 */
29public class NodeListTableCellRenderer extends JLabel implements TableCellRenderer {
30 static private final Logger logger = Logger.getLogger(NodeListTableCellRenderer.class.getName());
31 private static DecimalFormat COORD_FORMATTER = new DecimalFormat("###0.0000");
32 public final static Color BGCOLOR_SELECTED = new Color(143,170,255);
33 public final static Color BGCOLOR_EMPTY_ROW = new Color(234,234,234);
34 public final static Color BGCOLOR_FROZEN = new Color(234,234,234);
35 public final static Color BGCOLOR_PARTICIPAING_IN_COMPARISON = Color.BLACK;
36 public final static Color FGCOLOR_PARTICIPAING_IN_COMPARISON = Color.WHITE;
37
38 public final static Color BGCOLOR_NOT_IN_OPPOSITE = new Color(255,197,197);
39 public final static Color BGCOLOR_IN_OPPOSITE = new Color(255,234,213);
40 public final static Color BGCOLOR_SAME_POSITION_IN_OPPOSITE = new Color(217,255,217);
41
42 private final ImageIcon icon;
43 private final Border rowNumberBorder;
44
45 /**
46 * constructor
47 */
48 public NodeListTableCellRenderer() {
49 icon = ImageProvider.get("data", "node");
50 rowNumberBorder = BorderFactory.createEmptyBorder(0,4,0,0);
51 setOpaque(true);
52 }
53
54 /**
55 * build the tool tip text for an {@see OsmPrimitive}. It consist of the formatted
56 * key/value pairs for this primitive.
57 *
58 * @param primitive
59 * @return the tool tip text
60 */
61 public String buildToolTipText(OsmPrimitive primitive) {
62 StringBuilder sb = new StringBuilder();
63
64 sb.append("<html>");
65 // show the id
66 //
67 sb.append("<strong>id</strong>=")
68 .append(primitive.id)
69 .append("<br>");
70
71 // show the key/value-pairs, sorted by key
72 //
73 ArrayList<String> keyList = new ArrayList<String>(primitive.keySet());
74 Collections.sort(keyList);
75 for (int i = 0; i < keyList.size(); i++) {
76 if (i > 0) {
77 sb.append("<br>");
78 }
79 String key = keyList.get(i);
80 sb.append("<strong>")
81 .append(key)
82 .append("</strong>")
83 .append("=");
84 // make sure long values are split into several rows. Otherwise
85 // the tool tip window can become to wide
86 //
87 String value = primitive.get(key);
88 while(value.length() != 0) {
89 sb.append(value.substring(0,Math.min(50, value.length())));
90 if (value.length() > 50) {
91 sb.append("<br>");
92 value = value.substring(50);
93 } else {
94 value = "";
95 }
96 }
97 }
98 sb.append("</html>");
99 return sb.toString();
100 }
101
102 /**
103 * reset the renderer
104 */
105 protected void reset() {
106 setBackground(Color.WHITE);
107 setForeground(Color.BLACK);
108 }
109
110 /**
111 * render a node
112 * @param model the model
113 * @param node the node
114 * @param isSelected true, if the current row is selected
115 */
116 protected void renderNode(ListMergeModel<Node>.EntriesTableModel model, Node node, int row, boolean isSelected) {
117 setIcon(icon);
118 setBorder(null);
119 if (model.getListMergeModel().isFrozen()) {
120 setBackground(BGCOLOR_FROZEN);
121 } else if (isSelected) {
122 setBackground(BGCOLOR_SELECTED);
123 } else if (model.isParticipatingInCurrentComparePair()) {
124 if (model.isSamePositionInOppositeList(row)) {
125 setBackground(BGCOLOR_SAME_POSITION_IN_OPPOSITE);
126 } else if (model.isIncludedInOppositeList(row)) {
127 setBackground(BGCOLOR_IN_OPPOSITE);
128 } else {
129 setBackground(BGCOLOR_NOT_IN_OPPOSITE);
130 }
131 }
132 setText(node.getName());
133 setToolTipText(buildToolTipText(node));
134 }
135
136 /**
137 * render an empty row
138 */
139 protected void renderEmptyRow() {
140 setIcon(null);
141 setBackground(BGCOLOR_EMPTY_ROW);
142 setText("");
143 }
144
145 /**
146 * render the row id
147 * @param model the model
148 * @param row the row index
149 * @param isSelected true, if the current row is selected
150 */
151 protected void renderRowId( ListMergeModel<Node>.EntriesTableModel model, int row, boolean isSelected) {
152 setIcon(null);
153 setBorder(rowNumberBorder);
154 if (model.getListMergeModel().isFrozen()) {
155 setBackground(BGCOLOR_FROZEN);
156 } else if (model.isParticipatingInCurrentComparePair()) {
157 setBackground(BGCOLOR_PARTICIPAING_IN_COMPARISON);
158 setForeground(FGCOLOR_PARTICIPAING_IN_COMPARISON);
159 }
160 setText(Integer.toString(row+1));
161 }
162
163 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
164 int row, int column) {
165
166 Node node = (Node)value;
167 reset();
168 switch(column) {
169 case 0:
170 renderRowId(getModel(table),row, isSelected);
171 break;
172 case 1:
173 if (node == null) {
174 renderEmptyRow();
175 } else {
176 renderNode(getModel(table), node, row, isSelected);
177 }
178 break;
179 default:
180 // should not happen
181 throw new RuntimeException(tr("unexpected column index. Got {0}", column));
182 }
183 return this;
184 }
185
186 /**
187 * replies the model
188 * @param table the table
189 * @return the table model
190 */
191 @SuppressWarnings("unchecked")
192 protected ListMergeModel<Node>.EntriesTableModel getModel(JTable table) {
193 return (ListMergeModel.EntriesTableModel)table.getModel();
194 }
195}
Note: See TracBrowser for help on using the repository browser.