| 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.ArrayList;
|
|---|
| 9 | import java.util.Iterator;
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.JLabel;
|
|---|
| 13 | import javax.swing.JPanel;
|
|---|
| 14 | import javax.swing.JScrollPane;
|
|---|
| 15 | import javax.swing.JTable;
|
|---|
| 16 | import javax.swing.table.DefaultTableModel;
|
|---|
| 17 |
|
|---|
| 18 | import org.openstreetmap.josm.data.APIDataSet;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.CyclicUploadDependencyException;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 21 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 22 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 23 | import org.openstreetmap.josm.gui.PrimitiveRenderer;
|
|---|
| 24 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
|---|
| 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(new BorderLayout());
|
|---|
| 43 | String msg = tr("<html>{0} relations build a cycle because they refer to each other.<br>"
|
|---|
| 44 | + "JOSM cannot upload them. Please edit the relations and remove the "
|
|---|
| 45 | + "cyclic dependency.</html>", dep.size()-1);
|
|---|
| 46 | pnl.add(new JLabel(msg), BorderLayout.NORTH);
|
|---|
| 47 |
|
|---|
| 48 | DefaultTableModel model = new DefaultTableModel();
|
|---|
| 49 | model.addColumn(tr("Relation ..."));
|
|---|
| 50 | model.addColumn(tr("... refers to relation"));
|
|---|
| 51 | for (int i = 0; i < dep.size()-1; i++) {
|
|---|
| 52 | Relation r1 = dep.get(i);
|
|---|
| 53 | Relation r2 = dep.get(i+1);
|
|---|
| 54 | model.addRow(new Relation[] {r1, r2});
|
|---|
| 55 | }
|
|---|
| 56 | JTable tbl = new JTable(model);
|
|---|
| 57 | PrimitiveRenderer renderer = new PrimitiveRenderer();
|
|---|
| 58 | tbl.getColumnModel().getColumn(0).setCellRenderer(renderer);
|
|---|
| 59 | tbl.getColumnModel().getColumn(1).setCellRenderer(renderer);
|
|---|
| 60 | pnl.add(new JScrollPane(tbl), BorderLayout.CENTER);
|
|---|
| 61 | return pnl;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Warns the user if a cyclic dependency is detected
|
|---|
| 66 | *
|
|---|
| 67 | * @param e the cyclic dependency exception
|
|---|
| 68 | */
|
|---|
| 69 | protected void warnCyclicUploadDependency(CyclicUploadDependencyException e) {
|
|---|
| 70 | List<Relation> dep = new ArrayList<>(e.getCyclicUploadDependency());
|
|---|
| 71 | Relation last = dep.get(dep.size() -1);
|
|---|
| 72 | Iterator<Relation> it = dep.iterator();
|
|---|
| 73 | while (it.hasNext()) {
|
|---|
| 74 | if (it.next() != last) {
|
|---|
| 75 | it.remove();
|
|---|
| 76 | } else {
|
|---|
| 77 | break;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | JPanel pnl = buildWarningPanel(dep);
|
|---|
| 81 | ExtendedDialog dialog = new ExtendedDialog(
|
|---|
| 82 | MainApplication.getMainFrame(),
|
|---|
| 83 | tr("Cycling dependencies"),
|
|---|
| 84 | tr("OK")
|
|---|
| 85 | );
|
|---|
| 86 | dialog.setContent(pnl, false /* don't embed in scroll pane */);
|
|---|
| 87 | dialog.setButtonIcons("ok");
|
|---|
| 88 | dialog.setRememberWindowGeometry(
|
|---|
| 89 | getClass().getName() + ".geometry",
|
|---|
| 90 | WindowGeometry.centerInWindow(MainApplication.getMainFrame(), new Dimension(300, 300))
|
|---|
| 91 | );
|
|---|
| 92 | dialog.showDialog();
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | @Override
|
|---|
| 96 | public boolean checkUpload(APIDataSet apiDataSet) {
|
|---|
| 97 | try {
|
|---|
| 98 | apiDataSet.adjustRelationUploadOrder();
|
|---|
| 99 | return true;
|
|---|
| 100 | } catch (CyclicUploadDependencyException e) {
|
|---|
| 101 | warnCyclicUploadDependency(e);
|
|---|
| 102 | return false;
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|