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

Last change on this file since 18743 was 18722, checked in by pieren, 15 years ago

Improve autosourcing. Add history.

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
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;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.actions.upload.UploadHook;
18import org.openstreetmap.josm.command.ChangePropertyCommand;
19import org.openstreetmap.josm.data.APIDataSet;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.Way;
23import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
24import org.openstreetmap.josm.tools.GBC;
25
26/**
27 * This hook is called at JOSM upload and will check if new nodes and ways provide
28 * a tag "source=". If not and if auto-sourcing is enabled, it will add
29 * automatically a tag "source"="Cadastre..." as defined in the plugin preferences.
30 */
31public class CheckSourceUploadHook implements UploadHook
32{
33 /** Serializable ID */
34 private static final long serialVersionUID = -1;
35
36 /**
37 * Add the tag "source" if it doesn't exist for all new Nodes and Ways before uploading
38 */
39 public boolean checkUpload(APIDataSet apiDataSet)
40 {
41 if (CadastrePlugin.autoSourcing && CadastrePlugin.pluginUsed && !apiDataSet.getPrimitivesToAdd().isEmpty()) {
42 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
43 for (OsmPrimitive osm : apiDataSet.getPrimitivesToAdd()) {
44 if ((osm instanceof Way && (osm.getKeys().size() == 0 || !tagSourceExist(osm)))
45 || (osm instanceof Node && osm.getKeys().size() > 0 && !tagSourceExist(osm))) {
46 sel.add(osm);
47 }
48 }
49 if (!sel.isEmpty()) {
50 displaySource(sel);
51 }
52 }
53 return true;
54 }
55
56 /**
57 * Check whenever one of the keys of the object is "source"
58 * @param OsmPrimitive
59 * @return true if one of keys is "source"
60 */
61 private boolean tagSourceExist(OsmPrimitive osm) {
62 for (String key : osm.keySet()) {
63 if (key.equals("source") ) {
64 return true;
65 }
66 }
67 return false;
68 }
69
70 /**
71 * Displays a screen with the list of objects which will be tagged with
72 * source="cadastre.." if it is approved.
73 * @param sel the list of elements added without a key "source"
74 */
75 private void displaySource(Collection<OsmPrimitive> sel)
76 {
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 boolean 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
91 }
92}
Note: See TracBrowser for help on using the repository browser.