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