1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.InputEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.LinkedList;
|
---|
12 |
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JList;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
22 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
23 | import org.openstreetmap.josm.io.OsmServerWriter;
|
---|
24 | import org.openstreetmap.josm.tools.GBC;
|
---|
25 | import org.xml.sax.SAXException;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Action that opens a connection to the osm server and upload all changes.
|
---|
29 | *
|
---|
30 | * An dialog is displayed asking the user to specify a rectangle to grab.
|
---|
31 | * The url and account settings from the preferences are used.
|
---|
32 | *
|
---|
33 | * @author imi
|
---|
34 | */
|
---|
35 | public class UploadAction extends JosmAction {
|
---|
36 |
|
---|
37 | /** Upload Hook */
|
---|
38 | public interface UploadHook {
|
---|
39 | /**
|
---|
40 | * Checks the upload.
|
---|
41 | * @param add The added primitives
|
---|
42 | * @param update The updated primitives
|
---|
43 | * @param delete The deleted primitives
|
---|
44 | * @return true, if the upload can continue
|
---|
45 | */
|
---|
46 | public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * The list of upload hooks. These hooks will be called one after the other
|
---|
51 | * when the user wants to upload data. Plugins can insert their own hooks here
|
---|
52 | * if they want to be able to veto an upload.
|
---|
53 | *
|
---|
54 | * Be dafault, the standard upload dialog is the only element in the list.
|
---|
55 | * Plugins shold normally insert their code before that, so that the upload
|
---|
56 | * dialog is the last thing shown before upload really starts; on occasion
|
---|
57 | * however, a plugin might also want to insert something after that.
|
---|
58 | */
|
---|
59 | public final LinkedList<UploadHook> uploadHooks = new LinkedList<UploadHook>();
|
---|
60 |
|
---|
61 | public UploadAction() {
|
---|
62 | super(tr("Upload to OSM"), "upload", tr("Upload all changes to the OSM server."), KeyEvent.VK_U, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK, true);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Displays a screen where the actions that would be taken are displayed and
|
---|
66 | * give the user the possibility to cancel the upload.
|
---|
67 | */
|
---|
68 | uploadHooks.add(new UploadHook() {
|
---|
69 | public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
|
---|
70 |
|
---|
71 | JPanel p = new JPanel(new GridBagLayout());
|
---|
72 |
|
---|
73 | OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
|
---|
74 |
|
---|
75 | if (!add.isEmpty()) {
|
---|
76 | p.add(new JLabel(tr("Objects to add:")), GBC.eol());
|
---|
77 | JList l = new JList(add.toArray());
|
---|
78 | l.setCellRenderer(renderer);
|
---|
79 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
80 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (!update.isEmpty()) {
|
---|
84 | p.add(new JLabel(tr("Objects to modify:")), GBC.eol());
|
---|
85 | JList l = new JList(update.toArray());
|
---|
86 | l.setCellRenderer(renderer);
|
---|
87 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
88 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (!delete.isEmpty()) {
|
---|
92 | p.add(new JLabel(tr("Objects to delete:")), GBC.eol());
|
---|
93 | JList l = new JList(delete.toArray());
|
---|
94 | l.setCellRenderer(renderer);
|
---|
95 | l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
|
---|
96 | p.add(new JScrollPane(l), GBC.eol().fill());
|
---|
97 | }
|
---|
98 |
|
---|
99 | return JOptionPane.showConfirmDialog(Main.parent, p, tr("Upload these changes?"),
|
---|
100 | JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
|
---|
101 | }
|
---|
102 | });
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void actionPerformed(ActionEvent e) {
|
---|
106 | if (Main.map == null) {
|
---|
107 | JOptionPane.showMessageDialog(Main.parent,tr("Nothing to upload. Get some data first."));
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (!Main.map.conflictDialog.conflicts.isEmpty()) {
|
---|
112 | JOptionPane.showMessageDialog(Main.parent,tr("There are unresolved conflicts. You have to resolve these first."));
|
---|
113 | Main.map.conflictDialog.action.button.setSelected(true);
|
---|
114 | Main.map.conflictDialog.action.actionPerformed(null);
|
---|
115 | return;
|
---|
116 | }
|
---|
117 |
|
---|
118 | final LinkedList<OsmPrimitive> add = new LinkedList<OsmPrimitive>();
|
---|
119 | final LinkedList<OsmPrimitive> update = new LinkedList<OsmPrimitive>();
|
---|
120 | final LinkedList<OsmPrimitive> delete = new LinkedList<OsmPrimitive>();
|
---|
121 | for (OsmPrimitive osm : Main.ds.allPrimitives()) {
|
---|
122 | if (osm.get("josm/ignore") != null)
|
---|
123 | continue;
|
---|
124 | if (osm.id == 0 && !osm.deleted)
|
---|
125 | add.addLast(osm);
|
---|
126 | else if (osm.modified && !osm.deleted)
|
---|
127 | update.addLast(osm);
|
---|
128 | else if (osm.deleted && osm.id != 0)
|
---|
129 | delete.addFirst(osm);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
|
---|
133 | JOptionPane.showMessageDialog(Main.parent,tr("No changes to upload."));
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | // Call all upload hooks in sequence. The upload confirmation dialog
|
---|
138 | // is one of these.
|
---|
139 | for(UploadHook hook : uploadHooks)
|
---|
140 | if(!hook.checkUpload(add, update, delete))
|
---|
141 | return;
|
---|
142 |
|
---|
143 | final OsmServerWriter server = new OsmServerWriter();
|
---|
144 | final Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
|
---|
145 | all.addAll(add);
|
---|
146 | all.addAll(update);
|
---|
147 | all.addAll(delete);
|
---|
148 |
|
---|
149 | PleaseWaitRunnable uploadTask = new PleaseWaitRunnable(tr("Uploading data")){
|
---|
150 | @Override protected void realRun() throws SAXException {
|
---|
151 | server.uploadOsm(all);
|
---|
152 | }
|
---|
153 | @Override protected void finish() {
|
---|
154 | Main.main.editLayer().cleanData(server.processed, !add.isEmpty());
|
---|
155 | }
|
---|
156 | @Override protected void cancel() {
|
---|
157 | server.cancel();
|
---|
158 | }
|
---|
159 | };
|
---|
160 | Main.worker.execute(uploadTask);
|
---|
161 | }
|
---|
162 | }
|
---|