1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.text.MessageFormat;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
17 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
18 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
19 | import org.openstreetmap.josm.data.osm.history.History;
|
---|
20 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
23 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
24 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
25 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
---|
26 | import org.openstreetmap.josm.io.OsmServerHistoryReader;
|
---|
27 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
28 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
29 | import org.xml.sax.SAXException;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Loads the object history of an collection of objects from the
|
---|
33 | * server.
|
---|
34 | *
|
---|
35 | * It provides a fluent API for configuration.
|
---|
36 | *
|
---|
37 | * Sample usage:
|
---|
38 | *
|
---|
39 | * <pre>
|
---|
40 | * HistoryLoadTask task = new HistoryLoadTask()
|
---|
41 | * .add(1, OsmPrimitiveType.NODE)
|
---|
42 | * .add(1233, OsmPrimitiveType.WAY)
|
---|
43 | * .add(37234, OsmPrimitveType.RELATION)
|
---|
44 | * .add(aHistoryItem);
|
---|
45 | *
|
---|
46 | * Main.worker.execute(task);
|
---|
47 | *
|
---|
48 | * </pre>
|
---|
49 | */
|
---|
50 | public class HistoryLoadTask extends PleaseWaitRunnable {
|
---|
51 |
|
---|
52 | private boolean canceled;
|
---|
53 | private Exception lastException;
|
---|
54 | private final Set<PrimitiveId> toLoad;
|
---|
55 | private HistoryDataSet loadedData;
|
---|
56 | private OsmServerHistoryReader reader;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Constructs a new {@code HistoryLoadTask}.
|
---|
60 | */
|
---|
61 | public HistoryLoadTask() {
|
---|
62 | super(tr("Load history"), true);
|
---|
63 | toLoad = new HashSet<>();
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Constructs a new {@code HistoryLoadTask}.
|
---|
68 | *
|
---|
69 | * @param parent the component to be used as reference to find the
|
---|
70 | * parent for {@link org.openstreetmap.josm.gui.PleaseWaitDialog}.
|
---|
71 | * Must not be <code>null</code>.
|
---|
72 | * @throws IllegalArgumentException if parent is <code>null</code>
|
---|
73 | */
|
---|
74 | public HistoryLoadTask(Component parent) {
|
---|
75 | super(parent, tr("Load history"), true);
|
---|
76 | CheckParameterUtil.ensureParameterNotNull(parent, "parent");
|
---|
77 | toLoad = new HashSet<>();
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Adds an object whose history is to be loaded.
|
---|
82 | *
|
---|
83 | * @param id the object id
|
---|
84 | * @param type the object type
|
---|
85 | * @return this task
|
---|
86 | */
|
---|
87 | public HistoryLoadTask add(long id, OsmPrimitiveType type) {
|
---|
88 | if (id <= 0)
|
---|
89 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got {1}.", "id", id));
|
---|
90 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
91 | SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
|
---|
92 | toLoad.add(pid);
|
---|
93 | return this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Adds an object whose history is to be loaded.
|
---|
98 | *
|
---|
99 | * @param pid the primitive id. Must not be null. Id > 0 required.
|
---|
100 | * @return this task
|
---|
101 | */
|
---|
102 | public HistoryLoadTask add(PrimitiveId pid) {
|
---|
103 | CheckParameterUtil.ensureValidPrimitiveId(pid, "pid");
|
---|
104 | toLoad.add(pid);
|
---|
105 | return this;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Adds an object to be loaded, the object is specified by a history item.
|
---|
110 | *
|
---|
111 | * @param primitive the history item
|
---|
112 | * @return this task
|
---|
113 | * @throws IllegalArgumentException if primitive is null
|
---|
114 | */
|
---|
115 | public HistoryLoadTask add(HistoryOsmPrimitive primitive) {
|
---|
116 | CheckParameterUtil.ensureParameterNotNull(primitive, "primitive");
|
---|
117 | toLoad.add(primitive.getPrimitiveId());
|
---|
118 | return this;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Adds an object to be loaded, the object is specified by an already loaded object history.
|
---|
123 | *
|
---|
124 | * @param history the history. Must not be null.
|
---|
125 | * @return this task
|
---|
126 | * @throws IllegalArgumentException if history is null
|
---|
127 | */
|
---|
128 | public HistoryLoadTask add(History history) {
|
---|
129 | CheckParameterUtil.ensureParameterNotNull(history, "history");
|
---|
130 | toLoad.add(history.getPrimitiveId());
|
---|
131 | return this;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Adds an object to be loaded, the object is specified by an OSM primitive.
|
---|
136 | *
|
---|
137 | * @param primitive the OSM primitive. Must not be null. primitive.getId() > 0 required.
|
---|
138 | * @return this task
|
---|
139 | * @throws IllegalArgumentException if the primitive is null
|
---|
140 | * @throws IllegalArgumentException if primitive.getId() <= 0
|
---|
141 | */
|
---|
142 | public HistoryLoadTask add(OsmPrimitive primitive) {
|
---|
143 | CheckParameterUtil.ensureValidPrimitiveId(primitive, "primitive");
|
---|
144 | toLoad.add(primitive.getPrimitiveId());
|
---|
145 | return this;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Adds a collection of objects to loaded, specified by a collection of OSM primitives.
|
---|
150 | *
|
---|
151 | * @param primitives the OSM primitives. Must not be <code>null</code>.
|
---|
152 | * <code>primitive.getId() > 0</code> required.
|
---|
153 | * @return this task
|
---|
154 | * @throws IllegalArgumentException if primitives is <code>null</code>
|
---|
155 | * @throws IllegalArgumentException if one of the ids in the collection <= 0
|
---|
156 | */
|
---|
157 | public HistoryLoadTask add(Collection<? extends OsmPrimitive> primitives) {
|
---|
158 | CheckParameterUtil.ensureParameterNotNull(primitives, "primitives");
|
---|
159 | for (OsmPrimitive primitive: primitives) {
|
---|
160 | if (primitive == null) {
|
---|
161 | continue;
|
---|
162 | }
|
---|
163 | add(primitive);
|
---|
164 | }
|
---|
165 | return this;
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | protected void cancel() {
|
---|
170 | if (reader != null) {
|
---|
171 | reader.cancel();
|
---|
172 | }
|
---|
173 | canceled = true;
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | protected void finish() {
|
---|
178 | if (isCanceled())
|
---|
179 | return;
|
---|
180 | if (lastException != null) {
|
---|
181 | ExceptionDialogUtil.explainException(lastException);
|
---|
182 | return;
|
---|
183 | }
|
---|
184 | HistoryDataSet.getInstance().mergeInto(loadedData);
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
189 | loadedData = new HistoryDataSet();
|
---|
190 | try {
|
---|
191 | progressMonitor.setTicksCount(toLoad.size());
|
---|
192 | for (PrimitiveId pid: toLoad) {
|
---|
193 | if (canceled) {
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | String msg = "";
|
---|
197 | switch(pid.getType()) {
|
---|
198 | case NODE: msg = marktr("Loading history for node {0}"); break;
|
---|
199 | case WAY: msg = marktr("Loading history for way {0}"); break;
|
---|
200 | case RELATION: msg = marktr("Loading history for relation {0}"); break;
|
---|
201 | }
|
---|
202 | progressMonitor.indeterminateSubTask(tr(msg,
|
---|
203 | Long.toString(pid.getUniqueId())));
|
---|
204 | reader = null;
|
---|
205 | HistoryDataSet ds = null;
|
---|
206 | try {
|
---|
207 | reader = new OsmServerHistoryReader(pid.getType(), pid.getUniqueId());
|
---|
208 | ds = reader.parseHistory(progressMonitor.createSubTaskMonitor(1, false));
|
---|
209 | // load corresponding changesets (mostly for changeset comment)
|
---|
210 | for (final Changeset i : new OsmServerChangesetReader().queryChangesets(
|
---|
211 | new ChangesetQuery().forChangesetIds(ds.getChangesetIds()), progressMonitor.createSubTaskMonitor(1, false))) {
|
---|
212 | ds.putChangeset(i);
|
---|
213 | }
|
---|
214 | } catch (OsmTransferException e) {
|
---|
215 | if (canceled)
|
---|
216 | return;
|
---|
217 | throw e;
|
---|
218 | }
|
---|
219 | loadedData.mergeInto(ds);
|
---|
220 | }
|
---|
221 | } catch (OsmTransferException e) {
|
---|
222 | lastException = e;
|
---|
223 | return;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | public boolean isCanceled() {
|
---|
228 | return canceled;
|
---|
229 | }
|
---|
230 |
|
---|
231 | public Exception getLastException() {
|
---|
232 | return lastException;
|
---|
233 | }
|
---|
234 | }
|
---|