1 | // License: GPL. For details, see LICENSE file.
|
---|
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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.text.MessageFormat;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.Map;
|
---|
15 | import java.util.Map.Entry;
|
---|
16 |
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.SwingUtilities;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
25 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
27 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
28 | import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
|
---|
29 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
30 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
31 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
32 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
33 | import org.xml.sax.SAXException;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * This action loads the set of primitives referring to the current selection from the OSM
|
---|
37 | * server.
|
---|
38 | *
|
---|
39 | */
|
---|
40 | public class DownloadReferrersAction extends JosmAction{
|
---|
41 |
|
---|
42 | public DownloadReferrersAction() {
|
---|
43 | super(tr("Download parent ways/relations..."), "downloadreferrers", tr("Download primitives referring to one of the selected primitives"),
|
---|
44 | Shortcut.registerShortcut("file:downloadreferrers", tr("File: {0}", tr("Download parent ways/relations...")), KeyEvent.VK_D, Shortcut.GROUPS_ALT2+Shortcut.GROUP_HOTKEY), true);
|
---|
45 | putValue("help", ht("/Action/Downloadreferrers"));
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Downloads the primitives referring to the primitives in <code>primitives</code>
|
---|
50 | * into the target layer <code>targetLayer</code>.
|
---|
51 | * Does nothing if primitives is null or empty.
|
---|
52 | *
|
---|
53 | * @param targetLayer the target layer. Must not be null.
|
---|
54 | * @param children the collection of child primitives.
|
---|
55 | * @exception IllegalArgumentException thrown if targetLayer is null
|
---|
56 | */
|
---|
57 | static public void downloadReferrers(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) throws IllegalArgumentException {
|
---|
58 | if (children == null || children.isEmpty()) return;
|
---|
59 | Main.worker.submit(new DownloadReferrersTask(targetLayer, children));
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Downloads the primitives referring to the primitives in <code>primitives</code>
|
---|
64 | * into the target layer <code>targetLayer</code>.
|
---|
65 | * Does nothing if primitives is null or empty.
|
---|
66 | *
|
---|
67 | * @param targetLayer the target layer. Must not be null.
|
---|
68 | * @param children the collection of primitives, given as map of ids and types
|
---|
69 | * @exception IllegalArgumentException thrown if targetLayer is null
|
---|
70 | */
|
---|
71 | static public void downloadReferrers(OsmDataLayer targetLayer, Map<Long, OsmPrimitiveType> children) throws IllegalArgumentException {
|
---|
72 | if (children == null || children.isEmpty()) return;
|
---|
73 | Main.worker.submit(new DownloadReferrersTask(targetLayer, children));
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Downloads the primitives referring to the primitive given by <code>id</code> and
|
---|
78 | * <code>type</code>.
|
---|
79 | *
|
---|
80 | *
|
---|
81 | * @param targetLayer the target layer. Must not be null.
|
---|
82 | * @param id the primitive id. id > 0 required.
|
---|
83 | * @param type the primitive type. type != null required
|
---|
84 | * @exception IllegalArgumentException thrown if targetLayer is null
|
---|
85 | * @exception IllegalArgumentException thrown if id <= 0
|
---|
86 | * @exception IllegalArgumentException thrown if type == null
|
---|
87 | */
|
---|
88 | static public void downloadReferrers(OsmDataLayer targetLayer, long id, OsmPrimitiveType type) throws IllegalArgumentException {
|
---|
89 | if (id <= 0)
|
---|
90 | throw new IllegalArgumentException(MessageFormat.format("Id > 0 required, got {0}", id));
|
---|
91 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
92 | Main.worker.submit(new DownloadReferrersTask(targetLayer, id, type));
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void actionPerformed(ActionEvent e) {
|
---|
96 | if (!isEnabled() || ! Main.isDisplayingMapView())
|
---|
97 | return;
|
---|
98 | OsmDataLayer layer = Main.map.mapView.getEditLayer();
|
---|
99 | if (layer == null)
|
---|
100 | return;
|
---|
101 | Collection<OsmPrimitive> primitives = layer.data.getSelected();
|
---|
102 | downloadReferrers(layer,primitives);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The asynchronous task for downloading referring primitives
|
---|
107 | *
|
---|
108 | */
|
---|
109 | public static class DownloadReferrersTask extends PleaseWaitRunnable {
|
---|
110 | private boolean cancelled;
|
---|
111 | private Exception lastException;
|
---|
112 | private OsmServerBackreferenceReader reader;
|
---|
113 | /** the target layer */
|
---|
114 | private OsmDataLayer targetLayer;
|
---|
115 | /** the collection of child primitives */
|
---|
116 | private Map<Long, OsmPrimitiveType> children;
|
---|
117 | /** the parents */
|
---|
118 | private DataSet parents;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * constructor
|
---|
122 | *
|
---|
123 | * @param targetLayer the target layer for the downloaded primitives. Must not be null.
|
---|
124 | * @param children the collection of child primitives for which parents are to be downloaded
|
---|
125 | *
|
---|
126 | */
|
---|
127 | public DownloadReferrersTask(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) {
|
---|
128 | super("Download referrers", false /* don't ignore exception*/);
|
---|
129 | CheckParameterUtil.ensureParameterNotNull(targetLayer, "targetLayer");
|
---|
130 | cancelled = false;
|
---|
131 | this.children = new HashMap<Long, OsmPrimitiveType>();
|
---|
132 | if (children != null) {
|
---|
133 | for (OsmPrimitive p: children) {
|
---|
134 | if (! p.isNew()) {
|
---|
135 | this.children.put(p.getId(), OsmPrimitiveType.from(p));
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | this.targetLayer = targetLayer;
|
---|
140 | parents = new DataSet();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * constructor
|
---|
145 | *
|
---|
146 | * @param targetLayer the target layer for the downloaded primitives. Must not be null.
|
---|
147 | * @param primitives the collection of children for which parents are to be downloaded. Children
|
---|
148 | * are specified by their id and their type.
|
---|
149 | *
|
---|
150 | */
|
---|
151 | public DownloadReferrersTask(OsmDataLayer targetLayer, Map<Long, OsmPrimitiveType> children) {
|
---|
152 | super("Download referrers", false /* don't ignore exception*/);
|
---|
153 | CheckParameterUtil.ensureParameterNotNull(targetLayer, "targetLayer");
|
---|
154 | cancelled = false;
|
---|
155 | this.children = new HashMap<Long, OsmPrimitiveType>();
|
---|
156 | if (children != null) {
|
---|
157 | for (Entry<Long, OsmPrimitiveType> entry : children.entrySet()) {
|
---|
158 | if (entry.getKey() > 0 && entry.getValue() != null) {
|
---|
159 | children.put(entry.getKey(), entry.getValue());
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | this.targetLayer = targetLayer;
|
---|
164 | parents = new DataSet();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * constructor
|
---|
169 | *
|
---|
170 | * @param targetLayer the target layer. Must not be null.
|
---|
171 | * @param id the primitive id. id > 0 required.
|
---|
172 | * @param type the primitive type. type != null required
|
---|
173 | * @exception IllegalArgumentException thrown if id <= 0
|
---|
174 | * @exception IllegalArgumentException thrown if type == null
|
---|
175 | * @exception IllegalArgumentException thrown if targetLayer == null
|
---|
176 | *
|
---|
177 | */
|
---|
178 | public DownloadReferrersTask(OsmDataLayer targetLayer, long id, OsmPrimitiveType type) throws IllegalArgumentException {
|
---|
179 | super("Download referrers", false /* don't ignore exception*/);
|
---|
180 | CheckParameterUtil.ensureParameterNotNull(targetLayer, "targetLayer");
|
---|
181 | if (id <= 0)
|
---|
182 | throw new IllegalArgumentException(MessageFormat.format("Id > 0 required, got {0}", id));
|
---|
183 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
184 | cancelled = false;
|
---|
185 | this.children = new HashMap<Long, OsmPrimitiveType>();
|
---|
186 | this.children.put(id, type);
|
---|
187 | this.targetLayer = targetLayer;
|
---|
188 | parents = new DataSet();
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | protected void cancel() {
|
---|
193 | cancelled = true;
|
---|
194 | synchronized(this) {
|
---|
195 | if (reader != null) {
|
---|
196 | reader.cancel();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Override
|
---|
202 | protected void finish() {
|
---|
203 | if (cancelled)
|
---|
204 | return;
|
---|
205 | if (lastException != null) {
|
---|
206 | ExceptionUtil.explainException(lastException);
|
---|
207 | return;
|
---|
208 | }
|
---|
209 |
|
---|
210 | DataSetMerger visitor = new DataSetMerger(targetLayer.data, parents);
|
---|
211 | visitor.merge();
|
---|
212 | SwingUtilities.invokeLater(
|
---|
213 | new Runnable() {
|
---|
214 | public void run() {
|
---|
215 | targetLayer.fireDataChange();
|
---|
216 | targetLayer.onPostDownloadFromServer();
|
---|
217 | Main.map.mapView.repaint();
|
---|
218 | }
|
---|
219 | }
|
---|
220 | );
|
---|
221 | if (visitor.getConflicts().isEmpty())
|
---|
222 | return;
|
---|
223 | targetLayer.getConflicts().add(visitor.getConflicts());
|
---|
224 | JOptionPane.showMessageDialog(
|
---|
225 | Main.parent,
|
---|
226 | trn("There was {0} conflict during import.",
|
---|
227 | "There were {0} conflicts during import.",
|
---|
228 | visitor.getConflicts().size(),
|
---|
229 | visitor.getConflicts().size()
|
---|
230 | ),
|
---|
231 | trn("Conflict during download", "Conflicts during download", visitor.getConflicts().size()),
|
---|
232 | JOptionPane.WARNING_MESSAGE
|
---|
233 | );
|
---|
234 | }
|
---|
235 |
|
---|
236 | protected void downloadParents(long id, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException{
|
---|
237 | reader = new OsmServerBackreferenceReader(id, type);
|
---|
238 | DataSet ds = reader.parseOsm(progressMonitor);
|
---|
239 | synchronized(this) { // avoid race condition in cancel()
|
---|
240 | reader = null;
|
---|
241 | }
|
---|
242 | DataSetMerger visitor = new DataSetMerger(parents, ds);
|
---|
243 | visitor.merge();
|
---|
244 | }
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
248 | try {
|
---|
249 | progressMonitor.setTicksCount(children.size());
|
---|
250 | int i=1;
|
---|
251 | for (Entry<Long, OsmPrimitiveType> entry: children.entrySet()) {
|
---|
252 | if (cancelled)
|
---|
253 | return;
|
---|
254 | String msg = "";
|
---|
255 | switch(entry.getValue()) {
|
---|
256 | case NODE: msg = tr("({0}/{1}) Loading parents of node {2}", i+1,children.size(), entry.getKey()); break;
|
---|
257 | case WAY: msg = tr("({0}/{1}) Loading parents of way {2}", i+1,children.size(), entry.getKey()); break;
|
---|
258 | case RELATION: msg = tr("({0}/{1}) Loading parents of relation {2}", i+1,children.size(), entry.getKey()); break;
|
---|
259 | }
|
---|
260 | progressMonitor.subTask(msg);
|
---|
261 | downloadParents(entry.getKey(), entry.getValue(), progressMonitor.createSubTaskMonitor(1, false));
|
---|
262 | i++;
|
---|
263 | }
|
---|
264 | } catch(Exception e) {
|
---|
265 | if (cancelled)
|
---|
266 | return;
|
---|
267 | lastException = e;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | @Override
|
---|
273 | protected void updateEnabledState() {
|
---|
274 | if (getCurrentDataSet() == null) {
|
---|
275 | setEnabled(false);
|
---|
276 | } else {
|
---|
277 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | @Override
|
---|
282 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
283 | setEnabled(selection != null && !selection.isEmpty());
|
---|
284 | }
|
---|
285 | }
|
---|