[10198] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 6 |
|
---|
[12745] | 7 | import java.util.Arrays;
|
---|
[10198] | 8 | import java.util.List;
|
---|
| 9 |
|
---|
| 10 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
| 11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[12745] | 12 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
| 13 | import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
|
---|
[10198] | 14 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
[12672] | 15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[13926] | 16 | import org.openstreetmap.josm.data.osm.INode;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.IRelationMember;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.IWay;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.OsmData;
|
---|
[10198] | 22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[14120] | 23 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
[13167] | 24 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
---|
| 25 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator.Hemisphere;
|
---|
[10198] | 26 | import org.openstreetmap.josm.tools.Geometry;
|
---|
[13167] | 27 | import org.openstreetmap.josm.tools.Pair;
|
---|
[12745] | 28 | import org.openstreetmap.josm.tools.Utils;
|
---|
[10198] | 29 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Textual representation of primitive contents, used in {@code InspectPrimitiveDialog}.
|
---|
| 33 | * @since 10198
|
---|
| 34 | */
|
---|
| 35 | public class InspectPrimitiveDataText {
|
---|
| 36 | private static final String INDENT = " ";
|
---|
| 37 | private static final char NL = '\n';
|
---|
| 38 |
|
---|
| 39 | private final StringBuilder s = new StringBuilder();
|
---|
[13926] | 40 | private final OsmData<?, ?, ?, ?> ds;
|
---|
[10198] | 41 |
|
---|
[13926] | 42 | InspectPrimitiveDataText(OsmData<?, ?, ?, ?> ds) {
|
---|
[12672] | 43 | this.ds = ds;
|
---|
[10198] | 44 | }
|
---|
| 45 |
|
---|
| 46 | private InspectPrimitiveDataText add(String title, String... values) {
|
---|
| 47 | s.append(INDENT).append(title);
|
---|
| 48 | for (String v : values) {
|
---|
| 49 | s.append(v);
|
---|
| 50 | }
|
---|
| 51 | s.append(NL);
|
---|
| 52 | return this;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | private static String getNameAndId(String name, long id) {
|
---|
| 56 | if (name != null) {
|
---|
[14302] | 57 | return name + tr(" ({0})", /* sic to avoid thousand separators */ Long.toString(id));
|
---|
[10198] | 58 | } else {
|
---|
| 59 | return Long.toString(id);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Adds a new OSM primitive.
|
---|
| 65 | * @param o primitive to add
|
---|
| 66 | */
|
---|
[13926] | 67 | public void addPrimitive(IPrimitive o) {
|
---|
[10198] | 68 |
|
---|
| 69 | addHeadline(o);
|
---|
| 70 |
|
---|
| 71 | if (!(o.getDataSet() != null && o.getDataSet().getPrimitiveById(o) != null)) {
|
---|
| 72 | s.append(NL).append(INDENT).append(tr("not in data set")).append(NL);
|
---|
| 73 | return;
|
---|
| 74 | }
|
---|
| 75 | if (o.isIncomplete()) {
|
---|
| 76 | s.append(NL).append(INDENT).append(tr("incomplete")).append(NL);
|
---|
| 77 | return;
|
---|
| 78 | }
|
---|
| 79 | s.append(NL);
|
---|
| 80 |
|
---|
| 81 | addState(o);
|
---|
| 82 | addCommon(o);
|
---|
| 83 | addAttributes(o);
|
---|
| 84 | addSpecial(o);
|
---|
| 85 | addReferrers(s, o);
|
---|
[13926] | 86 | if (o instanceof OsmPrimitive) {
|
---|
| 87 | addConflicts((OsmPrimitive) o);
|
---|
| 88 | }
|
---|
[10198] | 89 | s.append(NL);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[13926] | 92 | void addHeadline(IPrimitive o) {
|
---|
[10198] | 93 | addType(o);
|
---|
| 94 | addNameAndId(o);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[13926] | 97 | void addType(IPrimitive o) {
|
---|
| 98 | if (o instanceof INode) {
|
---|
[10198] | 99 | s.append(tr("Node: "));
|
---|
[13926] | 100 | } else if (o instanceof IWay) {
|
---|
[10198] | 101 | s.append(tr("Way: "));
|
---|
[13926] | 102 | } else if (o instanceof IRelation) {
|
---|
[10198] | 103 | s.append(tr("Relation: "));
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[13926] | 107 | void addNameAndId(IPrimitive o) {
|
---|
[10198] | 108 | String name = o.get("name");
|
---|
| 109 | if (name == null) {
|
---|
| 110 | s.append(o.getUniqueId());
|
---|
| 111 | } else {
|
---|
| 112 | s.append(getNameAndId(name, o.getUniqueId()));
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[13926] | 116 | void addState(IPrimitive o) {
|
---|
[10198] | 117 | StringBuilder sb = new StringBuilder(INDENT);
|
---|
| 118 | /* selected state is left out: not interesting as it is always selected */
|
---|
| 119 | if (o.isDeleted()) {
|
---|
| 120 | sb.append(tr("deleted")).append(INDENT);
|
---|
| 121 | }
|
---|
| 122 | if (!o.isVisible()) {
|
---|
| 123 | sb.append(tr("deleted-on-server")).append(INDENT);
|
---|
| 124 | }
|
---|
| 125 | if (o.isModified()) {
|
---|
| 126 | sb.append(tr("modified")).append(INDENT);
|
---|
| 127 | }
|
---|
| 128 | if (o.isDisabledAndHidden()) {
|
---|
| 129 | sb.append(tr("filtered/hidden")).append(INDENT);
|
---|
| 130 | }
|
---|
| 131 | if (o.isDisabled()) {
|
---|
| 132 | sb.append(tr("filtered/disabled")).append(INDENT);
|
---|
| 133 | }
|
---|
| 134 | if (o.hasDirectionKeys()) {
|
---|
| 135 | if (o.reversedDirection()) {
|
---|
| 136 | sb.append(tr("has direction keys (reversed)")).append(INDENT);
|
---|
| 137 | } else {
|
---|
| 138 | sb.append(tr("has direction keys")).append(INDENT);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | String state = sb.toString().trim();
|
---|
| 142 | if (!state.isEmpty()) {
|
---|
| 143 | add(tr("State: "), sb.toString().trim());
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[13926] | 147 | void addCommon(IPrimitive o) {
|
---|
[10198] | 148 | add(tr("Data Set: "), Integer.toHexString(o.getDataSet().hashCode()));
|
---|
| 149 | add(tr("Edited at: "), o.isTimestampEmpty() ? tr("<new object>")
|
---|
| 150 | : DateUtils.fromTimestamp(o.getRawTimestamp()));
|
---|
| 151 | add(tr("Edited by: "), o.getUser() == null ? tr("<new object>")
|
---|
| 152 | : getNameAndId(o.getUser().getName(), o.getUser().getId()));
|
---|
[13761] | 153 | add(tr("Version:"), " ", Integer.toString(o.getVersion()));
|
---|
[10198] | 154 | add(tr("In changeset: "), Integer.toString(o.getChangesetId()));
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[13926] | 157 | void addAttributes(IPrimitive o) {
|
---|
[10198] | 158 | if (o.hasKeys()) {
|
---|
| 159 | add(tr("Tags: "));
|
---|
| 160 | for (String key : o.keySet()) {
|
---|
| 161 | s.append(INDENT).append(INDENT);
|
---|
| 162 | s.append(String.format("\"%s\"=\"%s\"%n", key, o.get(key)));
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[13926] | 167 | void addSpecial(IPrimitive o) {
|
---|
| 168 | if (o instanceof INode) {
|
---|
| 169 | addCoordinates((INode) o);
|
---|
| 170 | } else if (o instanceof IWay) {
|
---|
[10198] | 171 | addBbox(o);
|
---|
[12745] | 172 | add(tr("Centroid: "),
|
---|
[14120] | 173 | toStringCSV(", ", ProjectionRegistry.getProjection().eastNorth2latlon(
|
---|
[13953] | 174 | Geometry.getCentroid(((IWay<?>) o).getNodes()))));
|
---|
[13926] | 175 | addWayNodes((IWay<?>) o);
|
---|
| 176 | } else if (o instanceof IRelation) {
|
---|
[10198] | 177 | addBbox(o);
|
---|
[13926] | 178 | addRelationMembers((IRelation<?>) o);
|
---|
[10198] | 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[13926] | 182 | void addRelationMembers(IRelation<?> r) {
|
---|
[10198] | 183 | add(trn("{0} Member: ", "{0} Members: ", r.getMembersCount(), r.getMembersCount()));
|
---|
[13926] | 184 | for (IRelationMember<?> m : r.getMembers()) {
|
---|
[10198] | 185 | s.append(INDENT).append(INDENT);
|
---|
| 186 | addHeadline(m.getMember());
|
---|
| 187 | s.append(tr(" as \"{0}\"", m.getRole()));
|
---|
| 188 | s.append(NL);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[13926] | 192 | void addWayNodes(IWay<?> w) {
|
---|
[10198] | 193 | add(tr("{0} Nodes: ", w.getNodesCount()));
|
---|
[13926] | 194 | for (INode n : w.getNodes()) {
|
---|
[10198] | 195 | s.append(INDENT).append(INDENT);
|
---|
| 196 | addNameAndId(n);
|
---|
| 197 | s.append(NL);
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[13926] | 201 | void addBbox(IPrimitive o) {
|
---|
[10198] | 202 | BBox bbox = o.getBBox();
|
---|
| 203 | if (bbox != null) {
|
---|
| 204 | add(tr("Bounding box: "), bbox.toStringCSV(", "));
|
---|
[14120] | 205 | EastNorth bottomRigth = bbox.getBottomRight().getEastNorth(ProjectionRegistry.getProjection());
|
---|
| 206 | EastNorth topLeft = bbox.getTopLeft().getEastNorth(ProjectionRegistry.getProjection());
|
---|
[10198] | 207 | add(tr("Bounding box (projected): "),
|
---|
| 208 | Double.toString(topLeft.east()), ", ",
|
---|
| 209 | Double.toString(bottomRigth.north()), ", ",
|
---|
| 210 | Double.toString(bottomRigth.east()), ", ",
|
---|
| 211 | Double.toString(topLeft.north()));
|
---|
[12745] | 212 | add(tr("Center of bounding box: "), toStringCSV(", ", bbox.getCenter()));
|
---|
[10198] | 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[13926] | 216 | void addCoordinates(INode n) {
|
---|
[12161] | 217 | if (n.isLatLonKnown()) {
|
---|
[13761] | 218 | add(tr("Coordinates:"), " ",
|
---|
[12161] | 219 | Double.toString(n.lat()), ", ",
|
---|
| 220 | Double.toString(n.lon()));
|
---|
[13167] | 221 | EastNorth en = n.getEastNorth();
|
---|
[10198] | 222 | add(tr("Coordinates (projected): "),
|
---|
[13167] | 223 | Double.toString(en.east()), ", ",
|
---|
| 224 | Double.toString(en.north()));
|
---|
| 225 | Pair<Integer, Hemisphere> utmZone = TransverseMercator.locateUtmZone(n.getCoor());
|
---|
| 226 | String utmLabel = tr("UTM Zone");
|
---|
[13186] | 227 | add(utmLabel, utmLabel.endsWith(":") ? " " : ": ", Integer.toString(utmZone.a), utmZone.b.name().substring(0, 1));
|
---|
[10198] | 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[13926] | 231 | void addReferrers(StringBuilder s, IPrimitive o) {
|
---|
| 232 | List<? extends IPrimitive> refs = o.getReferrers();
|
---|
[10198] | 233 | if (!refs.isEmpty()) {
|
---|
| 234 | add(tr("Part of: "));
|
---|
[13926] | 235 | for (IPrimitive p : refs) {
|
---|
[10198] | 236 | s.append(INDENT).append(INDENT);
|
---|
| 237 | addHeadline(p);
|
---|
| 238 | s.append(NL);
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void addConflicts(OsmPrimitive o) {
|
---|
[13926] | 244 | Conflict<?> c = ((DataSet) ds).getConflicts().getConflictForMy(o);
|
---|
[10198] | 245 | if (c != null) {
|
---|
| 246 | add(tr("In conflict with: "));
|
---|
| 247 | addNameAndId(c.getTheir());
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[12745] | 251 | /**
|
---|
| 252 | * Returns lat/lon coordinate in human-readable format separated by {@code separator}.
|
---|
| 253 | * @param separator values separator
|
---|
| 254 | * @param ll the lat/lon
|
---|
| 255 | * @return String in the format {@code "1.23456[separator]2.34567"}
|
---|
| 256 | */
|
---|
| 257 | private static String toStringCSV(String separator, ILatLon ll) {
|
---|
| 258 | return Utils.join(separator, Arrays.asList(
|
---|
| 259 | DecimalDegreesCoordinateFormat.INSTANCE.latToString(ll),
|
---|
| 260 | DecimalDegreesCoordinateFormat.INSTANCE.lonToString(ll)
|
---|
| 261 | ));
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[10198] | 264 | @Override
|
---|
| 265 | public String toString() {
|
---|
| 266 | return s.toString();
|
---|
| 267 | }
|
---|
| 268 | }
|
---|