1 | Index: src/org/openstreetmap/josm/actions/ApiPreconditionChecker.java
|
---|
2 | ===================================================================
|
---|
3 | --- src/org/openstreetmap/josm/actions/ApiPreconditionChecker.java (revision 0)
|
---|
4 | +++ src/org/openstreetmap/josm/actions/ApiPreconditionChecker.java (revision 0)
|
---|
5 | @@ -0,0 +1,78 @@
|
---|
6 | +// License: GPL. For details, see LICENSE file.
|
---|
7 | +package org.openstreetmap.josm.actions;
|
---|
8 | +
|
---|
9 | +import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
10 | +
|
---|
11 | +import java.util.Collection;
|
---|
12 | +import java.util.LinkedList;
|
---|
13 | +import java.util.List;
|
---|
14 | +import java.util.Map.Entry;
|
---|
15 | +
|
---|
16 | +import javax.swing.JOptionPane;
|
---|
17 | +
|
---|
18 | +import org.openstreetmap.josm.Main;
|
---|
19 | +import org.openstreetmap.josm.actions.UploadAction.UploadHook;
|
---|
20 | +import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | +import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | +import org.openstreetmap.josm.io.OsmApi;
|
---|
23 | +import org.openstreetmap.josm.io.OsmApiInitializationException;
|
---|
24 | +
|
---|
25 | +public class ApiPreconditionChecker implements UploadHook {
|
---|
26 | +
|
---|
27 | + public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update,
|
---|
28 | + Collection<OsmPrimitive> delete) {
|
---|
29 | + OsmApi api = OsmApi.getOsmApi();
|
---|
30 | + try {
|
---|
31 | + api.initialize();
|
---|
32 | + long maxNodes = api.capabilities.getLong("waynodes","maximum");
|
---|
33 | +
|
---|
34 | + if( !checkMaxNodes(add, maxNodes))
|
---|
35 | + return false;
|
---|
36 | + if( !checkMaxNodes(update, maxNodes))
|
---|
37 | + return false;
|
---|
38 | + if( !checkMaxNodes(delete, maxNodes))
|
---|
39 | + return false;
|
---|
40 | +
|
---|
41 | + long maxElements = api.capabilities.getLong("changesets", "maximum_elements");
|
---|
42 | + if(add.size() + update.size() + delete.size() > maxElements) {
|
---|
43 | + JOptionPane.showMessageDialog(Main.parent,tr("Maximum number of changes exceeded ({0}).", maxElements));
|
---|
44 | +
|
---|
45 | + return false;
|
---|
46 | + }
|
---|
47 | + } catch (OsmApiInitializationException e) {
|
---|
48 | + e.printStackTrace();
|
---|
49 | + JOptionPane.showMessageDialog(Main.parent,tr("Network error. Unable to get server capabilities."));
|
---|
50 | +
|
---|
51 | + return false;
|
---|
52 | + }
|
---|
53 | +
|
---|
54 | + return true;
|
---|
55 | + }
|
---|
56 | +
|
---|
57 | + private boolean checkMaxNodes(Collection<OsmPrimitive> add, long maxNodes) {
|
---|
58 | + for (OsmPrimitive osmPrimitive : add) {
|
---|
59 | + for (Entry<String,String> e : osmPrimitive.keys.entrySet()) {
|
---|
60 | + if(e.getValue().length() > 255) {
|
---|
61 | + JOptionPane.showMessageDialog(Main.parent,tr("Id {0} tag {1} is too long.", osmPrimitive.id, e.getKey()));
|
---|
62 | + List<OsmPrimitive> newNodes = new LinkedList<OsmPrimitive>();
|
---|
63 | + newNodes.add(osmPrimitive);
|
---|
64 | +
|
---|
65 | + Main.ds.setSelected(newNodes);
|
---|
66 | + return false;
|
---|
67 | + }
|
---|
68 | + }
|
---|
69 | +
|
---|
70 | + if (osmPrimitive instanceof Way &&
|
---|
71 | + ((Way)osmPrimitive).nodes.size() > maxNodes) {
|
---|
72 | + JOptionPane.showMessageDialog(Main.parent,tr("Way id {1} exceeds maximum way nodes ({0}).", maxNodes, osmPrimitive.id));
|
---|
73 | + List<OsmPrimitive> newNodes = new LinkedList<OsmPrimitive>();
|
---|
74 | + newNodes.add(osmPrimitive);
|
---|
75 | +
|
---|
76 | + Main.ds.setSelected(newNodes);
|
---|
77 | + return false;
|
---|
78 | + }
|
---|
79 | + }
|
---|
80 | + return true;
|
---|
81 | + }
|
---|
82 | +
|
---|
83 | +}
|
---|
84 | Index: src/org/openstreetmap/josm/actions/UploadAction.java
|
---|
85 | ===================================================================
|
---|
86 | --- src/org/openstreetmap/josm/actions/UploadAction.java (revision 1690)
|
---|
87 | +++ src/org/openstreetmap/josm/actions/UploadAction.java (working copy)
|
---|
88 | @@ -80,6 +80,11 @@
|
---|
89 | Shortcut.registerShortcut("file:upload", tr("File: {0}", tr("Upload to OSM...")), KeyEvent.VK_U, Shortcut.GROUPS_ALT1+Shortcut.GROUP_HOTKEY), true);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | + * Checks server capabilities before upload.
|
---|
93 | + */
|
---|
94 | + uploadHooks.add(new ApiPreconditionChecker());
|
---|
95 | +
|
---|
96 | + /**
|
---|
97 | * Displays a screen where the actions that would be taken are displayed and
|
---|
98 | * give the user the possibility to cancel the upload.
|
---|
99 | */
|
---|