1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.logging.Logger;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.upload.ApiPreconditionCheckerHook;
|
---|
16 | import org.openstreetmap.josm.actions.upload.RelationUploadOrderHook;
|
---|
17 | import org.openstreetmap.josm.actions.upload.UploadHook;
|
---|
18 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
19 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
20 | import org.openstreetmap.josm.gui.io.UploadDialog;
|
---|
21 | import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import 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 | */
|
---|
35 | public 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 | }
|
---|