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 |
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
12 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
13 | import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
|
---|
14 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
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;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
24 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
---|
25 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator.Hemisphere;
|
---|
26 | import org.openstreetmap.josm.tools.Geometry;
|
---|
27 | import org.openstreetmap.josm.tools.Pair;
|
---|
28 | import org.openstreetmap.josm.tools.Utils;
|
---|
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();
|
---|
40 | private final OsmData<?, ?, ?, ?> ds;
|
---|
41 |
|
---|
42 | InspectPrimitiveDataText(OsmData<?, ?, ?, ?> ds) {
|
---|
43 | this.ds = ds;
|
---|
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) {
|
---|
57 | return name + tr(" ({0})", /* sic to avoid thousand separators */ Long.toString(id));
|
---|
58 | } else {
|
---|
59 | return Long.toString(id);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Adds a new OSM primitive.
|
---|
65 | * @param o primitive to add
|
---|
66 | */
|
---|
67 | public void addPrimitive(IPrimitive o) {
|
---|
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);
|
---|
86 | if (o instanceof OsmPrimitive) {
|
---|
87 | addConflicts((OsmPrimitive) o);
|
---|
88 | }
|
---|
89 | s.append(NL);
|
---|
90 | }
|
---|
91 |
|
---|
92 | void addHeadline(IPrimitive o) {
|
---|
93 | addType(o);
|
---|
94 | addNameAndId(o);
|
---|
95 | }
|
---|
96 |
|
---|
97 | void addType(IPrimitive o) {
|
---|
98 | if (o instanceof INode) {
|
---|
99 | s.append(tr("Node: "));
|
---|
100 | } else if (o instanceof IWay) {
|
---|
101 | s.append(tr("Way: "));
|
---|
102 | } else if (o instanceof IRelation) {
|
---|
103 | s.append(tr("Relation: "));
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | void addNameAndId(IPrimitive o) {
|
---|
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 |
|
---|
116 | void addState(IPrimitive o) {
|
---|
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 |
|
---|
147 | void addCommon(IPrimitive o) {
|
---|
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()));
|
---|
153 | add(tr("Version:"), " ", Integer.toString(o.getVersion()));
|
---|
154 | add(tr("In changeset: "), Integer.toString(o.getChangesetId()));
|
---|
155 | }
|
---|
156 |
|
---|
157 | void addAttributes(IPrimitive o) {
|
---|
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 |
|
---|
167 | void addSpecial(IPrimitive o) {
|
---|
168 | if (o instanceof INode) {
|
---|
169 | addCoordinates((INode) o);
|
---|
170 | } else if (o instanceof IWay) {
|
---|
171 | addBbox(o);
|
---|
172 | add(tr("Centroid: "),
|
---|
173 | toStringCSV(", ", ProjectionRegistry.getProjection().eastNorth2latlon(
|
---|
174 | Geometry.getCentroid(((IWay<?>) o).getNodes()))));
|
---|
175 | addWayNodes((IWay<?>) o);
|
---|
176 | } else if (o instanceof IRelation) {
|
---|
177 | addBbox(o);
|
---|
178 | addRelationMembers((IRelation<?>) o);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | void addRelationMembers(IRelation<?> r) {
|
---|
183 | add(trn("{0} Member: ", "{0} Members: ", r.getMembersCount(), r.getMembersCount()));
|
---|
184 | for (IRelationMember<?> m : r.getMembers()) {
|
---|
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 |
|
---|
192 | void addWayNodes(IWay<?> w) {
|
---|
193 | add(tr("{0} Nodes: ", w.getNodesCount()));
|
---|
194 | for (INode n : w.getNodes()) {
|
---|
195 | s.append(INDENT).append(INDENT);
|
---|
196 | addNameAndId(n);
|
---|
197 | s.append(NL);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | void addBbox(IPrimitive o) {
|
---|
202 | BBox bbox = o.getBBox();
|
---|
203 | if (bbox != null) {
|
---|
204 | add(tr("Bounding box: "), bbox.toStringCSV(", "));
|
---|
205 | EastNorth bottomRigth = bbox.getBottomRight().getEastNorth(ProjectionRegistry.getProjection());
|
---|
206 | EastNorth topLeft = bbox.getTopLeft().getEastNorth(ProjectionRegistry.getProjection());
|
---|
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()));
|
---|
212 | add(tr("Center of bounding box: "), toStringCSV(", ", bbox.getCenter()));
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | void addCoordinates(INode n) {
|
---|
217 | if (n.isLatLonKnown()) {
|
---|
218 | add(tr("Coordinates:"), " ",
|
---|
219 | Double.toString(n.lat()), ", ",
|
---|
220 | Double.toString(n.lon()));
|
---|
221 | EastNorth en = n.getEastNorth();
|
---|
222 | add(tr("Coordinates (projected): "),
|
---|
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");
|
---|
227 | add(utmLabel, utmLabel.endsWith(":") ? " " : ": ", Integer.toString(utmZone.a), utmZone.b.name().substring(0, 1));
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | void addReferrers(StringBuilder s, IPrimitive o) {
|
---|
232 | List<? extends IPrimitive> refs = o.getReferrers();
|
---|
233 | if (!refs.isEmpty()) {
|
---|
234 | add(tr("Part of: "));
|
---|
235 | for (IPrimitive p : refs) {
|
---|
236 | s.append(INDENT).append(INDENT);
|
---|
237 | addHeadline(p);
|
---|
238 | s.append(NL);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void addConflicts(OsmPrimitive o) {
|
---|
244 | Conflict<?> c = ((DataSet) ds).getConflicts().getConflictForMy(o);
|
---|
245 | if (c != null) {
|
---|
246 | add(tr("In conflict with: "));
|
---|
247 | addNameAndId(c.getTheir());
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
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 |
|
---|
264 | @Override
|
---|
265 | public String toString() {
|
---|
266 | return s.toString();
|
---|
267 | }
|
---|
268 | }
|
---|