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

Last change on this file since 6827 was 6440, checked in by simon04, 10 years ago

Load and display changeset comment in history dialog

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