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