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.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.Track;
|
---|
21 | import org.openstreetmap.josm.gui.GBC;
|
---|
22 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
23 | import org.openstreetmap.josm.io.OsmServerWriter;
|
---|
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.", KeyEvent.VK_U,
|
---|
37 | KeyStroke.getAWTKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void actionPerformed(ActionEvent e) {
|
---|
41 | final Collection<OsmPrimitive> add = new LinkedList<OsmPrimitive>();
|
---|
42 | final Collection<OsmPrimitive> update = new LinkedList<OsmPrimitive>();
|
---|
43 | final Collection<OsmPrimitive> delete = new LinkedList<OsmPrimitive>();
|
---|
44 | boolean acceptedTracks = false;
|
---|
45 | for (OsmPrimitive osm : Main.main.ds.allPrimitives()) {
|
---|
46 | boolean doSomething = true;
|
---|
47 | if (osm.id == 0 && !osm.isDeleted())
|
---|
48 | add.add(osm);
|
---|
49 | else if ((osm.modified || osm.modifiedProperties) && !osm.isDeleted())
|
---|
50 | update.add(osm);
|
---|
51 | else if (osm.isDeleted() && osm.id != 0)
|
---|
52 | delete.add(osm);
|
---|
53 | else
|
---|
54 | doSomething = false;
|
---|
55 |
|
---|
56 | if (osm instanceof Track && doSomething && !acceptedTracks) {
|
---|
57 | int answer = JOptionPane.showConfirmDialog(Main.main,
|
---|
58 | "The server currently does not understand the concept of Tracks.\n" +
|
---|
59 | "All tracks will be ignored on upload. Continue anyway?",
|
---|
60 | "No Track support", JOptionPane.YES_NO_OPTION);
|
---|
61 | if (answer != JOptionPane.YES_OPTION)
|
---|
62 | return;
|
---|
63 | acceptedTracks = true;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (!displayUploadScreen(add, update, delete))
|
---|
68 | return;
|
---|
69 |
|
---|
70 | final OsmServerWriter server = new OsmServerWriter();
|
---|
71 | final Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
|
---|
72 | all.addAll(add);
|
---|
73 | all.addAll(update);
|
---|
74 | all.addAll(delete);
|
---|
75 |
|
---|
76 | new Thread(new PleaseWaitRunnable("Uploading data"){
|
---|
77 | @Override
|
---|
78 | public void realRun() {
|
---|
79 | try {
|
---|
80 | server.uploadOsm(all);
|
---|
81 | } catch (JDOMException x) {
|
---|
82 | closeDialog();
|
---|
83 | x.printStackTrace();
|
---|
84 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
85 | }
|
---|
86 | Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty());
|
---|
87 | }
|
---|
88 | }).start();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Displays a screen where the actions that would be taken are displayed and
|
---|
93 | * give the user the possibility to cancel the upload.
|
---|
94 | * @return <code>true</code>, if the upload should continue. <code>false</code>
|
---|
95 | * if the user requested cancel.
|
---|
96 | */
|
---|
97 | private boolean displayUploadScreen(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
|
---|
98 | if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
|
---|
99 | JOptionPane.showMessageDialog(Main.main, "No changes to upload.");
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 |
|
---|
103 | JPanel p = new JPanel(new GridBagLayout());
|
---|
104 |
|
---|
105 | OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
|
---|
106 |
|
---|
107 | if (!add.isEmpty()) {
|
---|
108 | p.add(new JLabel("Objects to add:"), GBC.eol());
|
---|
109 | JList l = new JList(add.toArray());
|
---|
110 | l.setCellRenderer(renderer);
|
---|
111 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
112 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (!update.isEmpty()) {
|
---|
116 | p.add(new JLabel("Objects to modify:"), GBC.eol());
|
---|
117 | JList l = new JList(update.toArray());
|
---|
118 | l.setCellRenderer(renderer);
|
---|
119 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
120 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (!delete.isEmpty()) {
|
---|
124 | p.add(new JLabel("Objects to delete:"), GBC.eol());
|
---|
125 | JList l = new JList(delete.toArray());
|
---|
126 | l.setCellRenderer(renderer);
|
---|
127 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
128 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
129 | }
|
---|
130 |
|
---|
131 | return JOptionPane.showConfirmDialog(Main.main, p, "Upload this changes?",
|
---|
132 | JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
|
---|
133 | }
|
---|
134 | }
|
---|