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.awt.Dimension;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map.Entry;
|
---|
14 |
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.JScrollPane;
|
---|
17 | import javax.swing.JTabbedPane;
|
---|
18 | import javax.swing.JTextArea;
|
---|
19 | import javax.swing.SingleSelectionModel;
|
---|
20 | import javax.swing.event.ChangeEvent;
|
---|
21 | import javax.swing.event.ChangeListener;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
25 | import org.openstreetmap.josm.data.osm.Node;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
27 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
28 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
29 | import org.openstreetmap.josm.data.osm.User;
|
---|
30 | import org.openstreetmap.josm.data.osm.Way;
|
---|
31 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
32 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
33 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
34 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
35 | import org.openstreetmap.josm.gui.mappaint.ElemStyle;
|
---|
36 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
37 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
38 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
39 | import org.openstreetmap.josm.gui.mappaint.StyleCache;
|
---|
40 | import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
|
---|
41 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
42 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
43 | import org.openstreetmap.josm.gui.mappaint.xml.XmlStyleSource;
|
---|
44 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
45 | import org.openstreetmap.josm.tools.GBC;
|
---|
46 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
47 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Panel to inspect one or more OsmPrimitives.
|
---|
51 | *
|
---|
52 | * Gives an unfiltered view of the object's internal state.
|
---|
53 | * Might be useful for power users to give more detailed bug reports and
|
---|
54 | * to better understand the JOSM data representation.
|
---|
55 | *
|
---|
56 | * TODO: show conflicts
|
---|
57 | */
|
---|
58 | public class InspectPrimitiveDialog extends ExtendedDialog {
|
---|
59 | protected Collection<OsmPrimitive> primitives;
|
---|
60 | private JTextArea txtData;
|
---|
61 | private JTextArea txtMappaint;
|
---|
62 | boolean mappaintTabLoaded;
|
---|
63 |
|
---|
64 | public InspectPrimitiveDialog(Collection<OsmPrimitive> primitives) {
|
---|
65 | super(Main.parent, tr("Advanced object info"), new String[] {tr("Close")});
|
---|
66 | this.primitives = primitives;
|
---|
67 | setRememberWindowGeometry(getClass().getName() + ".geometry",
|
---|
68 | WindowGeometry.centerInWindow(Main.parent, new Dimension(750, 550)));
|
---|
69 |
|
---|
70 | setButtonIcons(new String[] {"ok.png"});
|
---|
71 | final JTabbedPane tabs = new JTabbedPane();
|
---|
72 | JPanel pData = buildDataPanel();
|
---|
73 | tabs.addTab(tr("data"), pData);
|
---|
74 | final JPanel pMapPaint = new JPanel();
|
---|
75 | tabs.addTab(tr("map style"), pMapPaint);
|
---|
76 | tabs.getModel().addChangeListener(new ChangeListener() {
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public void stateChanged(ChangeEvent e) {
|
---|
80 | if (!mappaintTabLoaded && ((SingleSelectionModel) e.getSource()).getSelectedIndex() == 1) {
|
---|
81 | mappaintTabLoaded = true;
|
---|
82 | buildMapPaintPanel(pMapPaint);
|
---|
83 | createMapPaintText();
|
---|
84 | }
|
---|
85 | }
|
---|
86 | });
|
---|
87 | txtData.setText(buildDataText());
|
---|
88 | setContent(tabs, false);
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected JPanel buildDataPanel() {
|
---|
92 | JPanel p = new JPanel(new GridBagLayout());
|
---|
93 | txtData = new JTextArea();
|
---|
94 | txtData.setFont(new Font("Monospaced", txtData.getFont().getStyle(), txtData.getFont().getSize()));
|
---|
95 | txtData.setEditable(false);
|
---|
96 |
|
---|
97 | JScrollPane scroll = new JScrollPane(txtData);
|
---|
98 |
|
---|
99 | p.add(scroll, GBC.std().fill());
|
---|
100 | return p;
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected String buildDataText() {
|
---|
104 | StringBuilder s = new StringBuilder();
|
---|
105 | for (Node n : new SubclassFilteredCollection<OsmPrimitive, Node>(primitives, OsmPrimitive.nodePredicate)) {
|
---|
106 | s.append(tr("Node id={0}", n.getUniqueId()));
|
---|
107 | if (!checkDataSet(n)) {
|
---|
108 | s.append(tr(" not in data set"));
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 | if (n.isIncomplete()) {
|
---|
112 | s.append(tr(" incomplete\n"));
|
---|
113 | addWayReferrer(s, n);
|
---|
114 | addRelationReferrer(s, n);
|
---|
115 | continue;
|
---|
116 | }
|
---|
117 | s.append(tr(" lat={0} lon={1} (projected: x={2}, y={3}); ",
|
---|
118 | Double.toString(n.getCoor().lat()), Double.toString(n.getCoor().lon()),
|
---|
119 | Double.toString(n.getEastNorth().east()), Double.toString(n.getEastNorth().north())));
|
---|
120 | addCommon(s, n);
|
---|
121 | addAttributes(s, n);
|
---|
122 | addWayReferrer(s, n);
|
---|
123 | addRelationReferrer(s, n);
|
---|
124 | s.append('\n');
|
---|
125 | }
|
---|
126 |
|
---|
127 | for (Way w : new SubclassFilteredCollection<OsmPrimitive, Way>(primitives, OsmPrimitive.wayPredicate)) {
|
---|
128 | s.append(tr("Way id={0}", w.getUniqueId()));
|
---|
129 | if (!checkDataSet(w)) {
|
---|
130 | s.append(tr(" not in data set"));
|
---|
131 | continue;
|
---|
132 | }
|
---|
133 | if (w.isIncomplete()) {
|
---|
134 | s.append(tr(" incomplete\n"));
|
---|
135 | addRelationReferrer(s, w);
|
---|
136 | continue;
|
---|
137 | }
|
---|
138 | s.append(trn(" {0} node; ", " {0} nodes; ", w.getNodes().size(), w.getNodes().size()));
|
---|
139 | addCommon(s, w);
|
---|
140 | addAttributes(s, w);
|
---|
141 | addRelationReferrer(s, w);
|
---|
142 |
|
---|
143 | s.append(tr(" nodes:\n"));
|
---|
144 | for (Node n : w.getNodes()) {
|
---|
145 | s.append(String.format(" %d\n", n.getUniqueId()));
|
---|
146 | }
|
---|
147 | s.append('\n');
|
---|
148 | }
|
---|
149 |
|
---|
150 | for (Relation r : new SubclassFilteredCollection<OsmPrimitive, Relation>(primitives, OsmPrimitive.relationPredicate)) {
|
---|
151 | s.append(tr("Relation id={0}",r.getUniqueId()));
|
---|
152 | if (!checkDataSet(r)) {
|
---|
153 | s.append(tr(" not in data set"));
|
---|
154 | continue;
|
---|
155 | }
|
---|
156 | if (r.isIncomplete()) {
|
---|
157 | s.append(tr(" incomplete\n"));
|
---|
158 | addRelationReferrer(s, r);
|
---|
159 | continue;
|
---|
160 | }
|
---|
161 | s.append(trn(" {0} member; ", " {0} members; ", r.getMembersCount(), r.getMembersCount()));
|
---|
162 | addCommon(s, r);
|
---|
163 | addAttributes(s, r);
|
---|
164 | addRelationReferrer(s, r);
|
---|
165 |
|
---|
166 | s.append(tr(" members:\n"));
|
---|
167 | for (RelationMember m : r.getMembers() ) {
|
---|
168 | s.append(String.format(" %s%d '%s'\n", m.getMember().getType().getAPIName().substring(0,1), m.getMember().getUniqueId(), m.getRole()));
|
---|
169 | }
|
---|
170 | s.append('\n');
|
---|
171 | }
|
---|
172 |
|
---|
173 | return s.toString().trim();
|
---|
174 | }
|
---|
175 |
|
---|
176 | protected void addCommon(StringBuilder s, OsmPrimitive o) {
|
---|
177 | s.append(tr("Data set: {0}; User: [{1}]; ChangeSet id: {2}; Timestamp: {3}, Version: {4}",
|
---|
178 | Integer.toHexString(o.getDataSet().hashCode()),
|
---|
179 | userString(o.getUser()),
|
---|
180 | o.getChangesetId(),
|
---|
181 | o.isTimestampEmpty() ? tr("<new object>") : DateUtils.fromDate(o.getTimestamp()),
|
---|
182 | o.getVersion()));
|
---|
183 |
|
---|
184 | /* selected state is left out: not interesting as it is always selected */
|
---|
185 | if (o.isDeleted()) {
|
---|
186 | s.append(tr("; deleted"));
|
---|
187 | }
|
---|
188 | if (!o.isVisible()) {
|
---|
189 | s.append(tr("; deleted-on-server"));
|
---|
190 | }
|
---|
191 | if (o.isModified()) {
|
---|
192 | s.append(tr("; modified"));
|
---|
193 | }
|
---|
194 | if (o.isDisabledAndHidden()) {
|
---|
195 | s.append(tr("; filtered/hidden"));
|
---|
196 | }
|
---|
197 | if (o.isDisabled()) {
|
---|
198 | s.append(tr("; filtered/disabled"));
|
---|
199 | }
|
---|
200 | if (o.hasDirectionKeys()) {
|
---|
201 | s.append(tr("; has direction keys"));
|
---|
202 | if (o.reversedDirection()) {
|
---|
203 | s.append(tr(" (reversed)"));
|
---|
204 | }
|
---|
205 | }
|
---|
206 | s.append("\n");
|
---|
207 | }
|
---|
208 |
|
---|
209 | protected void addAttributes(StringBuilder s, OsmPrimitive o) {
|
---|
210 | if (o.hasKeys()) {
|
---|
211 | s.append(tr(" tags:\n"));
|
---|
212 | for (String key: o.keySet()) {
|
---|
213 | s.append(String.format(" \"%s\"=\"%s\"\n", key, o.get(key)));
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | protected void addWayReferrer(StringBuilder s, Node n) {
|
---|
219 | // add way referrer
|
---|
220 | List<OsmPrimitive> refs = n.getReferrers();
|
---|
221 | Collection<Way> wayRefs = new SubclassFilteredCollection<OsmPrimitive, Way>(refs, OsmPrimitive.wayPredicate);
|
---|
222 | if (wayRefs.size() > 0) {
|
---|
223 | s.append(tr(" way referrer:\n"));
|
---|
224 | for (Way w : wayRefs) {
|
---|
225 | s.append(" "+w.getUniqueId()+"\n");
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | protected void addRelationReferrer(StringBuilder s, OsmPrimitive o) {
|
---|
231 | List<OsmPrimitive> refs = o.getReferrers();
|
---|
232 | Collection<Relation> relRefs = new SubclassFilteredCollection<OsmPrimitive, Relation>(refs, OsmPrimitive.relationPredicate);
|
---|
233 | if (relRefs.size() > 0) {
|
---|
234 | s.append(tr(" relation referrer:\n"));
|
---|
235 | for (Relation r : relRefs) {
|
---|
236 | s.append(" "+r.getUniqueId()+"\n");
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * See if primitive is in a data set properly.
|
---|
243 | * This does not hold for primitives that are new and deleted.
|
---|
244 | */
|
---|
245 | protected boolean checkDataSet(OsmPrimitive o) {
|
---|
246 | DataSet ds = o.getDataSet();
|
---|
247 | if (ds == null)
|
---|
248 | return false;
|
---|
249 | return ds.getPrimitiveById(o) != null;
|
---|
250 | }
|
---|
251 |
|
---|
252 | protected String userString(User user) {
|
---|
253 | if (user == null)
|
---|
254 | return tr("<new object>");
|
---|
255 |
|
---|
256 | List<String> names = user.getNames();
|
---|
257 |
|
---|
258 | StringBuilder us = new StringBuilder();
|
---|
259 |
|
---|
260 | us.append(tr("id: {0}",user.getId()));
|
---|
261 | if (names.size() == 1) {
|
---|
262 | us.append(tr(" name: {0}",user.getName()));
|
---|
263 | }
|
---|
264 | else if (names.size() > 1) {
|
---|
265 | us.append(trn(" {0} name: {1}", " {0} names: {1}", names.size(), names.size(), user.getName()));
|
---|
266 | }
|
---|
267 | return us.toString();
|
---|
268 | }
|
---|
269 |
|
---|
270 | protected void buildMapPaintPanel(JPanel p) {
|
---|
271 | p.setLayout(new GridBagLayout());
|
---|
272 | txtMappaint = new JTextArea();
|
---|
273 | txtMappaint.setFont(new Font("Monospaced", txtMappaint.getFont().getStyle(), txtMappaint.getFont().getSize()));
|
---|
274 | txtMappaint.setEditable(false);
|
---|
275 |
|
---|
276 | p.add(new JScrollPane(txtMappaint), GBC.std().fill());
|
---|
277 | }
|
---|
278 |
|
---|
279 | protected void createMapPaintText() {
|
---|
280 | final Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
|
---|
281 | ElemStyles elemstyles = MapPaintStyles.getStyles();
|
---|
282 | NavigatableComponent nc = Main.map.mapView;
|
---|
283 | double scale = nc.getDist100Pixel();
|
---|
284 |
|
---|
285 | for (OsmPrimitive osm : sel) {
|
---|
286 | txtMappaint.append(tr("Styles Cache for \"{0}\":",osm.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
287 |
|
---|
288 | MultiCascade mc = new MultiCascade();
|
---|
289 |
|
---|
290 | for (StyleSource s : elemstyles.getStyleSources()) {
|
---|
291 | if (s.active) {
|
---|
292 | txtMappaint.append(tr("\n\n> applying {0} style \"{1}\"\n",getSort(s), s.getDisplayString()));
|
---|
293 | s.apply(mc, osm, scale, null, false);
|
---|
294 | txtMappaint.append(tr("\nRange:{0}",mc.range));
|
---|
295 | for (Entry<String, Cascade> e : mc.getLayers()) {
|
---|
296 | txtMappaint.append("\n "+e.getKey()+": \n"+e.getValue());
|
---|
297 | }
|
---|
298 | } else {
|
---|
299 | txtMappaint.append(tr("\n\n> skipping \"{0}\" (not active)",s.getDisplayString()));
|
---|
300 | }
|
---|
301 | }
|
---|
302 | txtMappaint.append(tr("\n\nList of generated Styles:\n"));
|
---|
303 | StyleList sl = elemstyles.get(osm, scale, nc);
|
---|
304 | for (ElemStyle s : sl) {
|
---|
305 | txtMappaint.append(" * "+s+"\n");
|
---|
306 | }
|
---|
307 | txtMappaint.append("\n\n");
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (sel.size() == 2) {
|
---|
311 | List<OsmPrimitive> selList = new ArrayList<OsmPrimitive>(sel);
|
---|
312 | StyleCache sc1 = selList.get(0).mappaintStyle;
|
---|
313 | StyleCache sc2 = selList.get(1).mappaintStyle;
|
---|
314 | if (sc1 == sc2) {
|
---|
315 | txtMappaint.append(tr("The 2 selected objects have identical style caches."));
|
---|
316 | }
|
---|
317 | if (!sc1.equals(sc2)) {
|
---|
318 | txtMappaint.append(tr("The 2 selected objects have different style caches."));
|
---|
319 | }
|
---|
320 | if (sc1.equals(sc2) && sc1 != sc2) {
|
---|
321 | txtMappaint.append(tr("Warning: The 2 selected objects have equal, but not identical style caches."));
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | private String getSort(StyleSource s) {
|
---|
327 | if (s instanceof XmlStyleSource)
|
---|
328 | return tr("xml");
|
---|
329 | if (s instanceof MapCSSStyleSource)
|
---|
330 | return tr("mapcss");
|
---|
331 | return tr("unkown");
|
---|
332 | }
|
---|
333 |
|
---|
334 | }
|
---|