1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.upload;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.util.Iterator;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.JLabel;
|
---|
10 | import javax.swing.JPanel;
|
---|
11 | import javax.swing.JScrollPane;
|
---|
12 | import javax.swing.JTable;
|
---|
13 | import javax.swing.table.DefaultTableModel;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
19 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
20 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
21 |
|
---|
22 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.actions.upload.UploadHook;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This upload hook reorders the list of new relations to upload such that child
|
---|
28 | * relations are uploaded before parent relations. It also checks for cyclic
|
---|
29 | * dependencies in the list of new relations.
|
---|
30 | *
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class RelationUploadOrderHook implements UploadHook {
|
---|
34 |
|
---|
35 | /** the data to be analyzed */
|
---|
36 | private APIDataSet data;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * builds the panel which warns users about a cyclic dependency
|
---|
40 | *
|
---|
41 | * @param dep the list of relations with a cyclic dependency
|
---|
42 | * @return the panel
|
---|
43 | */
|
---|
44 | protected JPanel buildWarningPanel(List<Relation> dep) {
|
---|
45 | JPanel pnl = new JPanel();
|
---|
46 | pnl.setLayout(new BorderLayout());
|
---|
47 | String msg = tr("<html>{0} relations build a cycle because they refer to each other.<br>"
|
---|
48 | + "JOSM can''t upload them. Please edit the relations and remove the "
|
---|
49 | + "cyclic dependency.</html>", dep.size()-1);
|
---|
50 | pnl.add(new JLabel(msg), BorderLayout.NORTH);
|
---|
51 |
|
---|
52 | DefaultTableModel model = new DefaultTableModel();
|
---|
53 | model.addColumn(tr("Relation ..."));
|
---|
54 | model.addColumn(tr("... refers to relation"));
|
---|
55 | for (int i=0; i<dep.size()-1;i++) {
|
---|
56 | Relation r1 = dep.get(i);
|
---|
57 | Relation r2 = dep.get(i+1);
|
---|
58 | model.addRow(new Relation[] {r1,r2});
|
---|
59 | }
|
---|
60 | JTable tbl = new JTable(model);
|
---|
61 | OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
|
---|
62 | tbl.getColumnModel().getColumn(0).setCellRenderer(renderer);
|
---|
63 | tbl.getColumnModel().getColumn(1).setCellRenderer(renderer);
|
---|
64 | pnl.add(new JScrollPane(tbl), BorderLayout.CENTER);
|
---|
65 | return pnl;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Warns the user if a cyclic dependency is detected
|
---|
70 | *
|
---|
71 | * @param e the cyclic dependency exception
|
---|
72 | */
|
---|
73 | protected void warnCyclicUploadDependency(CyclicUploadDependencyException e) {
|
---|
74 | List<Relation> dep = e.getCyclicUploadDependency();
|
---|
75 | Relation last = dep.get(dep.size() -1);
|
---|
76 | Iterator<Relation> it = dep.iterator();
|
---|
77 | while(it.hasNext()) {
|
---|
78 | if (it.next() != last) {
|
---|
79 | it.remove();
|
---|
80 | } else {
|
---|
81 | break;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | JPanel pnl = buildWarningPanel(dep);
|
---|
85 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
86 | Main.parent,
|
---|
87 | tr("Cycling dependencies"),
|
---|
88 | new String[] {tr("OK")}
|
---|
89 | );
|
---|
90 | dialog.setContent(pnl, false /* don't embed in scroll pane */);
|
---|
91 | dialog.setButtonIcons(new String[] {"ok"});
|
---|
92 | dialog.setRememberWindowGeometry(
|
---|
93 | getClass().getName() + ".geometry",
|
---|
94 | WindowGeometry.centerInWindow(Main.parent, new Dimension(300, 300))
|
---|
95 | );
|
---|
96 | dialog.showDialog();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public boolean checkUpload(APIDataSet apiDataSet) {
|
---|
100 | this.data = apiDataSet;
|
---|
101 | try {
|
---|
102 | data.adjustRelationUploadOrder();
|
---|
103 | return true;
|
---|
104 | } catch(CyclicUploadDependencyException e) {
|
---|
105 | warnCyclicUploadDependency(e);
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|