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