source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/ParentRelationLoadingTask.java@ 8444

Last change on this file since 8444 was 8444, checked in by Don-vip, 9 years ago

remove extra whitespaces

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.List;
9
10import javax.swing.JOptionPane;
11import javax.swing.SwingUtilities;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.DataSource;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.DataSetMerger;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.gui.PleaseWaitRunnable;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
21import org.openstreetmap.josm.io.OsmApi;
22import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
23import org.openstreetmap.josm.io.OsmTransferException;
24import org.openstreetmap.josm.tools.CheckParameterUtil;
25import 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&lt;Relation&gt; 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 */
53public 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 List<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 &gt; 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 * @throws IllegalArgumentException if child is null
72 * @throws IllegalArgumentException if layer is null
73 * @throws IllegalArgumentException 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<>();
82 this.child = child;
83 this.full = full;
84 }
85
86 /**
87 * Set a continuation which is called upon the job finished.
88 *
89 * @param continuation the continuation
90 */
91 public void setContinuation(Runnable continuation) {
92 this.continuation = continuation;
93 }
94
95 /**
96 * Replies true if this has been canceled by the user.
97 *
98 * @return true if this has been canceled by the user.
99 */
100 public boolean isCanceled() {
101 return canceled;
102 }
103
104 /**
105 * Replies true if an exception has been caught during the execution of this task.
106 *
107 * @return true if an exception has been caught during the execution of this task.
108 */
109 public boolean hasError() {
110 return lastException != null;
111 }
112
113 protected OsmDataLayer getLayer() {
114 return layer;
115 }
116
117 public List<Relation> getParents() {
118 return parents;
119 }
120
121 @Override
122 protected void cancel() {
123 canceled = true;
124 OsmApi.getOsmApi().cancel();
125 }
126
127 protected void showLastException() {
128 String msg = lastException.getMessage();
129 if (msg == null) {
130 msg = lastException.toString();
131 }
132 JOptionPane.showMessageDialog(
133 Main.parent,
134 msg,
135 tr("Error"),
136 JOptionPane.ERROR_MESSAGE
137 );
138 }
139
140 @Override
141 protected void finish() {
142 if (canceled) return;
143 if (lastException != null) {
144 showLastException();
145 return;
146 }
147 parents.clear();
148 for (Relation parent : referrers.getRelations()) {
149 parents.add((Relation) getLayer().data.getPrimitiveById(parent));
150 }
151 if (continuation != null) {
152 continuation.run();
153 }
154 }
155
156 @Override
157 protected void realRun() throws SAXException, IOException, OsmTransferException {
158 try {
159 progressMonitor.indeterminateSubTask(null);
160 OsmServerBackreferenceReader reader = new OsmServerBackreferenceReader(child, full);
161 referrers = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
162 if (referrers != null) {
163 final DataSetMerger visitor = new DataSetMerger(getLayer().data, referrers);
164 visitor.merge();
165
166 // copy the merged layer's data source info
167 for (DataSource src : referrers.dataSources) {
168 getLayer().data.dataSources.add(src);
169 }
170 // FIXME: this is necessary because there are dialogs listening
171 // for DataChangeEvents which manipulate Swing components on this
172 // thread.
173 //
174 SwingUtilities.invokeLater(
175 new Runnable() {
176 @Override
177 public void run() {
178 getLayer().onPostDownloadFromServer();
179 }
180 }
181 );
182
183 if (visitor.getConflicts().isEmpty())
184 return;
185 getLayer().getConflicts().add(visitor.getConflicts());
186 JOptionPane.showMessageDialog(
187 Main.parent,
188 tr("There were {0} conflicts during import.",
189 visitor.getConflicts().size()),
190 tr("Warning"),
191 JOptionPane.WARNING_MESSAGE
192 );
193 }
194 } catch(Exception e) {
195 if (canceled) {
196 Main.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
197 return;
198 }
199 lastException = e;
200 }
201 }
202}
Note: See TracBrowser for help on using the repository browser.