| 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 | /**
|
|---|
| 36 | * builds the panel which warns users about a cyclic dependency
|
|---|
| 37 | *
|
|---|
| 38 | * @param dep the list of relations with a cyclic dependency
|
|---|
| 39 | * @return the panel
|
|---|
| 40 | */
|
|---|
| 41 | protected JPanel buildWarningPanel(List<Relation> dep) {
|
|---|
| 42 | JPanel pnl = new JPanel();
|
|---|
| 43 | pnl.setLayout(new BorderLayout());
|
|---|
| 44 | String msg = tr("<html>{0} relations build a cycle because they refer to each other.<br>"
|
|---|
| 45 | + "JOSM cannot upload them. Please edit the relations and remove the "
|
|---|
| 46 | + "cyclic dependency.</html>", dep.size()-1);
|
|---|
| 47 | pnl.add(new JLabel(msg), BorderLayout.NORTH);
|
|---|
| 48 |
|
|---|
| 49 | DefaultTableModel model = new DefaultTableModel();
|
|---|
| 50 | model.addColumn(tr("Relation ..."));
|
|---|
| 51 | model.addColumn(tr("... refers to relation"));
|
|---|
| 52 | for (int i=0; i<dep.size()-1;i++) {
|
|---|
| 53 | Relation r1 = dep.get(i);
|
|---|
| 54 | Relation r2 = dep.get(i+1);
|
|---|
| 55 | model.addRow(new Relation[] {r1,r2});
|
|---|
| 56 | }
|
|---|
| 57 | JTable tbl = new JTable(model);
|
|---|
| 58 | OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
|
|---|
| 59 | tbl.getColumnModel().getColumn(0).setCellRenderer(renderer);
|
|---|
| 60 | tbl.getColumnModel().getColumn(1).setCellRenderer(renderer);
|
|---|
| 61 | pnl.add(new JScrollPane(tbl), BorderLayout.CENTER);
|
|---|
| 62 | return pnl;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Warns the user if a cyclic dependency is detected
|
|---|
| 67 | *
|
|---|
| 68 | * @param e the cyclic dependency exception
|
|---|
| 69 | */
|
|---|
| 70 | protected void warnCyclicUploadDependency(CyclicUploadDependencyException e) {
|
|---|
| 71 | List<Relation> dep = e.getCyclicUploadDependency();
|
|---|
| 72 | Relation last = dep.get(dep.size() -1);
|
|---|
| 73 | Iterator<Relation> it = dep.iterator();
|
|---|
| 74 | while(it.hasNext()) {
|
|---|
| 75 | if (it.next() != last) {
|
|---|
| 76 | it.remove();
|
|---|
| 77 | } else {
|
|---|
| 78 | break;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | JPanel pnl = buildWarningPanel(dep);
|
|---|
| 82 | ExtendedDialog dialog = new ExtendedDialog(
|
|---|
| 83 | Main.parent,
|
|---|
| 84 | tr("Cycling dependencies"),
|
|---|
| 85 | new String[] {tr("OK")}
|
|---|
| 86 | );
|
|---|
| 87 | dialog.setContent(pnl, false /* don't embed in scroll pane */);
|
|---|
| 88 | dialog.setButtonIcons(new String[] {"ok"});
|
|---|
| 89 | dialog.setRememberWindowGeometry(
|
|---|
| 90 | getClass().getName() + ".geometry",
|
|---|
| 91 | WindowGeometry.centerInWindow(Main.parent, new Dimension(300, 300))
|
|---|
| 92 | );
|
|---|
| 93 | dialog.showDialog();
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | @Override
|
|---|
| 97 | public boolean checkUpload(APIDataSet apiDataSet) {
|
|---|
| 98 | try {
|
|---|
| 99 | apiDataSet.adjustRelationUploadOrder();
|
|---|
| 100 | return true;
|
|---|
| 101 | } catch(CyclicUploadDependencyException e) {
|
|---|
| 102 | warnCyclicUploadDependency(e);
|
|---|
| 103 | return false;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|