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

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

fix potential NPEs and Sonar issues related to serialization

  • Property svn:eol-style set to native
File size: 4.5 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;
6
7import javax.swing.BorderFactory;
8import javax.swing.ImageIcon;
9import javax.swing.JLabel;
10import javax.swing.JTable;
11import javax.swing.border.Border;
12import javax.swing.table.TableCellRenderer;
13
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.gui.DefaultNameFormatter;
16import org.openstreetmap.josm.gui.conflict.ConflictColors;
17import org.openstreetmap.josm.gui.conflict.pair.ListMergeModel;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * This is the {@link TableCellRenderer} used in the node tables of {@link NodeListMerger}.
22 *
23 */
24public class NodeListTableCellRenderer extends JLabel implements TableCellRenderer {
25
26 private final ImageIcon icon;
27 private final transient Border rowNumberBorder;
28
29 /**
30 * constructor
31 */
32 public NodeListTableCellRenderer() {
33 icon = ImageProvider.get("data", "node");
34 rowNumberBorder = BorderFactory.createEmptyBorder(0,4,0,0);
35 setOpaque(true);
36 }
37
38 /**
39 * reset the renderer
40 */
41 protected void reset() {
42 setBackground(ConflictColors.BGCOLOR.get());
43 setForeground(ConflictColors.FGCOLOR.get());
44 setBorder(null);
45 setIcon(null);
46 setToolTipText(null);
47 }
48
49 /**
50 * render a node
51 * @param model the model
52 * @param node the node
53 * @param isSelected true, if the current row is selected
54 */
55 protected void renderNode(ListMergeModel<Node>.EntriesTableModel model, Node node, int row, boolean isSelected) {
56 setIcon(icon);
57 setBorder(null);
58 if (model.getListMergeModel().isFrozen()) {
59 setBackground(ConflictColors.BGCOLOR_FROZEN.get());
60 } else if (isSelected) {
61 setBackground(ConflictColors.BGCOLOR_SELECTED.get());
62 } else if (model.isParticipatingInCurrentComparePair()) {
63 if (model.isSamePositionInOppositeList(row)) {
64 setBackground(ConflictColors.BGCOLOR_SAME_POSITION_IN_OPPOSITE.get());
65 } else if (model.isIncludedInOppositeList(row)) {
66 setBackground(ConflictColors.BGCOLOR_IN_OPPOSITE.get());
67 } else {
68 setBackground(ConflictColors.BGCOLOR_NOT_IN_OPPOSITE.get());
69 }
70 }
71 setText(node.getDisplayName(DefaultNameFormatter.getInstance()));
72 setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(node));
73 }
74
75 /**
76 * render an empty row
77 */
78 protected void renderEmptyRow() {
79 setIcon(null);
80 setBackground(ConflictColors.BGCOLOR_EMPTY_ROW.get());
81 setText("");
82 }
83
84 /**
85 * render the row id
86 * @param model the model
87 * @param row the row index
88 * @param isSelected true, if the current row is selected
89 */
90 protected void renderRowId( ListMergeModel<Node>.EntriesTableModel model, int row, boolean isSelected) {
91 setIcon(null);
92 setBorder(rowNumberBorder);
93 if (model.getListMergeModel().isFrozen()) {
94 setBackground(ConflictColors.BGCOLOR_FROZEN.get());
95 } else if (model.isParticipatingInCurrentComparePair()) {
96 setBackground(ConflictColors.BGCOLOR_PARTICIPATING_IN_COMPARISON.get());
97 setForeground(ConflictColors.FGCOLOR_PARTICIPATING_IN_COMPARISON.get());
98 }
99 setText(Integer.toString(row+1));
100 }
101
102 @Override
103 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
104 int row, int column) {
105
106 Node node = (Node)value;
107 reset();
108 if (node == null) {
109 renderEmptyRow();
110 } else {
111 switch(column) {
112 case 0:
113 renderRowId(getModel(table),row, isSelected);
114 break;
115 case 1:
116 renderNode(getModel(table), node, row, isSelected);
117 break;
118 default:
119 // should not happen
120 throw new RuntimeException(MessageFormat.format("Unexpected column index. Got {0}.", column));
121 }
122 }
123 return this;
124 }
125
126 /**
127 * replies the model
128 * @param table the table
129 * @return the table model
130 */
131 @SuppressWarnings("unchecked")
132 protected ListMergeModel<Node>.EntriesTableModel getModel(JTable table) {
133 return (ListMergeModel<Node>.EntriesTableModel)table.getModel();
134 }
135}
Note: See TracBrowser for help on using the repository browser.