1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.pair.relation;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Component;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 |
|
---|
9 | import javax.swing.BorderFactory;
|
---|
10 | import javax.swing.ImageIcon;
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JTable;
|
---|
13 | import javax.swing.border.Border;
|
---|
14 | import javax.swing.table.TableCellRenderer;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
19 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
20 | import org.openstreetmap.josm.gui.conflict.pair.ListMergeModel;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This is the {@see TableCellRenderer} used in the tables of {@see RelationMemberMerger}.
|
---|
25 | *
|
---|
26 | */
|
---|
27 | public class RelationMemberTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
28 | public final static Color BGCOLOR_SELECTED = new Color(143,170,255);
|
---|
29 | public final static Color BGCOLOR_EMPTY_ROW = new Color(234,234,234);
|
---|
30 |
|
---|
31 | public final static Color BGCOLOR_NOT_IN_OPPOSITE = new Color(255,197,197);
|
---|
32 | public final static Color BGCOLOR_IN_OPPOSITE = new Color(255,234,213);
|
---|
33 | public final static Color BGCOLOR_SAME_POSITION_IN_OPPOSITE = new Color(217,255,217);
|
---|
34 |
|
---|
35 | public final static Color BGCOLOR_PARTICIPAING_IN_COMPARISON = Color.BLACK;
|
---|
36 | public final static Color FGCOLOR_PARTICIPAING_IN_COMPARISON = Color.WHITE;
|
---|
37 |
|
---|
38 | public final static Color BGCOLOR_FROZEN = new Color(234,234,234);
|
---|
39 |
|
---|
40 | private ImageIcon nodeIcon;
|
---|
41 | private ImageIcon wayIcon;
|
---|
42 | private ImageIcon relationIcon;
|
---|
43 | private Border rowNumberBorder = null;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Load the image icon for an OSM primitive of type node
|
---|
47 | *
|
---|
48 | * @return the icon; null, if not found
|
---|
49 | */
|
---|
50 | protected void loadIcons() {
|
---|
51 | nodeIcon = ImageProvider.get("data", "node");
|
---|
52 | wayIcon = ImageProvider.get("data", "way");
|
---|
53 | relationIcon = ImageProvider.get("data", "relation");
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * constructor
|
---|
58 | */
|
---|
59 | public RelationMemberTableCellRenderer() {
|
---|
60 | setIcon(null);
|
---|
61 | setOpaque(true);
|
---|
62 | loadIcons();
|
---|
63 | rowNumberBorder = BorderFactory.createEmptyBorder(0,4,0,0);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public String buildToolTipText(OsmPrimitive primitive) {
|
---|
67 | StringBuilder sb = new StringBuilder();
|
---|
68 | sb.append("<html>");
|
---|
69 | sb.append("<strong>id</strong>=")
|
---|
70 | .append(primitive.getId())
|
---|
71 | .append("<br>");
|
---|
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 | String value = primitive.get(key);
|
---|
84 | while(value.length() != 0) {
|
---|
85 | sb.append(value.substring(0,Math.min(50, value.length())));
|
---|
86 | if (value.length() > 50) {
|
---|
87 | sb.append("<br>");
|
---|
88 | value = value.substring(50);
|
---|
89 | } else {
|
---|
90 | value = "";
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | sb.append("</html>");
|
---|
95 | return sb.toString();
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * reset the renderer
|
---|
100 | */
|
---|
101 | protected void reset() {
|
---|
102 | setBackground(Color.WHITE);
|
---|
103 | setForeground(Color.BLACK);
|
---|
104 | setBorder(null);
|
---|
105 | setIcon(null);
|
---|
106 | setToolTipText(null);
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected void renderBackground(ListMergeModel<Node>.EntriesTableModel model, RelationMember member, int row, int col, boolean isSelected) {
|
---|
110 | Color bgc = Color.WHITE;
|
---|
111 | if (col == 0) {
|
---|
112 | if (model.getListMergeModel().isFrozen()) {
|
---|
113 | bgc = BGCOLOR_FROZEN;
|
---|
114 | } else if (model.isParticipatingInCurrentComparePair()) {
|
---|
115 | bgc = BGCOLOR_PARTICIPAING_IN_COMPARISON;
|
---|
116 | } else if (isSelected) {
|
---|
117 | bgc = BGCOLOR_SELECTED;
|
---|
118 | }
|
---|
119 | } else {
|
---|
120 | if (model.getListMergeModel().isFrozen()) {
|
---|
121 | bgc = BGCOLOR_FROZEN;
|
---|
122 | } else if (member == null) {
|
---|
123 | bgc = BGCOLOR_EMPTY_ROW;
|
---|
124 | } else if (isSelected) {
|
---|
125 | bgc = BGCOLOR_SELECTED;
|
---|
126 | } else {
|
---|
127 | if (model.isParticipatingInCurrentComparePair()) {
|
---|
128 | if (model.isSamePositionInOppositeList(row)) {
|
---|
129 | bgc = BGCOLOR_SAME_POSITION_IN_OPPOSITE;
|
---|
130 | } else if (model.isIncludedInOppositeList(row)) {
|
---|
131 | bgc = BGCOLOR_IN_OPPOSITE;
|
---|
132 | } else {
|
---|
133 | bgc = BGCOLOR_NOT_IN_OPPOSITE;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | setBackground(bgc);
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected void renderForeground(ListMergeModel<Node>.EntriesTableModel model, RelationMember member, int row, int col, boolean isSelected) {
|
---|
142 | Color fgc = Color.BLACK;
|
---|
143 | if (col == 0 && model.isParticipatingInCurrentComparePair() && ! model.getListMergeModel().isFrozen()) {
|
---|
144 | fgc = Color.WHITE;
|
---|
145 | }
|
---|
146 | setForeground(fgc);
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected void renderRole(RelationMember member) {
|
---|
150 | setText(member.getRole());
|
---|
151 | setToolTipText(member.getRole());
|
---|
152 | }
|
---|
153 |
|
---|
154 | protected void renderPrimitive(RelationMember member) {
|
---|
155 | String displayName = member.getMember().getDisplayName(DefaultNameFormatter.getInstance());
|
---|
156 | setText(displayName);
|
---|
157 | setToolTipText(buildToolTipText(member.getMember()));
|
---|
158 | if (member.isNode()) {
|
---|
159 | setIcon(nodeIcon);
|
---|
160 | } else if (member.isWay()) {
|
---|
161 | setIcon(wayIcon);
|
---|
162 | } else if (member.isRelation()) {
|
---|
163 | setIcon(relationIcon);
|
---|
164 | } else {
|
---|
165 | // should not happen
|
---|
166 | setIcon(null);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * render the row id
|
---|
172 | * @param row the row index
|
---|
173 | * @param isSelected
|
---|
174 | */
|
---|
175 | protected void renderRowId(int row) {
|
---|
176 | setBorder(rowNumberBorder);
|
---|
177 | setText(Integer.toString(row+1));
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected void renderEmptyRow() {
|
---|
181 | setIcon(null);
|
---|
182 | setBackground(BGCOLOR_EMPTY_ROW);
|
---|
183 | setText("");
|
---|
184 | }
|
---|
185 |
|
---|
186 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
187 | int row, int column) {
|
---|
188 |
|
---|
189 | RelationMember member = (RelationMember)value;
|
---|
190 | reset();
|
---|
191 | renderBackground(getModel(table), member, row, column, isSelected);
|
---|
192 | renderForeground(getModel(table), member, row, column, isSelected);
|
---|
193 | switch(column) {
|
---|
194 | case 0:
|
---|
195 | renderRowId(row);
|
---|
196 | break;
|
---|
197 | case 1:
|
---|
198 | if (member == null) {
|
---|
199 | renderEmptyRow();
|
---|
200 | } else {
|
---|
201 | renderRole(member);
|
---|
202 | }
|
---|
203 | break;
|
---|
204 | case 2:
|
---|
205 | if (member == null) {
|
---|
206 | renderEmptyRow();
|
---|
207 | } else {
|
---|
208 | renderPrimitive(member);
|
---|
209 | }
|
---|
210 | break;
|
---|
211 | default:
|
---|
212 | // should not happen
|
---|
213 | }
|
---|
214 | return this;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * replies the model
|
---|
219 | * @param table the table
|
---|
220 | * @return the table model
|
---|
221 | */
|
---|
222 | @SuppressWarnings("unchecked")
|
---|
223 | protected ListMergeModel<Node>.EntriesTableModel getModel(JTable table) {
|
---|
224 | return (ListMergeModel.EntriesTableModel)table.getModel();
|
---|
225 | }
|
---|
226 | }
|
---|