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

Last change on this file since 5299 was 5299, checked in by Don-vip, 12 years ago

conflict dialog: better display of empty rows in members tab, like previous work on nodes tab, plus fix Junit test

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