1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.pair.nodes;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.text.MessageFormat;
|
---|
6 |
|
---|
7 | import javax.swing.BorderFactory;
|
---|
8 | import javax.swing.ImageIcon;
|
---|
9 | import javax.swing.JLabel;
|
---|
10 | import javax.swing.JTable;
|
---|
11 | import javax.swing.border.Border;
|
---|
12 | import javax.swing.table.TableCellRenderer;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
16 | import org.openstreetmap.josm.gui.conflict.ConflictColors;
|
---|
17 | import org.openstreetmap.josm.gui.conflict.pair.ListMergeModel;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * This is the {@link TableCellRenderer} used in the node tables of {@link NodeListMerger}.
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public 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 | */
|
---|
89 | protected void renderRowId(ListMergeModel<Node>.EntriesTableModel model, int row) {
|
---|
90 | setIcon(null);
|
---|
91 | setBorder(rowNumberBorder);
|
---|
92 | if (model.getListMergeModel().isFrozen()) {
|
---|
93 | setBackground(ConflictColors.BGCOLOR_FROZEN.get());
|
---|
94 | } else if (model.isParticipatingInCurrentComparePair()) {
|
---|
95 | setBackground(ConflictColors.BGCOLOR_PARTICIPATING_IN_COMPARISON.get());
|
---|
96 | setForeground(ConflictColors.FGCOLOR_PARTICIPATING_IN_COMPARISON.get());
|
---|
97 | }
|
---|
98 | setText(Integer.toString(row+1));
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
103 | int row, int column) {
|
---|
104 |
|
---|
105 | Node node = (Node) value;
|
---|
106 | reset();
|
---|
107 | if (node == null) {
|
---|
108 | renderEmptyRow();
|
---|
109 | } else {
|
---|
110 | switch(column) {
|
---|
111 | case 0:
|
---|
112 | renderRowId(getModel(table), row);
|
---|
113 | break;
|
---|
114 | case 1:
|
---|
115 | renderNode(getModel(table), node, row, isSelected);
|
---|
116 | break;
|
---|
117 | default:
|
---|
118 | // should not happen
|
---|
119 | throw new RuntimeException(MessageFormat.format("Unexpected column index. Got {0}.", column));
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return this;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * replies the model
|
---|
127 | * @param table the table
|
---|
128 | * @return the table model
|
---|
129 | */
|
---|
130 | @SuppressWarnings("unchecked")
|
---|
131 | protected ListMergeModel<Node>.EntriesTableModel getModel(JTable table) {
|
---|
132 | return (ListMergeModel<Node>.EntriesTableModel) table.getModel();
|
---|
133 | }
|
---|
134 | }
|
---|