1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.GridBagLayout;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.InputEvent;
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.LinkedList;
|
---|
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.KeyStroke;
|
---|
16 |
|
---|
17 | import org.jdom.JDOMException;
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.Preferences.PreferencesException;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
22 | import org.openstreetmap.josm.io.OsmServerWriter;
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Action that opens a connection to the osm server and upload all changes.
|
---|
27 | *
|
---|
28 | * An dialog is displayed asking the user to specify a rectangle to grab.
|
---|
29 | * The url and account settings from the preferences are used.
|
---|
30 | *
|
---|
31 | * @author imi
|
---|
32 | */
|
---|
33 | public class UploadAction extends JosmAction {
|
---|
34 |
|
---|
35 | public UploadAction() {
|
---|
36 | super("Upload to OSM", "upload", "Upload all changes to the OSM server.", "Ctrl-Shift-U",
|
---|
37 | KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void actionPerformed(ActionEvent e) {
|
---|
41 |
|
---|
42 | //TODO: Remove this in later versions (temporary only)
|
---|
43 | if (Main.pref.osmDataServer.endsWith("/0.2") || Main.pref.osmDataServer.endsWith("/0.2/")) {
|
---|
44 | int answer = JOptionPane.showConfirmDialog(Main.main,
|
---|
45 | "You seem to have an outdated server entry in your preferences.\n" +
|
---|
46 | "\n" +
|
---|
47 | "As of JOSM Release 1.2, you must no longer specify the API version in\n" +
|
---|
48 | "the osm url. For the OSM standard server, use http://www.openstreetmap.org/api" +
|
---|
49 | "\n" +
|
---|
50 | "Fix settings and continue?", "Outdated server url detected.", JOptionPane.YES_NO_OPTION);
|
---|
51 | if (answer != JOptionPane.YES_OPTION)
|
---|
52 | return;
|
---|
53 | int cutPos = Main.pref.osmDataServer.endsWith("/0.2") ? 4 : 5;
|
---|
54 | Main.pref.osmDataServer = Main.pref.osmDataServer.substring(0, Main.pref.osmDataServer.length()-cutPos);
|
---|
55 | try {
|
---|
56 | Main.pref.save();
|
---|
57 | } catch (PreferencesException x) {
|
---|
58 | x.printStackTrace();
|
---|
59 | JOptionPane.showMessageDialog(Main.main, "Could not save the preferences chane:\n" +
|
---|
60 | x.getMessage());
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | final Collection<OsmPrimitive> add = new LinkedList<OsmPrimitive>();
|
---|
65 | final Collection<OsmPrimitive> update = new LinkedList<OsmPrimitive>();
|
---|
66 | final Collection<OsmPrimitive> delete = new LinkedList<OsmPrimitive>();
|
---|
67 | for (OsmPrimitive osm : Main.main.ds.allPrimitives()) {
|
---|
68 | if (osm.id == 0 && !osm.isDeleted())
|
---|
69 | add.add(osm);
|
---|
70 | else if ((osm.modified || osm.modifiedProperties) && !osm.isDeleted())
|
---|
71 | update.add(osm);
|
---|
72 | else if (osm.isDeleted() && osm.id != 0)
|
---|
73 | delete.add(osm);
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (!displayUploadScreen(add, update, delete))
|
---|
77 | return;
|
---|
78 |
|
---|
79 | final OsmServerWriter server = new OsmServerWriter();
|
---|
80 | final Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
|
---|
81 | all.addAll(add);
|
---|
82 | all.addAll(update);
|
---|
83 | all.addAll(delete);
|
---|
84 |
|
---|
85 | new Thread(new PleaseWaitRunnable("Uploading data"){
|
---|
86 | @Override
|
---|
87 | public void realRun() {
|
---|
88 | try {
|
---|
89 | server.uploadOsm(all);
|
---|
90 | } catch (JDOMException x) {
|
---|
91 | closeDialog();
|
---|
92 | x.printStackTrace();
|
---|
93 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
94 | }
|
---|
95 | Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty());
|
---|
96 | }
|
---|
97 | }).start();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Displays a screen where the actions that would be taken are displayed and
|
---|
102 | * give the user the possibility to cancel the upload.
|
---|
103 | * @return <code>true</code>, if the upload should continue. <code>false</code>
|
---|
104 | * if the user requested cancel.
|
---|
105 | */
|
---|
106 | private boolean displayUploadScreen(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
|
---|
107 | if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
|
---|
108 | JOptionPane.showMessageDialog(Main.main, "No changes to upload.");
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 |
|
---|
112 | JPanel p = new JPanel(new GridBagLayout());
|
---|
113 |
|
---|
114 | OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
|
---|
115 |
|
---|
116 | if (!add.isEmpty()) {
|
---|
117 | p.add(new JLabel("Objects to add:"), GBC.eol());
|
---|
118 | JList l = new JList(add.toArray());
|
---|
119 | l.setCellRenderer(renderer);
|
---|
120 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
121 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (!update.isEmpty()) {
|
---|
125 | p.add(new JLabel("Objects to modify:"), GBC.eol());
|
---|
126 | JList l = new JList(update.toArray());
|
---|
127 | l.setCellRenderer(renderer);
|
---|
128 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
129 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (!delete.isEmpty()) {
|
---|
133 | p.add(new JLabel("Objects to delete:"), GBC.eol());
|
---|
134 | JList l = new JList(delete.toArray());
|
---|
135 | l.setCellRenderer(renderer);
|
---|
136 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
137 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
138 | }
|
---|
139 |
|
---|
140 | return JOptionPane.showConfirmDialog(Main.main, p, "Upload this changes?",
|
---|
141 | JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
|
---|
142 | }
|
---|
143 | }
|
---|