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

Last change on this file since 13382 was 13382, checked in by pieren, 16 years ago

First commit of the french land registry WMS plugin for JOSM.

File size: 3.4 KB
Line 
1package cadastre_fr;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.GridBagLayout;
6import java.util.Collection;
7import java.util.HashSet;
8
9import javax.swing.JLabel;
10import javax.swing.JList;
11import javax.swing.JOptionPane;
12import javax.swing.JPanel;
13import javax.swing.JScrollPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.actions.UploadAction.UploadHook;
17import org.openstreetmap.josm.command.ChangePropertyCommand;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
22import 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 */
29public 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=Cadastre...\" to ?"),
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}
Note: See TracBrowser for help on using the repository browser.