source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CheckSourceUploadHook.java@ 33514

Last change on this file since 33514 was 32556, checked in by donvip, 9 years ago

checkstyle

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package cadastre_fr;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.util.Collection;
8import java.util.HashSet;
9
10import javax.swing.JLabel;
11import javax.swing.JList;
12import javax.swing.JOptionPane;
13import javax.swing.JPanel;
14import javax.swing.JScrollPane;
15import javax.swing.JTextField;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.upload.UploadHook;
19import org.openstreetmap.josm.command.ChangePropertyCommand;
20import org.openstreetmap.josm.data.APIDataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
25import 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 */
32public class CheckSourceUploadHook implements UploadHook {
33
34 /**
35 * Add the tag "source" if it doesn't exist for all new Nodes and Ways before uploading
36 */
37 @Override
38 public boolean checkUpload(APIDataSet apiDataSet) {
39 if (CadastrePlugin.autoSourcing && CadastrePlugin.pluginUsed && !apiDataSet.getPrimitivesToAdd().isEmpty()) {
40 Collection<OsmPrimitive> sel = new HashSet<>();
41 for (OsmPrimitive osm : apiDataSet.getPrimitivesToAdd()) {
42 if ((osm instanceof Way && (osm.getKeys().size() == 0 || !tagSourceExist(osm)))
43 || (osm instanceof Node && osm.getKeys().size() > 0 && !tagSourceExist(osm))) {
44 sel.add(osm);
45 }
46 }
47 if (!sel.isEmpty()) {
48 displaySource(sel);
49 }
50 }
51 return true;
52 }
53
54 /**
55 * Check whenever one of the keys of the object is "source"
56 * @return true if one of keys is "source"
57 */
58 private boolean tagSourceExist(OsmPrimitive osm) {
59 for (String key : osm.keySet()) {
60 if (key.equals("source")) {
61 return true;
62 }
63 }
64 return false;
65 }
66
67 /**
68 * Displays a screen with the list of objects which will be tagged with
69 * source="cadastre.." if it is approved.
70 * @param sel the list of elements added without a key "source"
71 */
72 private void displaySource(Collection<OsmPrimitive> sel) {
73 if (!sel.isEmpty()) {
74 JPanel p = new JPanel(new GridBagLayout());
75 OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
76 p.add(new JLabel(tr("Add \"source=...\" to elements?")), GBC.eol());
77 JTextField tf = new JTextField(CadastrePlugin.source);
78 p.add(tf, GBC.eol());
79 JList<OsmPrimitive> l = new JList<>(sel.toArray(new OsmPrimitive[0]));
80 l.setCellRenderer(renderer);
81 l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
82 p.add(new JScrollPane(l), GBC.eol().fill());
83 boolean bContinue = JOptionPane.showConfirmDialog(Main.parent, p, tr("Add \"source=...\" to elements?"),
84 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
85 if (bContinue)
86 Main.main.undoRedo.add(new ChangePropertyCommand(sel, "source", tf.getText()));
87 }
88 }
89}
Note: See TracBrowser for help on using the repository browser.