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

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

Improved Download Location Dialog and created help page.
ExtendedDialog now supports context sensitive help including a help button.
ExtendedDialog now supports tooltip texts for buttons in the button row.

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