1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dialog;
|
---|
10 | import java.awt.FlowLayout;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.net.HttpURLConnection;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.Iterator;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Set;
|
---|
18 | import java.util.Stack;
|
---|
19 |
|
---|
20 | import javax.swing.AbstractAction;
|
---|
21 | import javax.swing.JButton;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.JScrollPane;
|
---|
25 | import javax.swing.SwingUtilities;
|
---|
26 | import javax.swing.event.TreeSelectionEvent;
|
---|
27 | import javax.swing.event.TreeSelectionListener;
|
---|
28 | import javax.swing.tree.TreePath;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
32 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
33 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
34 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
35 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
36 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
37 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
38 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
39 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
40 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
41 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
42 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
43 | import org.openstreetmap.josm.io.OsmApi;
|
---|
44 | import org.openstreetmap.josm.io.OsmApiException;
|
---|
45 | import org.openstreetmap.josm.io.OsmServerObjectReader;
|
---|
46 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
47 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
48 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
49 | import org.openstreetmap.josm.tools.Logging;
|
---|
50 | import org.openstreetmap.josm.tools.Utils;
|
---|
51 | import org.xml.sax.SAXException;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * ChildRelationBrowser is a UI component which provides a tree-like view on the hierarchical
|
---|
55 | * structure of relations.
|
---|
56 | *
|
---|
57 | * @since 1828
|
---|
58 | */
|
---|
59 | public class ChildRelationBrowser extends JPanel {
|
---|
60 | /** the tree with relation children */
|
---|
61 | private RelationTree childTree;
|
---|
62 | /** the tree model */
|
---|
63 | private transient RelationTreeModel model;
|
---|
64 |
|
---|
65 | /** the osm data layer this browser is related to */
|
---|
66 | private transient OsmDataLayer layer;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Replies the {@link OsmDataLayer} this editor is related to
|
---|
70 | *
|
---|
71 | * @return the osm data layer
|
---|
72 | */
|
---|
73 | protected OsmDataLayer getLayer() {
|
---|
74 | return layer;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * builds the UI
|
---|
79 | */
|
---|
80 | protected void build() {
|
---|
81 | setLayout(new BorderLayout());
|
---|
82 | childTree = new RelationTree(model);
|
---|
83 | JScrollPane pane = new JScrollPane(childTree);
|
---|
84 | add(pane, BorderLayout.CENTER);
|
---|
85 |
|
---|
86 | add(buildButtonPanel(), BorderLayout.SOUTH);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * builds the panel with the command buttons
|
---|
91 | *
|
---|
92 | * @return the button panel
|
---|
93 | */
|
---|
94 | protected JPanel buildButtonPanel() {
|
---|
95 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
96 |
|
---|
97 | // ---
|
---|
98 | DownloadAllChildRelationsAction downloadAction = new DownloadAllChildRelationsAction();
|
---|
99 | pnl.add(new JButton(downloadAction));
|
---|
100 |
|
---|
101 | // ---
|
---|
102 | DownloadSelectedAction downloadSelectedAction = new DownloadSelectedAction();
|
---|
103 | childTree.addTreeSelectionListener(downloadSelectedAction);
|
---|
104 | pnl.add(new JButton(downloadSelectedAction));
|
---|
105 |
|
---|
106 | // ---
|
---|
107 | EditAction editAction = new EditAction();
|
---|
108 | childTree.addTreeSelectionListener(editAction);
|
---|
109 | pnl.add(new JButton(editAction));
|
---|
110 |
|
---|
111 | return pnl;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * constructor
|
---|
116 | *
|
---|
117 | * @param layer the {@link OsmDataLayer} this browser is related to. Must not be null.
|
---|
118 | * @throws IllegalArgumentException if layer is null
|
---|
119 | */
|
---|
120 | public ChildRelationBrowser(OsmDataLayer layer) {
|
---|
121 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
122 | this.layer = layer;
|
---|
123 | model = new RelationTreeModel();
|
---|
124 | build();
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * constructor
|
---|
129 | *
|
---|
130 | * @param layer the {@link OsmDataLayer} this browser is related to. Must not be null.
|
---|
131 | * @param root the root relation
|
---|
132 | * @throws IllegalArgumentException if layer is null
|
---|
133 | */
|
---|
134 | public ChildRelationBrowser(OsmDataLayer layer, Relation root) {
|
---|
135 | this(layer);
|
---|
136 | populate(root);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * populates the browser with a relation
|
---|
141 | *
|
---|
142 | * @param r the relation
|
---|
143 | */
|
---|
144 | public void populate(Relation r) {
|
---|
145 | model.populate(r);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * populates the browser with a list of relation members
|
---|
150 | *
|
---|
151 | * @param members the list of relation members
|
---|
152 | */
|
---|
153 |
|
---|
154 | public void populate(List<RelationMember> members) {
|
---|
155 | model.populate(members);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * replies the parent dialog this browser is embedded in
|
---|
160 | *
|
---|
161 | * @return the parent dialog; null, if there is no {@link Dialog} as parent dialog
|
---|
162 | */
|
---|
163 | protected Dialog getParentDialog() {
|
---|
164 | Component c = this;
|
---|
165 | while (c != null && !(c instanceof Dialog)) {
|
---|
166 | c = c.getParent();
|
---|
167 | }
|
---|
168 | return (Dialog) c;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Action for editing the currently selected relation
|
---|
173 | *
|
---|
174 | *
|
---|
175 | */
|
---|
176 | class EditAction extends AbstractAction implements TreeSelectionListener {
|
---|
177 | EditAction() {
|
---|
178 | putValue(SHORT_DESCRIPTION, tr("Edit the relation the currently selected relation member refers to."));
|
---|
179 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
180 | putValue(NAME, tr("Edit"));
|
---|
181 | refreshEnabled();
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected void refreshEnabled() {
|
---|
185 | TreePath[] selection = childTree.getSelectionPaths();
|
---|
186 | setEnabled(selection != null && selection.length > 0);
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void run() {
|
---|
190 | TreePath[] selection = childTree.getSelectionPaths();
|
---|
191 | if (selection == null || selection.length == 0) return;
|
---|
192 | // do not launch more than 10 relation editors in parallel
|
---|
193 | //
|
---|
194 | for (int i = 0; i < Math.min(selection.length, 10); i++) {
|
---|
195 | Relation r = (Relation) selection[i].getLastPathComponent();
|
---|
196 | if (r.isIncomplete()) {
|
---|
197 | continue;
|
---|
198 | }
|
---|
199 | RelationEditor editor = RelationEditor.getEditor(getLayer(), r, null);
|
---|
200 | editor.setVisible(true);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public void actionPerformed(ActionEvent e) {
|
---|
206 | if (!isEnabled())
|
---|
207 | return;
|
---|
208 | run();
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public void valueChanged(TreeSelectionEvent e) {
|
---|
213 | refreshEnabled();
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Action for downloading all child relations for a given parent relation.
|
---|
219 | * Recursively.
|
---|
220 | */
|
---|
221 | class DownloadAllChildRelationsAction extends AbstractAction {
|
---|
222 | DownloadAllChildRelationsAction() {
|
---|
223 | putValue(SHORT_DESCRIPTION, tr("Download all child relations (recursively)"));
|
---|
224 | putValue(SMALL_ICON, ImageProvider.get("download"));
|
---|
225 | putValue(NAME, tr("Download All Children"));
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void run() {
|
---|
229 | MainApplication.worker.submit(new DownloadAllChildrenTask(getParentDialog(), (Relation) model.getRoot()));
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public void actionPerformed(ActionEvent e) {
|
---|
234 | if (!isEnabled())
|
---|
235 | return;
|
---|
236 | run();
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Action for downloading all selected relations
|
---|
242 | */
|
---|
243 | class DownloadSelectedAction extends AbstractAction implements TreeSelectionListener {
|
---|
244 | DownloadSelectedAction() {
|
---|
245 | putValue(SHORT_DESCRIPTION, tr("Download selected relations"));
|
---|
246 | // FIXME: replace with better icon
|
---|
247 | //
|
---|
248 | putValue(SMALL_ICON, ImageProvider.get("download"));
|
---|
249 | putValue(NAME, tr("Download Selected Children"));
|
---|
250 | updateEnabledState();
|
---|
251 | }
|
---|
252 |
|
---|
253 | protected void updateEnabledState() {
|
---|
254 | TreePath[] selection = childTree.getSelectionPaths();
|
---|
255 | setEnabled(selection != null && selection.length > 0);
|
---|
256 | }
|
---|
257 |
|
---|
258 | public void run() {
|
---|
259 | TreePath[] selection = childTree.getSelectionPaths();
|
---|
260 | if (selection == null || selection.length == 0)
|
---|
261 | return;
|
---|
262 | Set<Relation> relations = new HashSet<>();
|
---|
263 | for (TreePath aSelection : selection) {
|
---|
264 | relations.add((Relation) aSelection.getLastPathComponent());
|
---|
265 | }
|
---|
266 | MainApplication.worker.submit(new DownloadRelationSetTask(getParentDialog(), relations));
|
---|
267 | }
|
---|
268 |
|
---|
269 | @Override
|
---|
270 | public void actionPerformed(ActionEvent e) {
|
---|
271 | if (!isEnabled())
|
---|
272 | return;
|
---|
273 | run();
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | public void valueChanged(TreeSelectionEvent e) {
|
---|
278 | updateEnabledState();
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | abstract class DownloadTask extends PleaseWaitRunnable {
|
---|
283 | protected boolean canceled;
|
---|
284 | protected int conflictsCount;
|
---|
285 | protected Exception lastException;
|
---|
286 |
|
---|
287 | DownloadTask(String title, Dialog parent) {
|
---|
288 | super(title, new PleaseWaitProgressMonitor(parent), false);
|
---|
289 | }
|
---|
290 |
|
---|
291 | @Override
|
---|
292 | protected void cancel() {
|
---|
293 | canceled = true;
|
---|
294 | OsmApi.getOsmApi().cancel();
|
---|
295 | }
|
---|
296 |
|
---|
297 | protected void refreshView(Relation relation) {
|
---|
298 | for (int i = 0; i < childTree.getRowCount(); i++) {
|
---|
299 | Relation reference = (Relation) childTree.getPathForRow(i).getLastPathComponent();
|
---|
300 | if (reference == relation) {
|
---|
301 | model.refreshNode(childTree.getPathForRow(i));
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | @Override
|
---|
307 | protected void finish() {
|
---|
308 | if (canceled)
|
---|
309 | return;
|
---|
310 | if (lastException != null) {
|
---|
311 | ExceptionDialogUtil.explainException(lastException);
|
---|
312 | return;
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (conflictsCount > 0) {
|
---|
316 | JOptionPane.showMessageDialog(
|
---|
317 | Main.parent,
|
---|
318 | trn("There was {0} conflict during import.",
|
---|
319 | "There were {0} conflicts during import.",
|
---|
320 | conflictsCount, conflictsCount),
|
---|
321 | trn("Conflict in data", "Conflicts in data", conflictsCount),
|
---|
322 | JOptionPane.WARNING_MESSAGE
|
---|
323 | );
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * The asynchronous task for downloading relation members.
|
---|
330 | */
|
---|
331 | class DownloadAllChildrenTask extends DownloadTask {
|
---|
332 | private final Stack<Relation> relationsToDownload;
|
---|
333 | private final Set<Long> downloadedRelationIds;
|
---|
334 |
|
---|
335 | DownloadAllChildrenTask(Dialog parent, Relation r) {
|
---|
336 | super(tr("Download relation members"), parent);
|
---|
337 | relationsToDownload = new Stack<>();
|
---|
338 | downloadedRelationIds = new HashSet<>();
|
---|
339 | relationsToDownload.push(r);
|
---|
340 | }
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * warns the user if a relation couldn't be loaded because it was deleted on
|
---|
344 | * the server (the server replied a HTTP code 410)
|
---|
345 | *
|
---|
346 | * @param r the relation
|
---|
347 | */
|
---|
348 | protected void warnBecauseOfDeletedRelation(Relation r) {
|
---|
349 | String message = tr("<html>The child relation<br>"
|
---|
350 | + "{0}<br>"
|
---|
351 | + "is deleted on the server. It cannot be loaded</html>",
|
---|
352 | Utils.escapeReservedCharactersHTML(r.getDisplayName(DefaultNameFormatter.getInstance()))
|
---|
353 | );
|
---|
354 |
|
---|
355 | JOptionPane.showMessageDialog(
|
---|
356 | Main.parent,
|
---|
357 | message,
|
---|
358 | tr("Relation is deleted"),
|
---|
359 | JOptionPane.WARNING_MESSAGE
|
---|
360 | );
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Remembers the child relations to download
|
---|
365 | *
|
---|
366 | * @param parent the parent relation
|
---|
367 | */
|
---|
368 | protected void rememberChildRelationsToDownload(Relation parent) {
|
---|
369 | downloadedRelationIds.add(parent.getId());
|
---|
370 | for (RelationMember member: parent.getMembers()) {
|
---|
371 | if (member.isRelation()) {
|
---|
372 | Relation child = member.getRelation();
|
---|
373 | if (!downloadedRelationIds.contains(child.getId())) {
|
---|
374 | relationsToDownload.push(child);
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Merges the primitives in <code>ds</code> to the dataset of the
|
---|
382 | * edit layer
|
---|
383 | *
|
---|
384 | * @param ds the data set
|
---|
385 | */
|
---|
386 | protected void mergeDataSet(DataSet ds) {
|
---|
387 | if (ds != null) {
|
---|
388 | final DataSetMerger visitor = new DataSetMerger(getLayer().data, ds);
|
---|
389 | visitor.merge();
|
---|
390 | if (!visitor.getConflicts().isEmpty()) {
|
---|
391 | getLayer().getConflicts().add(visitor.getConflicts());
|
---|
392 | conflictsCount += visitor.getConflicts().size();
|
---|
393 | }
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | @Override
|
---|
398 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
399 | try {
|
---|
400 | while (!relationsToDownload.isEmpty() && !canceled) {
|
---|
401 | Relation r = relationsToDownload.pop();
|
---|
402 | if (r.isNew()) {
|
---|
403 | continue;
|
---|
404 | }
|
---|
405 | rememberChildRelationsToDownload(r);
|
---|
406 | progressMonitor.setCustomText(tr("Downloading relation {0}", r.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
407 | OsmServerObjectReader reader = new OsmServerObjectReader(r.getId(), OsmPrimitiveType.RELATION,
|
---|
408 | true);
|
---|
409 | DataSet dataSet = null;
|
---|
410 | try {
|
---|
411 | dataSet = reader.parseOsm(progressMonitor
|
---|
412 | .createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
413 | } catch (OsmApiException e) {
|
---|
414 | if (e.getResponseCode() == HttpURLConnection.HTTP_GONE) {
|
---|
415 | warnBecauseOfDeletedRelation(r);
|
---|
416 | continue;
|
---|
417 | }
|
---|
418 | throw e;
|
---|
419 | }
|
---|
420 | mergeDataSet(dataSet);
|
---|
421 | refreshView(r);
|
---|
422 | }
|
---|
423 | SwingUtilities.invokeLater(MainApplication.getMap()::repaint);
|
---|
424 | } catch (OsmTransferException e) {
|
---|
425 | if (canceled) {
|
---|
426 | Logging.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
|
---|
427 | return;
|
---|
428 | }
|
---|
429 | lastException = e;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * The asynchronous task for downloading a set of relations
|
---|
436 | */
|
---|
437 | class DownloadRelationSetTask extends DownloadTask {
|
---|
438 | private final Set<Relation> relations;
|
---|
439 |
|
---|
440 | DownloadRelationSetTask(Dialog parent, Set<Relation> relations) {
|
---|
441 | super(tr("Download relation members"), parent);
|
---|
442 | this.relations = relations;
|
---|
443 | }
|
---|
444 |
|
---|
445 | protected void mergeDataSet(DataSet dataSet) {
|
---|
446 | if (dataSet != null) {
|
---|
447 | final DataSetMerger visitor = new DataSetMerger(getLayer().data, dataSet);
|
---|
448 | visitor.merge();
|
---|
449 | if (!visitor.getConflicts().isEmpty()) {
|
---|
450 | getLayer().getConflicts().add(visitor.getConflicts());
|
---|
451 | conflictsCount += visitor.getConflicts().size();
|
---|
452 | }
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | @Override
|
---|
457 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
458 | try {
|
---|
459 | Iterator<Relation> it = relations.iterator();
|
---|
460 | while (it.hasNext() && !canceled) {
|
---|
461 | Relation r = it.next();
|
---|
462 | if (r.isNew()) {
|
---|
463 | continue;
|
---|
464 | }
|
---|
465 | progressMonitor.setCustomText(tr("Downloading relation {0}", r.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
466 | OsmServerObjectReader reader = new OsmServerObjectReader(r.getId(), OsmPrimitiveType.RELATION,
|
---|
467 | true);
|
---|
468 | DataSet dataSet = reader.parseOsm(progressMonitor
|
---|
469 | .createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
470 | mergeDataSet(dataSet);
|
---|
471 | refreshView(r);
|
---|
472 | }
|
---|
473 | } catch (OsmTransferException e) {
|
---|
474 | if (canceled) {
|
---|
475 | Logging.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
|
---|
476 | return;
|
---|
477 | }
|
---|
478 | lastException = e;
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 | }
|
---|