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

Last change on this file since 8554 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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