| 1 | package cadastre_fr;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.GridBagLayout;
|
|---|
| 6 | import java.util.Collection;
|
|---|
| 7 | import java.util.HashSet;
|
|---|
| 8 |
|
|---|
| 9 | import javax.swing.JLabel;
|
|---|
| 10 | import javax.swing.JList;
|
|---|
| 11 | import javax.swing.JOptionPane;
|
|---|
| 12 | import javax.swing.JPanel;
|
|---|
| 13 | import javax.swing.JScrollPane;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.actions.UploadAction.UploadHook;
|
|---|
| 17 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 21 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
|---|
| 22 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * This hook is called at JOSM upload and will check if new nodes and ways provide
|
|---|
| 26 | * a tag "source=". If not and if auto-sourcing is enabled, it will add
|
|---|
| 27 | * automatically a tag "source"="Cadastre..." as defined in the plugin preferences.
|
|---|
| 28 | */
|
|---|
| 29 | public class CheckSourceUploadHook implements UploadHook
|
|---|
| 30 | {
|
|---|
| 31 | /** Serializable ID */
|
|---|
| 32 | private static final long serialVersionUID = -1;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Add the tag "source" if it doesn't exist for all new Nodes and Ways before uploading
|
|---|
| 36 | */
|
|---|
| 37 | public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete)
|
|---|
| 38 | {
|
|---|
| 39 | if (CadastrePlugin.autoSourcing && CadastrePlugin.pluginUsed && !add.isEmpty()) {
|
|---|
| 40 | Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
|---|
| 41 | for (OsmPrimitive osm : add) {
|
|---|
| 42 | if ((osm instanceof Node || osm instanceof Way)
|
|---|
| 43 | && (osm.keys == null || !tagSourceExist(osm))) {
|
|---|
| 44 | sel.add(osm);
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | if (!sel.isEmpty()) {
|
|---|
| 48 | return displaySource(sel);
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | return true;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Check whenever one of the keys of the object is "source"
|
|---|
| 56 | * @param OsmPrimitive
|
|---|
| 57 | * @return true if one of keys is "source"
|
|---|
| 58 | */
|
|---|
| 59 | private boolean tagSourceExist(OsmPrimitive osm) {
|
|---|
| 60 | for (String key : osm.keys.keySet()) {
|
|---|
| 61 | if (key.equals("source") ) {
|
|---|
| 62 | return true;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | return false;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Displays a screen with the list of objects which will be tagged with
|
|---|
| 70 | * source="cadastre.." if it is approved.
|
|---|
| 71 | * @param sel the list of elements added without a key "source"
|
|---|
| 72 | * @return true if it is accepted by user
|
|---|
| 73 | */
|
|---|
| 74 | private boolean displaySource(Collection<OsmPrimitive> sel)
|
|---|
| 75 | {
|
|---|
| 76 | boolean bContinue = true;
|
|---|
| 77 | if (!sel.isEmpty()) {
|
|---|
| 78 | JPanel p = new JPanel(new GridBagLayout());
|
|---|
| 79 | OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
|
|---|
| 80 | p.add(new JLabel(tr("Auto-tag source added:")), GBC.eol());
|
|---|
| 81 | JList l = new JList(sel.toArray());
|
|---|
| 82 | l.setCellRenderer(renderer);
|
|---|
| 83 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
|---|
| 84 | p.add(new JScrollPane(l), GBC.eol().fill());
|
|---|
| 85 | bContinue = JOptionPane.showConfirmDialog(Main.parent, p, tr("Add \"source=...\" to elements?"),
|
|---|
| 86 | JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
|
|---|
| 87 | if (bContinue)
|
|---|
| 88 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, "source", CadastrePlugin.source));
|
|---|
| 89 | }
|
|---|
| 90 | return bContinue;
|
|---|
| 91 |
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|