1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
20 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
21 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
22 | import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
|
---|
23 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
|
---|
26 | import org.openstreetmap.josm.io.OsmApi;
|
---|
27 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
28 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
29 | import org.xml.sax.SAXException;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This action synchronizes a set of primitives with their state on the server.
|
---|
33 | *
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public class UpdateSelectionAction extends JosmAction {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * handle an exception thrown because a primitive was deleted on the server
|
---|
40 | *
|
---|
41 | * @param id the primitive id
|
---|
42 | */
|
---|
43 | protected void handlePrimitiveGoneException(long id) {
|
---|
44 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
45 | reader.append(getCurrentDataSet(),id);
|
---|
46 | DataSet ds = null;
|
---|
47 | try {
|
---|
48 | ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
49 | } catch(Exception e) {
|
---|
50 | handleUpdateException(e);
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | Main.map.mapView.getEditLayer().mergeFrom(ds);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * handle an exception thrown during updating a primitive
|
---|
59 | *
|
---|
60 | * @param id the id of the primitive
|
---|
61 | * @param e the exception
|
---|
62 | */
|
---|
63 | protected void handleUpdateException(Exception e) {
|
---|
64 | e.printStackTrace();
|
---|
65 | JOptionPane.showMessageDialog(
|
---|
66 | Main.parent,
|
---|
67 | tr("Failed to update the selected primitives."),
|
---|
68 | tr("Update failed"),
|
---|
69 | JOptionPane.ERROR_MESSAGE
|
---|
70 | );
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * handles an exception case: primitive with id <code>id</code> is not in the current
|
---|
75 | * data set
|
---|
76 | *
|
---|
77 | * @param id the primitive id
|
---|
78 | */
|
---|
79 | protected void handleMissingPrimitive(long id) {
|
---|
80 | JOptionPane.showMessageDialog(
|
---|
81 | Main.parent,
|
---|
82 | tr("Could not find primitive with id {0} in the current dataset", new Long(id).toString()),
|
---|
83 | tr("Missing primitive"),
|
---|
84 | JOptionPane.ERROR_MESSAGE
|
---|
85 | );
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Updates the data for for the {@see OsmPrimitive}s in <code>selection</code>
|
---|
90 | * with the data currently kept on the server.
|
---|
91 | *
|
---|
92 | * @param selection a collection of {@see OsmPrimitive}s to update
|
---|
93 | *
|
---|
94 | */
|
---|
95 | public void updatePrimitives(final Collection<OsmPrimitive> selection) {
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * The asynchronous task for updating the data using multi fetch.
|
---|
99 | *
|
---|
100 | */
|
---|
101 | class UpdatePrimitiveTask extends PleaseWaitRunnable {
|
---|
102 | private DataSet ds;
|
---|
103 | private boolean cancelled;
|
---|
104 | Exception lastException;
|
---|
105 |
|
---|
106 | public UpdatePrimitiveTask() {
|
---|
107 | super("Update primitives", false /* don't ignore exception*/);
|
---|
108 | cancelled = false;
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected void showLastException() {
|
---|
112 | String msg = lastException.getMessage();
|
---|
113 | if (msg == null) {
|
---|
114 | msg = lastException.toString();
|
---|
115 | }
|
---|
116 | JOptionPane.showMessageDialog(
|
---|
117 | Main.map,
|
---|
118 | msg,
|
---|
119 | tr("Error"),
|
---|
120 | JOptionPane.ERROR_MESSAGE
|
---|
121 | );
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | protected void cancel() {
|
---|
126 | cancelled = true;
|
---|
127 | OsmApi.getOsmApi().cancel();
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | protected void finish() {
|
---|
132 | if (cancelled)
|
---|
133 | return;
|
---|
134 | if (lastException != null) {
|
---|
135 | showLastException();
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | if (ds != null) {
|
---|
139 | Main.map.mapView.getEditLayer().mergeFrom(ds);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
145 | progressMonitor.indeterminateSubTask("");
|
---|
146 | try {
|
---|
147 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
148 | reader.append(selection);
|
---|
149 | ds = reader.parseOsm(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
150 | } catch(Exception e) {
|
---|
151 | if (cancelled)
|
---|
152 | return;
|
---|
153 | lastException = e;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | Main.worker.submit(new UpdatePrimitiveTask());
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Updates the data for for the {@see OsmPrimitive}s with id <code>id</code>
|
---|
163 | * with the data currently kept on the server.
|
---|
164 | *
|
---|
165 | * @param id the id of a primitive in the {@see DataSet} of the current edit layser
|
---|
166 | *
|
---|
167 | */
|
---|
168 | public void updatePrimitive(long id) {
|
---|
169 | OsmPrimitive primitive = Main.map.mapView.getEditLayer().data.getPrimitiveById(id);
|
---|
170 | Set<OsmPrimitive> s = new HashSet<OsmPrimitive>();
|
---|
171 | s.add(primitive);
|
---|
172 | updatePrimitives(s);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * constructor
|
---|
177 | */
|
---|
178 | public UpdateSelectionAction() {
|
---|
179 | super(tr("Update Selection"),
|
---|
180 | "updateselection",
|
---|
181 | tr("Updates the currently selected primitives from the server"),
|
---|
182 | Shortcut.registerShortcut("file:updateselection",
|
---|
183 | tr("Update Selection"),
|
---|
184 | KeyEvent.VK_U,
|
---|
185 | Shortcut.GROUP_HOTKEY + Shortcut.GROUPS_ALT2),
|
---|
186 | true);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Refreshes the enabled state
|
---|
191 | *
|
---|
192 | */
|
---|
193 | @Override
|
---|
194 | protected void updateEnabledState() {
|
---|
195 | setEnabled(getCurrentDataSet() != null
|
---|
196 | && ! getCurrentDataSet().getSelected().isEmpty()
|
---|
197 | );
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * action handler
|
---|
202 | */
|
---|
203 | public void actionPerformed(ActionEvent e) {
|
---|
204 | if (! isEnabled())
|
---|
205 | return;
|
---|
206 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
207 | if (selection.size() == 0) {
|
---|
208 | OptionPaneUtil.showMessageDialog(
|
---|
209 | Main.parent,
|
---|
210 | tr("There are no selected primitives to update."),
|
---|
211 | tr("Selection empty"),
|
---|
212 | JOptionPane.INFORMATION_MESSAGE
|
---|
213 | );
|
---|
214 | return;
|
---|
215 | }
|
---|
216 | updatePrimitives(selection);
|
---|
217 | }
|
---|
218 | }
|
---|