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

Last change on this file since 2990 was 2990, checked in by jttt, 14 years ago

Fix some eclipse warnings

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.logging.Logger;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.upload.ApiPreconditionCheckerHook;
16import org.openstreetmap.josm.actions.upload.RelationUploadOrderHook;
17import org.openstreetmap.josm.actions.upload.UploadHook;
18import org.openstreetmap.josm.data.APIDataSet;
19import org.openstreetmap.josm.data.conflict.ConflictCollection;
20import org.openstreetmap.josm.gui.HelpAwareOptionPane;
21import org.openstreetmap.josm.gui.help.HelpUtil;
22import org.openstreetmap.josm.gui.io.UploadDialog;
23import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
25import org.openstreetmap.josm.tools.Shortcut;
26
27/**
28 * Action that opens a connection to the osm server and uploads 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 * If the upload fails this action offers various options to resolve conflicts.
34 *
35 * @author imi
36 */
37public class UploadAction extends JosmAction{
38 @SuppressWarnings("unused")
39 static private Logger logger = Logger.getLogger(UploadAction.class.getName());
40 /**
41 * The list of upload hooks. These hooks will be called one after the other
42 * when the user wants to upload data. Plugins can insert their own hooks here
43 * if they want to be able to veto an upload.
44 *
45 * Be default, the standard upload dialog is the only element in the list.
46 * Plugins should normally insert their code before that, so that the upload
47 * dialog is the last thing shown before upload really starts; on occasion
48 * however, a plugin might also want to insert something after that.
49 */
50 private static final LinkedList<UploadHook> uploadHooks = new LinkedList<UploadHook>();
51 static {
52 /**
53 * Checks server capabilities before upload.
54 */
55 uploadHooks.add(new ApiPreconditionCheckerHook());
56
57 /**
58 * Adjusts the upload order of new relations
59 */
60 uploadHooks.add(new RelationUploadOrderHook());
61 }
62
63 /**
64 * Registers an upload hook. Adds the hook at the first position of the upload hooks.
65 *
66 * @param hook the upload hook. Ignored if null.
67 */
68 public static void registerUploadHook(UploadHook hook) {
69 if(hook == null) return;
70 if (!uploadHooks.contains(hook)) {
71 uploadHooks.add(0,hook);
72 }
73 }
74
75 /**
76 * Unregisters an upload hook. Removes the hook from the list of upload hooks.
77 *
78 * @param hook the upload hook. Ignored if null.
79 */
80 public static void unregisterUploadHook(UploadHook hook) {
81 if(hook == null) return;
82 if (uploadHooks.contains(hook)) {
83 uploadHooks.remove(hook);
84 }
85 }
86
87 public UploadAction() {
88 super(tr("Upload data"), "upload", tr("Upload all changes in the active data layer to the OSM server"),
89 Shortcut.registerShortcut("file:upload", tr("File: {0}", tr("Upload data")), KeyEvent.VK_U, Shortcut.GROUPS_ALT1+Shortcut.GROUP_HOTKEY), true);
90 putValue("help", ht("/Action/Upload"));
91 }
92
93 /**
94 * Refreshes the enabled state
95 *
96 */
97 @Override
98 protected void updateEnabledState() {
99 setEnabled(getEditLayer() != null);
100 }
101
102 public boolean checkPreUploadConditions(OsmDataLayer layer) {
103 return checkPreUploadConditions(layer, new APIDataSet(layer.data));
104 }
105
106 protected void alertUnresolvedConflicts(OsmDataLayer layer) {
107 HelpAwareOptionPane.showOptionDialog(
108 Main.parent,
109 tr("<html>The data to be uploaded participates in unresolved conflicts of layer ''{0}''.<br>"
110 + "You have to resolve them first.</html>", layer.getName()
111 ),
112 tr("Warning"),
113 JOptionPane.WARNING_MESSAGE,
114 HelpUtil.ht("/Action/Upload#PrimitivesParticipateInConflicts")
115 );
116 }
117
118 /**
119 * Check whether the preconditions are met to upload data in <code>apiData</code>.
120 * Makes sure primitives in <code>apiData</code> don't participate in conflicts and
121 * runs the installed {@see UploadHook}s.
122 *
123 * @param layer the source layer of the data to be uploaded
124 * @param apiData the data to be uploaded
125 * @return true, if the preconditions are met; false, otherwise
126 */
127 public boolean checkPreUploadConditions(OsmDataLayer layer, APIDataSet apiData) {
128 ConflictCollection conflicts = layer.getConflicts();
129 if (apiData.participatesInConflict(conflicts)) {
130 alertUnresolvedConflicts(layer);
131 return false;
132 }
133 // Call all upload hooks in sequence.
134 // FIXME: this should become an asynchronous task
135 //
136 for(UploadHook hook : uploadHooks)
137 if(!hook.checkUpload(apiData))
138 return false;
139
140 return true;
141 }
142
143 /**
144 * Uploads data to the OSM API.
145 *
146 * @param layer the source layer for the data to upload
147 * @param apiData the primitives to be added, updated, or deleted
148 */
149 public void uploadData(OsmDataLayer layer, APIDataSet apiData) {
150 if (apiData.isEmpty()) {
151 JOptionPane.showMessageDialog(
152 Main.parent,
153 tr("No changes to upload."),
154 tr("Warning"),
155 JOptionPane.INFORMATION_MESSAGE
156 );
157 return;
158 }
159 if (!checkPreUploadConditions(layer, apiData))
160 return;
161
162 final UploadDialog dialog = UploadDialog.getUploadDialog();
163 dialog.setUploadedPrimitives(apiData);
164 dialog.setVisible(true);
165 if (dialog.isCanceled())
166 return;
167 dialog.rememberUserInput();
168
169 Main.worker.execute(
170 new UploadPrimitivesTask(
171 UploadDialog.getUploadDialog().getUploadStrategySpecification(),
172 layer,
173 apiData,
174 UploadDialog.getUploadDialog().getChangeset()
175 )
176 );
177 }
178
179 public void actionPerformed(ActionEvent e) {
180 if (!isEnabled())
181 return;
182 if (Main.map == null) {
183 JOptionPane.showMessageDialog(
184 Main.parent,
185 tr("Nothing to upload. Get some data first."),
186 tr("Warning"),
187 JOptionPane.WARNING_MESSAGE
188 );
189 return;
190 }
191 APIDataSet apiData = new APIDataSet(Main.main.getCurrentDataSet());
192 uploadData(Main.map.mapView.getEditLayer(), apiData);
193 }
194}
Note: See TracBrowser for help on using the repository browser.