source: josm/trunk/src/org/openstreetmap/josm/actions/UploadAction.java@ 9688

Last change on this file since 9688 was 9514, checked in by simon04, 8 years ago

fix #4003 fix #7004 fix #8149 - Improve changeset tags handling (from earlier changeset, from dataset)

  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.LinkedList;
10import java.util.List;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.upload.ApiPreconditionCheckerHook;
16import org.openstreetmap.josm.actions.upload.DiscardTagsHook;
17import org.openstreetmap.josm.actions.upload.FixDataHook;
18import org.openstreetmap.josm.actions.upload.RelationUploadOrderHook;
19import org.openstreetmap.josm.actions.upload.UploadHook;
20import org.openstreetmap.josm.actions.upload.ValidateUploadHook;
21import org.openstreetmap.josm.data.APIDataSet;
22import org.openstreetmap.josm.data.conflict.ConflictCollection;
23import org.openstreetmap.josm.gui.HelpAwareOptionPane;
24import org.openstreetmap.josm.gui.help.HelpUtil;
25import org.openstreetmap.josm.gui.io.UploadDialog;
26import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
27import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
28import org.openstreetmap.josm.gui.layer.OsmDataLayer;
29import org.openstreetmap.josm.gui.util.GuiHelper;
30import org.openstreetmap.josm.tools.ImageProvider;
31import org.openstreetmap.josm.tools.Shortcut;
32
33/**
34 * Action that opens a connection to the osm server and uploads all changes.
35 *
36 * An dialog is displayed asking the user to specify a rectangle to grab.
37 * The url and account settings from the preferences are used.
38 *
39 * If the upload fails this action offers various options to resolve conflicts.
40 *
41 * @author imi
42 */
43public class UploadAction extends JosmAction {
44 /**
45 * The list of upload hooks. These hooks will be called one after the other
46 * when the user wants to upload data. Plugins can insert their own hooks here
47 * if they want to be able to veto an upload.
48 *
49 * Be default, the standard upload dialog is the only element in the list.
50 * Plugins should normally insert their code before that, so that the upload
51 * dialog is the last thing shown before upload really starts; on occasion
52 * however, a plugin might also want to insert something after that.
53 */
54 private static final List<UploadHook> uploadHooks = new LinkedList<>();
55 private static final List<UploadHook> lateUploadHooks = new LinkedList<>();
56
57 static {
58 /**
59 * Calls validator before upload.
60 */
61 uploadHooks.add(new ValidateUploadHook());
62
63 /**
64 * Fixes database errors
65 */
66 uploadHooks.add(new FixDataHook());
67
68 /**
69 * Checks server capabilities before upload.
70 */
71 uploadHooks.add(new ApiPreconditionCheckerHook());
72
73 /**
74 * Adjusts the upload order of new relations
75 */
76 uploadHooks.add(new RelationUploadOrderHook());
77
78 /**
79 * Removes discardable tags like created_by on modified objects
80 */
81 lateUploadHooks.add(new DiscardTagsHook());
82 }
83
84 /**
85 * Registers an upload hook. Adds the hook at the first position of the upload hooks.
86 *
87 * @param hook the upload hook. Ignored if null.
88 */
89 public static void registerUploadHook(UploadHook hook) {
90 registerUploadHook(hook, false);
91 }
92
93 /**
94 * Registers an upload hook. Adds the hook at the first position of the upload hooks.
95 *
96 * @param hook the upload hook. Ignored if null.
97 * @param late true, if the hook should be executed after the upload dialog
98 * has been confirmed. Late upload hooks should in general succeed and not
99 * abort the upload.
100 */
101 public static void registerUploadHook(UploadHook hook, boolean late) {
102 if (hook == null) return;
103 if (late) {
104 if (!lateUploadHooks.contains(hook)) {
105 lateUploadHooks.add(0, hook);
106 }
107 } else {
108 if (!uploadHooks.contains(hook)) {
109 uploadHooks.add(0, hook);
110 }
111 }
112 }
113
114 /**
115 * Unregisters an upload hook. Removes the hook from the list of upload hooks.
116 *
117 * @param hook the upload hook. Ignored if null.
118 */
119 public static void unregisterUploadHook(UploadHook hook) {
120 if (hook == null) return;
121 if (uploadHooks.contains(hook)) {
122 uploadHooks.remove(hook);
123 }
124 if (lateUploadHooks.contains(hook)) {
125 lateUploadHooks.remove(hook);
126 }
127 }
128
129 public UploadAction() {
130 super(tr("Upload data"), "upload", tr("Upload all changes in the active data layer to the OSM server"),
131 Shortcut.registerShortcut("file:upload", tr("File: {0}", tr("Upload data")), KeyEvent.VK_UP, Shortcut.CTRL_SHIFT), true);
132 putValue("help", ht("/Action/Upload"));
133 }
134
135 /**
136 * Refreshes the enabled state
137 *
138 */
139 @Override
140 protected void updateEnabledState() {
141 setEnabled(getEditLayer() != null);
142 }
143
144 public static boolean checkPreUploadConditions(AbstractModifiableLayer layer) {
145 return checkPreUploadConditions(layer,
146 layer instanceof OsmDataLayer ? new APIDataSet(((OsmDataLayer) layer).data) : null);
147 }
148
149 protected static void alertUnresolvedConflicts(OsmDataLayer layer) {
150 HelpAwareOptionPane.showOptionDialog(
151 Main.parent,
152 tr("<html>The data to be uploaded participates in unresolved conflicts of layer ''{0}''.<br>"
153 + "You have to resolve them first.</html>", layer.getName()
154 ),
155 tr("Warning"),
156 JOptionPane.WARNING_MESSAGE,
157 HelpUtil.ht("/Action/Upload#PrimitivesParticipateInConflicts")
158 );
159 }
160
161 /**
162 * Warn user about discouraged upload, propose to cancel operation.
163 * @param layer incriminated layer
164 * @return true if the user wants to cancel, false if they want to continue
165 */
166 public static boolean warnUploadDiscouraged(AbstractModifiableLayer layer) {
167 return GuiHelper.warnUser(tr("Upload discouraged"),
168 "<html>" +
169 tr("You are about to upload data from the layer ''{0}''.<br /><br />"+
170 "Sending data from this layer is <b>strongly discouraged</b>. If you continue,<br />"+
171 "it may require you subsequently have to revert your changes, or force other contributors to.<br /><br />"+
172 "Are you sure you want to continue?", layer.getName())+
173 "</html>",
174 ImageProvider.get("upload"), tr("Ignore this hint and upload anyway"));
175 }
176
177 /**
178 * Check whether the preconditions are met to upload data in <code>apiData</code>.
179 * Makes sure upload is allowed, primitives in <code>apiData</code> don't participate in conflicts and
180 * runs the installed {@link UploadHook}s.
181 *
182 * @param layer the source layer of the data to be uploaded
183 * @param apiData the data to be uploaded
184 * @return true, if the preconditions are met; false, otherwise
185 */
186 public static boolean checkPreUploadConditions(AbstractModifiableLayer layer, APIDataSet apiData) {
187 if (layer.isUploadDiscouraged()) {
188 if (warnUploadDiscouraged(layer)) {
189 return false;
190 }
191 }
192 if (layer instanceof OsmDataLayer) {
193 OsmDataLayer osmLayer = (OsmDataLayer) layer;
194 ConflictCollection conflicts = osmLayer.getConflicts();
195 if (apiData.participatesInConflict(conflicts)) {
196 alertUnresolvedConflicts(osmLayer);
197 return false;
198 }
199 }
200 // Call all upload hooks in sequence.
201 // FIXME: this should become an asynchronous task
202 //
203 if (apiData != null) {
204 for (UploadHook hook : uploadHooks) {
205 if (!hook.checkUpload(apiData))
206 return false;
207 }
208 }
209
210 return true;
211 }
212
213 /**
214 * Uploads data to the OSM API.
215 *
216 * @param layer the source layer for the data to upload
217 * @param apiData the primitives to be added, updated, or deleted
218 */
219 public void uploadData(final OsmDataLayer layer, APIDataSet apiData) {
220 if (apiData.isEmpty()) {
221 JOptionPane.showMessageDialog(
222 Main.parent,
223 tr("No changes to upload."),
224 tr("Warning"),
225 JOptionPane.INFORMATION_MESSAGE
226 );
227 return;
228 }
229 if (!checkPreUploadConditions(layer, apiData))
230 return;
231
232 final UploadDialog dialog = UploadDialog.getUploadDialog();
233 dialog.setChangesetTags(layer.data);
234 dialog.setUploadedPrimitives(apiData);
235 dialog.setVisible(true);
236 dialog.rememberUserInput();
237 if (dialog.isCanceled())
238 return;
239
240 for (UploadHook hook : lateUploadHooks) {
241 if (!hook.checkUpload(apiData))
242 return;
243 }
244
245 Main.worker.execute(
246 new UploadPrimitivesTask(
247 UploadDialog.getUploadDialog().getUploadStrategySpecification(),
248 layer,
249 apiData,
250 UploadDialog.getUploadDialog().getChangeset()
251 )
252 );
253 }
254
255 @Override
256 public void actionPerformed(ActionEvent e) {
257 if (!isEnabled())
258 return;
259 if (Main.map == null) {
260 JOptionPane.showMessageDialog(
261 Main.parent,
262 tr("Nothing to upload. Get some data first."),
263 tr("Warning"),
264 JOptionPane.WARNING_MESSAGE
265 );
266 return;
267 }
268 APIDataSet apiData = new APIDataSet(Main.main.getCurrentDataSet());
269 uploadData(Main.main.getEditLayer(), apiData);
270 }
271}
Note: See TracBrowser for help on using the repository browser.