1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 | import javax.swing.SwingUtilities;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
20 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.io.OsmApi;
|
---|
22 | import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
|
---|
23 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
24 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
25 | import org.xml.sax.SAXException;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * This is an asynchronous task for loading the parents of a given relation.
|
---|
29 | *
|
---|
30 | * Typical usage:
|
---|
31 | * <pre>
|
---|
32 | * final ParentRelationLoadingTask task = new ParentRelationLoadingTask(
|
---|
33 | * child, // the child relation
|
---|
34 | * Main.main.getEditLayer(), // the edit layer
|
---|
35 | * true, // load fully
|
---|
36 | * new PleaseWaitProgressMonitor() // a progress monitor
|
---|
37 | * );
|
---|
38 | * task.setContinuation(
|
---|
39 | * new Runnable() {
|
---|
40 | * public void run() {
|
---|
41 | * if (task.isCanceled() || task.hasError())
|
---|
42 | * return;
|
---|
43 | * List<Relation> parents = task.getParents();
|
---|
44 | * // do something with the parent relations
|
---|
45 | * }
|
---|
46 | * );
|
---|
47 | *
|
---|
48 | * // start the task
|
---|
49 | * Main.worker.submit(task);
|
---|
50 | * </pre>
|
---|
51 | *
|
---|
52 | */
|
---|
53 | public class ParentRelationLoadingTask extends PleaseWaitRunnable{
|
---|
54 | private boolean canceled;
|
---|
55 | private Exception lastException;
|
---|
56 | private DataSet referrers;
|
---|
57 | private boolean full;
|
---|
58 | private OsmDataLayer layer;
|
---|
59 | private Relation child;
|
---|
60 | private ArrayList<Relation> parents;
|
---|
61 | private Runnable continuation;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Creates a new task for asynchronously downloading the parents of a child relation.
|
---|
65 | *
|
---|
66 | * @param child the child relation. Must not be null. Must have an id > 0.
|
---|
67 | * @param layer the OSM data layer. Must not be null.
|
---|
68 | * @param full if true, parent relations are fully downloaded (i.e. with their members)
|
---|
69 | * @param monitor the progress monitor to be used
|
---|
70 | *
|
---|
71 | * @exception IllegalArgumentException thrown if child is null
|
---|
72 | * @exception IllegalArgumentException thrown if layer is null
|
---|
73 | * @exception IllegalArgumentException thrown if child.getId() == 0
|
---|
74 | */
|
---|
75 | public ParentRelationLoadingTask(Relation child, OsmDataLayer layer, boolean full, PleaseWaitProgressMonitor monitor ) {
|
---|
76 | super(tr("Download referring relations"), monitor, false /* don't ignore exception */);
|
---|
77 | CheckParameterUtil.ensureValidPrimitiveId(child, "child");
|
---|
78 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
79 | referrers = null;
|
---|
80 | this.layer = layer;
|
---|
81 | parents = new ArrayList<Relation>();
|
---|
82 | this.child = child;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Set a continuation which is called upon the job finished.
|
---|
87 | *
|
---|
88 | * @param continuation the continuation
|
---|
89 | */
|
---|
90 | public void setContinuation(Runnable continuation) {
|
---|
91 | this.continuation = continuation;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Replies true if this has been canceled by the user.
|
---|
96 | *
|
---|
97 | * @return true if this has been canceled by the user.
|
---|
98 | */
|
---|
99 | public boolean isCanceled() {
|
---|
100 | return canceled;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Replies true if an exception has been caught during the execution of this task.
|
---|
105 | *
|
---|
106 | * @return true if an exception has been caught during the execution of this task.
|
---|
107 | */
|
---|
108 | public boolean hasError() {
|
---|
109 | return lastException != null;
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected OsmDataLayer getLayer() {
|
---|
113 | return layer;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public List<Relation> getParents() {
|
---|
117 | return parents;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | protected void cancel() {
|
---|
122 | canceled = true;
|
---|
123 | OsmApi.getOsmApi().cancel();
|
---|
124 | }
|
---|
125 |
|
---|
126 | protected void showLastException() {
|
---|
127 | String msg = lastException.getMessage();
|
---|
128 | if (msg == null) {
|
---|
129 | msg = lastException.toString();
|
---|
130 | }
|
---|
131 | JOptionPane.showMessageDialog(
|
---|
132 | Main.parent,
|
---|
133 | msg,
|
---|
134 | tr("Error"),
|
---|
135 | JOptionPane.ERROR_MESSAGE
|
---|
136 | );
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | protected void finish() {
|
---|
141 | if (canceled) return;
|
---|
142 | if (lastException != null) {
|
---|
143 | showLastException();
|
---|
144 | return;
|
---|
145 | }
|
---|
146 | parents.clear();
|
---|
147 | for (Relation parent : referrers.getRelations()) {
|
---|
148 | parents.add((Relation) getLayer().data.getPrimitiveById(parent));
|
---|
149 | }
|
---|
150 | if (continuation != null) {
|
---|
151 | continuation.run();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
157 | try {
|
---|
158 | progressMonitor.indeterminateSubTask(null);
|
---|
159 | OsmServerBackreferenceReader reader = new OsmServerBackreferenceReader(child, full);
|
---|
160 | referrers = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
|
---|
161 | if (referrers != null) {
|
---|
162 | final DataSetMerger visitor = new DataSetMerger(getLayer().data, referrers);
|
---|
163 | visitor.merge();
|
---|
164 |
|
---|
165 | // copy the merged layer's data source info
|
---|
166 | for (DataSource src : referrers.dataSources) {
|
---|
167 | getLayer().data.dataSources.add(src);
|
---|
168 | }
|
---|
169 | // FIXME: this is necessary because there are dialogs listening
|
---|
170 | // for DataChangeEvents which manipulate Swing components on this
|
---|
171 | // thread.
|
---|
172 | //
|
---|
173 | SwingUtilities.invokeLater(
|
---|
174 | new Runnable() {
|
---|
175 | @Override
|
---|
176 | public void run() {
|
---|
177 | getLayer().onPostDownloadFromServer();
|
---|
178 | }
|
---|
179 | }
|
---|
180 | );
|
---|
181 |
|
---|
182 | if (visitor.getConflicts().isEmpty())
|
---|
183 | return;
|
---|
184 | getLayer().getConflicts().add(visitor.getConflicts());
|
---|
185 | JOptionPane.showMessageDialog(
|
---|
186 | Main.parent,
|
---|
187 | tr("There were {0} conflicts during import.",
|
---|
188 | visitor.getConflicts().size()),
|
---|
189 | tr("Warning"),
|
---|
190 | JOptionPane.WARNING_MESSAGE
|
---|
191 | );
|
---|
192 | }
|
---|
193 | } catch(Exception e) {
|
---|
194 | if (canceled) {
|
---|
195 | Main.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
|
---|
196 | return;
|
---|
197 | }
|
---|
198 | lastException = e;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|