source: josm/trunk/src/org/openstreetmap/josm/gui/history/HistoryLoadTask.java@ 9982

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

sonar - Immutable Field

  • Property svn:eol-style set to native
File size: 7.9 KB
RevLine 
[2512]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
[2689]7import java.awt.Component;
[2512]8import java.io.IOException;
[2850]9import java.text.MessageFormat;
[2512]10import java.util.Collection;
11import java.util.HashSet;
[6316]12import java.util.Set;
[2512]13
[6440]14import org.openstreetmap.josm.data.osm.Changeset;
[2512]15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
17import org.openstreetmap.josm.data.osm.PrimitiveId;
18import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
19import org.openstreetmap.josm.data.osm.history.History;
20import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
21import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
22import org.openstreetmap.josm.gui.ExceptionDialogUtil;
23import org.openstreetmap.josm.gui.PleaseWaitRunnable;
[6440]24import org.openstreetmap.josm.io.ChangesetQuery;
25import org.openstreetmap.josm.io.OsmServerChangesetReader;
[2512]26import org.openstreetmap.josm.io.OsmServerHistoryReader;
27import org.openstreetmap.josm.io.OsmTransferException;
[2689]28import org.openstreetmap.josm.tools.CheckParameterUtil;
[2512]29import org.xml.sax.SAXException;
30
31/**
[2711]32 * Loads the object history of an collection of objects from the
[2512]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 */
50public class HistoryLoadTask extends PleaseWaitRunnable {
51
[8840]52 private boolean canceled;
53 private Exception lastException;
[9078]54 private final Set<PrimitiveId> toLoad;
[2512]55 private HistoryDataSet loadedData;
[8840]56 private OsmServerHistoryReader reader;
[2512]57
[6830]58 /**
59 * Constructs a new {@code HistoryLoadTask}.
60 */
[2512]61 public HistoryLoadTask() {
62 super(tr("Load history"), true);
[7005]63 toLoad = new HashSet<>();
[2512]64 }
65
66 /**
[6830]67 * Constructs a new {@code HistoryLoadTask}.
[2711]68 *
[5863]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>.
[8291]72 * @throws IllegalArgumentException if parent is <code>null</code>
[2689]73 */
74 public HistoryLoadTask(Component parent) {
75 super(parent, tr("Load history"), true);
76 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
[7005]77 toLoad = new HashSet<>();
[2689]78 }
79
80 /**
[2512]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 */
[8291]87 public HistoryLoadTask add(long id, OsmPrimitiveType type) {
[2512]88 if (id <= 0)
[2850]89 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got {1}.", "id", id));
90 CheckParameterUtil.ensureParameterNotNull(type, "type");
[2512]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 *
[6830]99 * @param pid the primitive id. Must not be null. Id &gt; 0 required.
[2512]100 * @return this task
101 */
[2850]102 public HistoryLoadTask add(PrimitiveId pid) {
103 CheckParameterUtil.ensureValidPrimitiveId(pid, "pid");
[2512]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
[8291]113 * @throws IllegalArgumentException if primitive is null
[2512]114 */
[2850]115 public HistoryLoadTask add(HistoryOsmPrimitive primitive) {
116 CheckParameterUtil.ensureParameterNotNull(primitive, "primitive");
[2512]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
[8291]126 * @throws IllegalArgumentException if history is null
[2512]127 */
[2850]128 public HistoryLoadTask add(History history) {
129 CheckParameterUtil.ensureParameterNotNull(history, "history");
[5171]130 toLoad.add(history.getPrimitiveId());
[2512]131 return this;
132 }
133
134 /**
135 * Adds an object to be loaded, the object is specified by an OSM primitive.
136 *
[6830]137 * @param primitive the OSM primitive. Must not be null. primitive.getId() &gt; 0 required.
[2512]138 * @return this task
[8291]139 * @throws IllegalArgumentException if the primitive is null
140 * @throws IllegalArgumentException if primitive.getId() &lt;= 0
[2512]141 */
[2850]142 public HistoryLoadTask add(OsmPrimitive primitive) {
143 CheckParameterUtil.ensureValidPrimitiveId(primitive, "primitive");
[2512]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 *
[5863]151 * @param primitives the OSM primitives. Must not be <code>null</code>.
[6830]152 * <code>primitive.getId() &gt; 0</code> required.
[2512]153 * @return this task
[8291]154 * @throws IllegalArgumentException if primitives is <code>null</code>
155 * @throws IllegalArgumentException if one of the ids in the collection &lt;= 0
[2512]156 */
[2850]157 public HistoryLoadTask add(Collection<? extends OsmPrimitive> primitives) {
158 CheckParameterUtil.ensureParameterNotNull(primitives, "primitives");
[2512]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() {
[5863]170 if (reader != null) {
171 reader.cancel();
172 }
[4310]173 canceled = true;
[2512]174 }
175
176 @Override
177 protected void finish() {
[4310]178 if (isCanceled())
[2512]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 {
[5476]191 progressMonitor.setTicksCount(toLoad.size());
[8510]192 for (PrimitiveId pid: toLoad) {
[4310]193 if (canceled) {
[2512]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())));
[5856]204 reader = null;
[2512]205 HistoryDataSet ds = null;
206 try {
207 reader = new OsmServerHistoryReader(pid.getType(), pid.getUniqueId());
208 ds = reader.parseHistory(progressMonitor.createSubTaskMonitor(1, false));
[6440]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 }
[8510]214 } catch (OsmTransferException e) {
[4310]215 if (canceled)
[2512]216 return;
217 throw e;
218 }
219 loadedData.mergeInto(ds);
220 }
[8510]221 } catch (OsmTransferException e) {
[2512]222 lastException = e;
223 return;
224 }
225 }
226
[4310]227 public boolean isCanceled() {
228 return canceled;
[2512]229 }
230
231 public Exception getLastException() {
232 return lastException;
233 }
234}
Note: See TracBrowser for help on using the repository browser.