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