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