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

Last change on this file since 2667 was 2598, checked in by Gubaer, 14 years ago

comment to follow in a later commit
Have to break up a commit because of SSL problem in SVN (filed a ticket - #4093)

  • Property svn:eol-style set to native
File size: 6.0 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.io.UploadDialog;
21import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.tools.Shortcut;
24
25/**
26 * Action that opens a connection to the osm server and uploads all changes.
27 *
28 * An dialog is displayed asking the user to specify a rectangle to grab.
29 * The url and account settings from the preferences are used.
30 *
31 * If the upload fails this action offers various options to resolve conflicts.
32 *
33 * @author imi
34 */
35public class UploadAction extends JosmAction{
36 static private Logger logger = Logger.getLogger(UploadAction.class.getName());
37 /**
38 * The list of upload hooks. These hooks will be called one after the other
39 * when the user wants to upload data. Plugins can insert their own hooks here
40 * if they want to be able to veto an upload.
41 *
42 * Be default, the standard upload dialog is the only element in the list.
43 * Plugins should normally insert their code before that, so that the upload
44 * dialog is the last thing shown before upload really starts; on occasion
45 * however, a plugin might also want to insert something after that.
46 */
47 private static final LinkedList<UploadHook> uploadHooks = new LinkedList<UploadHook>();
48 static {
49 /**
50 * Checks server capabilities before upload.
51 */
52 uploadHooks.add(new ApiPreconditionCheckerHook());
53
54 /**
55 * Adjusts the upload order of new relations
56 */
57 uploadHooks.add(new RelationUploadOrderHook());
58 }
59
60 /**
61 * Registers an upload hook. Adds the hook at the first position of the upload hooks.
62 *
63 * @param hook the upload hook. Ignored if null.
64 */
65 public static void registerUploadHook(UploadHook hook) {
66 if(hook == null) return;
67 if (!uploadHooks.contains(hook)) {
68 uploadHooks.add(0,hook);
69 }
70 }
71
72 /**
73 * Unregisters an upload hook. Removes the hook from the list of upload hooks.
74 *
75 * @param hook the upload hook. Ignored if null.
76 */
77 public static void unregisterUploadHook(UploadHook hook) {
78 if(hook == null) return;
79 if (uploadHooks.contains(hook)) {
80 uploadHooks.remove(hook);
81 }
82 }
83
84 public UploadAction() {
85 super(tr("Upload data"), "upload", tr("Upload all changes in the active data layer to the OSM server"),
86 Shortcut.registerShortcut("file:upload", tr("File: {0}", tr("Upload data")), KeyEvent.VK_U, Shortcut.GROUPS_ALT1+Shortcut.GROUP_HOTKEY), true);
87 putValue("help", ht("/Action/Upload"));
88 }
89
90 /**
91 * Refreshes the enabled state
92 *
93 */
94 @Override
95 protected void updateEnabledState() {
96 setEnabled(getEditLayer() != null);
97 }
98
99 public boolean checkPreUploadConditions(OsmDataLayer layer) {
100 return checkPreUploadConditions(layer, new APIDataSet(layer.data));
101 }
102
103 public boolean checkPreUploadConditions(OsmDataLayer layer, APIDataSet apiData) {
104 ConflictCollection conflicts = layer.getConflicts();
105 if (conflicts !=null && !conflicts.isEmpty()) {
106 JOptionPane.showMessageDialog(
107 Main.parent,
108 tr("<html>There are unresolved conflicts in layer ''{0}''.<br>"
109 + "You have to resolve them first.</html>", layer.getName()),
110 tr("Warning"),
111 JOptionPane.WARNING_MESSAGE
112 );
113 return false;
114 }
115 // Call all upload hooks in sequence. The upload confirmation dialog
116 // is one of these.
117 for(UploadHook hook : uploadHooks)
118 if(!hook.checkUpload(apiData))
119 return false;
120
121 return true;
122 }
123
124 public void uploadData(OsmDataLayer layer, APIDataSet apiData) {
125 if (apiData.isEmpty()) {
126 JOptionPane.showMessageDialog(
127 Main.parent,
128 tr("No changes to upload."),
129 tr("Warning"),
130 JOptionPane.INFORMATION_MESSAGE
131 );
132 return;
133 }
134 if (!checkPreUploadConditions(layer, apiData))
135 return;
136
137 final UploadDialog dialog = UploadDialog.getUploadDialog();
138 dialog.setUploadedPrimitives(apiData);
139 dialog.setVisible(true);
140 if (dialog.isCanceled())
141 return;
142 dialog.rememberUserInput();
143
144 Main.worker.execute(
145 new UploadPrimitivesTask(
146 UploadDialog.getUploadDialog().getUploadStrategySpecification(),
147 layer,
148 apiData,
149 UploadDialog.getUploadDialog().getChangeset()
150 )
151 );
152 }
153
154 public void actionPerformed(ActionEvent e) {
155 if (!isEnabled())
156 return;
157 if (Main.map == null) {
158 JOptionPane.showMessageDialog(
159 Main.parent,
160 tr("Nothing to upload. Get some data first."),
161 tr("Warning"),
162 JOptionPane.WARNING_MESSAGE
163 );
164 return;
165 }
166 APIDataSet apiData = new APIDataSet(Main.main.getCurrentDataSet());
167 uploadData(Main.map.mapView.getEditLayer(), apiData);
168 }
169}
Note: See TracBrowser for help on using the repository browser.