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