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.gui.GBC;
|
---|
21 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
22 | import org.openstreetmap.josm.io.OsmServerWriter;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Action that opens a connection to the osm server and upload all changes.
|
---|
26 | *
|
---|
27 | * An dialog is displayed asking the user to specify a rectangle to grab.
|
---|
28 | * The url and account settings from the preferences are used.
|
---|
29 | *
|
---|
30 | * @author imi
|
---|
31 | */
|
---|
32 | public class UploadAction extends JosmAction {
|
---|
33 |
|
---|
34 | public UploadAction() {
|
---|
35 | super("Upload to OSM", "upload", "Upload all changes to the OSM server.", KeyEvent.VK_U,
|
---|
36 | KeyStroke.getAWTKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void actionPerformed(ActionEvent e) {
|
---|
40 | Collection<OsmPrimitive> add = new LinkedList<OsmPrimitive>();
|
---|
41 | Collection<OsmPrimitive> update = new LinkedList<OsmPrimitive>();
|
---|
42 | Collection<OsmPrimitive> delete = new LinkedList<OsmPrimitive>();
|
---|
43 | for (OsmPrimitive osm : Main.main.ds.nodes)
|
---|
44 | if (osm.id == 0)
|
---|
45 | add.add(osm);
|
---|
46 | else if (osm.modified)
|
---|
47 | update.add(osm);
|
---|
48 | for (OsmPrimitive osm : Main.main.ds.lineSegments)
|
---|
49 | if (osm.id == 0)
|
---|
50 | add.add(osm);
|
---|
51 | else if (osm.modified)
|
---|
52 | update.add(osm);
|
---|
53 | for (OsmPrimitive osm : Main.main.ds.tracks)
|
---|
54 | if (osm.id == 0)
|
---|
55 | add.add(osm);
|
---|
56 | else if (osm.modified)
|
---|
57 | update.add(osm);
|
---|
58 | for (OsmPrimitive osm : Main.main.ds.deleted)
|
---|
59 | if (osm.id != 0)
|
---|
60 | delete.add(osm);
|
---|
61 |
|
---|
62 | if (!displayUploadScreen(add, update, delete))
|
---|
63 | return;
|
---|
64 |
|
---|
65 | OsmServerWriter server = new OsmServerWriter();
|
---|
66 | try {
|
---|
67 | Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
|
---|
68 | all.addAll(add);
|
---|
69 | all.addAll(update);
|
---|
70 | all.addAll(delete);
|
---|
71 | server.uploadOsm(all);
|
---|
72 | } catch (JDOMException x) {
|
---|
73 | x.printStackTrace();
|
---|
74 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Displays a screen where the actions that would be taken are displayed and
|
---|
80 | * give the user the possibility to cancel the upload.
|
---|
81 | * @return <code>true</code>, if the upload should continue. <code>false</code>
|
---|
82 | * if the user requested cancel.
|
---|
83 | */
|
---|
84 | private boolean displayUploadScreen(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
|
---|
85 | if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
|
---|
86 | JOptionPane.showMessageDialog(Main.main, "No changes to upload.");
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | JPanel p = new JPanel(new GridBagLayout());
|
---|
91 |
|
---|
92 | OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
|
---|
93 |
|
---|
94 | if (!add.isEmpty()) {
|
---|
95 | p.add(new JLabel("Objects to add:"), GBC.eol());
|
---|
96 | JList l = new JList(add.toArray());
|
---|
97 | l.setCellRenderer(renderer);
|
---|
98 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
99 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (!update.isEmpty()) {
|
---|
103 | p.add(new JLabel("Objects to modify:"), GBC.eol());
|
---|
104 | JList l = new JList(update.toArray());
|
---|
105 | l.setCellRenderer(renderer);
|
---|
106 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
107 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (!delete.isEmpty()) {
|
---|
111 | p.add(new JLabel("Objects to delete:"), GBC.eol());
|
---|
112 | JList l = new JList(delete.toArray());
|
---|
113 | l.setCellRenderer(renderer);
|
---|
114 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
115 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
116 | }
|
---|
117 |
|
---|
118 | return JOptionPane.showConfirmDialog(Main.main, p, "Upload this changes?",
|
---|
119 | JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
|
---|
120 | }
|
---|
121 | }
|
---|