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.EventQueue;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.Collection;
|
---|
11 |
|
---|
12 | import javax.swing.JDialog;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
21 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
22 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
24 | import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
|
---|
25 | import org.openstreetmap.josm.io.OsmApi;
|
---|
26 | import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
|
---|
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 loads the set of primitives referring to the current selection from the OSM
|
---|
33 | * server.
|
---|
34 | *
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class DownloadReferrersAction extends JosmAction implements SelectionChangedListener, LayerChangeListener {
|
---|
38 |
|
---|
39 | public DownloadReferrersAction() {
|
---|
40 | super(tr("Download referrers from OSM..."), "downloadreferrers", tr("Download primitives referring to one of the selected primitives"),
|
---|
41 | Shortcut.registerShortcut("file:downloadreferrers", tr("File: {0}", tr("Download referrers...")), KeyEvent.VK_D, Shortcut.GROUPS_ALT2+Shortcut.GROUP_HOTKEY), true);
|
---|
42 |
|
---|
43 | Layer.listeners.add(this);
|
---|
44 | DataSet.selListeners.add(this);
|
---|
45 | refreshEnabled();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Downloads the primitives referring to the primitives in <code>primitives</code>.
|
---|
50 | * Does nothing if primitives is null or empty.
|
---|
51 | *
|
---|
52 | * @param primitives the collection of primitives.
|
---|
53 | */
|
---|
54 | public void downloadReferrers(Collection<OsmPrimitive> primitives) {
|
---|
55 | if (primitives == null || primitives.isEmpty()) return;
|
---|
56 | Main.worker.submit(new DownloadReferrersTask(primitives));
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | public void actionPerformed(ActionEvent e) {
|
---|
61 | if (!isEnabled())
|
---|
62 | return;
|
---|
63 | OsmDataLayer layer = Main.map.mapView.getEditLayer();
|
---|
64 | Collection<OsmPrimitive> primitives = layer.data.getSelected();
|
---|
65 | downloadReferrers(primitives);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * The asynchronous task for downloading referring primitives
|
---|
70 | *
|
---|
71 | */
|
---|
72 | class DownloadReferrersTask extends PleaseWaitRunnable {
|
---|
73 | private DataSet ds;
|
---|
74 | private boolean cancelled;
|
---|
75 | Exception lastException;
|
---|
76 | private Collection<OsmPrimitive> primitives;
|
---|
77 | private DataSet parents;
|
---|
78 |
|
---|
79 | protected void setIndeterminateEnabled(final boolean enabled) {
|
---|
80 | EventQueue.invokeLater(
|
---|
81 | new Runnable() {
|
---|
82 | public void run() {
|
---|
83 | Main.pleaseWaitDlg.setIndeterminate(enabled);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | );
|
---|
87 | }
|
---|
88 |
|
---|
89 | public DownloadReferrersTask(Collection<OsmPrimitive> primitives) {
|
---|
90 | super("Download referrers", false /* don't ignore exception*/);
|
---|
91 | cancelled = false;
|
---|
92 | this.primitives = primitives;
|
---|
93 | parents = new DataSet();
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected void showLastException() {
|
---|
97 | String msg = lastException.getMessage();
|
---|
98 | if (msg == null) {
|
---|
99 | msg = lastException.toString();
|
---|
100 | }
|
---|
101 | JOptionPane.showMessageDialog(
|
---|
102 | Main.map,
|
---|
103 | msg,
|
---|
104 | tr("Error"),
|
---|
105 | JOptionPane.ERROR_MESSAGE
|
---|
106 | );
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | protected void cancel() {
|
---|
111 | cancelled = true;
|
---|
112 | OsmApi.getOsmApi().cancel();
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | protected void finish() {
|
---|
117 | if (cancelled)
|
---|
118 | return;
|
---|
119 | if (lastException != null) {
|
---|
120 | showLastException();
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | MergeVisitor visitor = new MergeVisitor(Main.map.mapView.getEditLayer().data, parents);
|
---|
125 | visitor.merge();
|
---|
126 | SwingUtilities.invokeLater(
|
---|
127 | new Runnable() {
|
---|
128 | public void run() {
|
---|
129 | Main.map.mapView.getEditLayer().fireDataChange();
|
---|
130 | Main.map.mapView.repaint();
|
---|
131 | }
|
---|
132 | }
|
---|
133 | );
|
---|
134 | if (visitor.getConflicts().isEmpty())
|
---|
135 | return;
|
---|
136 | Main.map.mapView.getEditLayer().getConflicts().add(visitor.getConflicts());
|
---|
137 | JOptionPane op = new JOptionPane(
|
---|
138 | tr("There were {0} conflicts during import.",
|
---|
139 | visitor.getConflicts().size()),
|
---|
140 | JOptionPane.WARNING_MESSAGE
|
---|
141 | );
|
---|
142 | JDialog dialog = op.createDialog(Main.pleaseWaitDlg, tr("Conflicts in data"));
|
---|
143 | dialog.setAlwaysOnTop(true);
|
---|
144 | dialog.setModal(true);
|
---|
145 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
---|
146 | dialog.setVisible(true);
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected void downloadParents(OsmPrimitive primitive) throws OsmTransferException{
|
---|
150 | OsmServerBackreferenceReader reader = new OsmServerBackreferenceReader(primitive);
|
---|
151 | DataSet ds = reader.parseOsm();
|
---|
152 | MergeVisitor visitor = new MergeVisitor(parents, ds);
|
---|
153 | visitor.merge();
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
158 | setIndeterminateEnabled(true);
|
---|
159 | try {
|
---|
160 | int i=1;
|
---|
161 | for (OsmPrimitive primitive: primitives) {
|
---|
162 | if (cancelled)
|
---|
163 | return;
|
---|
164 | String title = tr("({0}/{1}) Loading parents of primitive {2}", i+1,primitives.size(), primitive.getName());
|
---|
165 | Main.pleaseWaitDlg.setTitle(title);
|
---|
166 | downloadParents(primitive);
|
---|
167 | i++;
|
---|
168 | }
|
---|
169 | } catch(Exception e) {
|
---|
170 | if (cancelled)
|
---|
171 | return;
|
---|
172 | lastException = e;
|
---|
173 | } finally {
|
---|
174 | setIndeterminateEnabled(false);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Refreshes the enabled state
|
---|
181 | *
|
---|
182 | */
|
---|
183 | protected void refreshEnabled() {
|
---|
184 | setEnabled(Main.map != null
|
---|
185 | && Main.map.mapView !=null
|
---|
186 | && Main.map.mapView.getEditLayer() != null
|
---|
187 | && ! Main.map.mapView.getEditLayer().data.getSelected().isEmpty()
|
---|
188 | );
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* ---------------------------------------------------------------------------------- */
|
---|
192 | /* Interface SelectionChangeListener */
|
---|
193 | /* ---------------------------------------------------------------------------------- */
|
---|
194 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
195 | refreshEnabled();
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* ---------------------------------------------------------------------------------- */
|
---|
199 | /* Interface LayerChangeListener */
|
---|
200 | /* ---------------------------------------------------------------------------------- */
|
---|
201 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
202 | refreshEnabled();
|
---|
203 | }
|
---|
204 |
|
---|
205 | public void layerAdded(Layer newLayer) {
|
---|
206 | refreshEnabled();
|
---|
207 | }
|
---|
208 |
|
---|
209 | public void layerRemoved(Layer oldLayer) {
|
---|
210 | refreshEnabled();
|
---|
211 | }
|
---|
212 | }
|
---|