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

Last change on this file since 2689 was 2689, checked in by Gubaer, 17 years ago

new: Changeset Cache Manager for querying, downloading, browsing, and managing changesets within JOSM. See also Changeset Manager and Changeset Query Dialog

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.util.Collection;
10import java.util.HashSet;
11
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
14import org.openstreetmap.josm.data.osm.PrimitiveId;
15import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
16import org.openstreetmap.josm.data.osm.history.History;
17import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
18import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.PleaseWaitRunnable;
21import org.openstreetmap.josm.io.OsmApi;
22import org.openstreetmap.josm.io.OsmServerHistoryReader;
23import org.openstreetmap.josm.io.OsmTransferException;
24import org.openstreetmap.josm.tools.CheckParameterUtil;
25import org.xml.sax.SAXException;
26
27/**
28 * Loads the the object history of an collection of objects from the
29 * server.
30 *
31 * It provides a fluent API for configuration.
32 *
33 * Sample usage:
34 *
35 * <pre>
36 * HistoryLoadTask task = new HistoryLoadTask()
37 * .add(1, OsmPrimitiveType.NODE)
38 * .add(1233, OsmPrimitiveType.WAY)
39 * .add(37234, OsmPrimitveType.RELATION)
40 * .add(aHistoryItem);
41 *
42 * Main.worker.execute(task);
43 *
44 * </pre>
45 */
46public class HistoryLoadTask extends PleaseWaitRunnable {
47
48 private boolean cancelled = false;
49 private Exception lastException = null;
50 private HashSet<PrimitiveId> toLoad;
51 private HistoryDataSet loadedData;
52
53 public HistoryLoadTask() {
54 super(tr("Load history"), true);
55 toLoad = new HashSet<PrimitiveId>();
56 }
57
58 /**
59 * Creates a new task
60 *
61 * @param parent the component to be used as reference to find the parent for {@see PleaseWaitDialog}.
62 * Must not be null.
63 * @throws IllegalArgumentException thrown if parent is null
64 */
65 public HistoryLoadTask(Component parent) {
66 super(parent, tr("Load history"), true);
67 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
68 toLoad = new HashSet<PrimitiveId>();
69 }
70
71 /**
72 * Adds an object whose history is to be loaded.
73 *
74 * @param id the object id
75 * @param type the object type
76 * @return this task
77 */
78 public HistoryLoadTask add(long id, OsmPrimitiveType type) throws IllegalArgumentException {
79 if (id <= 0)
80 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected. Got {1}.", "id", id));
81 if (type == null)
82 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "type"));
83 SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
84 toLoad.add(pid);
85 return this;
86 }
87
88 /**
89 * Adds an object whose history is to be loaded.
90 *
91 * @param pid the primitive id. Must not be null. Id > 0 required.
92 * @return this task
93 */
94 public HistoryLoadTask add(PrimitiveId pid) throws IllegalArgumentException {
95 if (pid == null)
96 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "pid"));
97 if (pid.getUniqueId() <= 0)
98 throw new IllegalArgumentException(tr("id in parameter ''{0}'' > 0 expected, got {1}.", "pid", pid.getUniqueId()));
99 toLoad.add(pid);
100 return this;
101 }
102
103 /**
104 * Adds an object to be loaded, the object is specified by a history item.
105 *
106 * @param primitive the history item
107 * @return this task
108 * @throws IllegalArgumentException thrown if primitive is null
109 */
110 public HistoryLoadTask add(HistoryOsmPrimitive primitive) throws IllegalArgumentException {
111 if (primitive == null)
112 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "primitive"));
113 toLoad.add(primitive.getPrimitiveId());
114 return this;
115 }
116
117 /**
118 * Adds an object to be loaded, the object is specified by an already loaded object history.
119 *
120 * @param history the history. Must not be null.
121 * @return this task
122 * @throws IllegalArgumentException thrown if history is null
123 */
124 public HistoryLoadTask add(History history)throws IllegalArgumentException {
125 if (history == null)
126 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "history"));
127 toLoad.add(history.getPrimitmiveId());
128 return this;
129 }
130
131 /**
132 * Adds an object to be loaded, the object is specified by an OSM primitive.
133 *
134 * @param primitive the OSM primitive. Must not be null. primitive.getId() > 0 required.
135 * @return this task
136 * @throws IllegalArgumentException thrown if the primitive is null
137 * @throws IllegalArgumentException thrown if primitive.getId() <= 0
138 */
139 public HistoryLoadTask add(OsmPrimitive primitive) throws IllegalArgumentException {
140 if (primitive == null)
141 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "primitive"));
142 if (primitive.getId() <= 0)
143 throw new IllegalArgumentException(tr("Object id > 0 expected. Got {0}", primitive.getId()));
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 primitive the OSM primitive. Must not be null. primitive.getId() > 0 required.
152 * @return this task
153 * @throws IllegalArgumentException thrown if primitives is null
154 * @throws IllegalArgumentException thrown if one of the ids in the collection <= 0
155 */
156 public HistoryLoadTask add(Collection<? extends OsmPrimitive> primitives) throws IllegalArgumentException{
157 if (primitives == null)
158 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "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 OsmApi.getOsmApi().cancel();
171 cancelled = true;
172 }
173
174 @Override
175 protected void finish() {
176 if (isCancelled())
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 for(PrimitiveId pid: toLoad) {
190 if (cancelled) {
191 break;
192 }
193 String msg = "";
194 switch(pid.getType()) {
195 case NODE: msg = marktr("Loading history for node {0}"); break;
196 case WAY: msg = marktr("Loading history for way {0}"); break;
197 case RELATION: msg = marktr("Loading history for relation {0}"); break;
198 }
199 progressMonitor.indeterminateSubTask(tr(msg,
200 Long.toString(pid.getUniqueId())));
201 OsmServerHistoryReader reader = null;
202 HistoryDataSet ds = null;
203 try {
204 reader = new OsmServerHistoryReader(pid.getType(), pid.getUniqueId());
205 ds = reader.parseHistory(progressMonitor.createSubTaskMonitor(1, false));
206 } catch(OsmTransferException e) {
207 if (cancelled)
208 return;
209 throw e;
210 }
211 loadedData.mergeInto(ds);
212 }
213 } catch(OsmTransferException e) {
214 lastException = e;
215 return;
216 }
217 }
218
219 public boolean isCancelled() {
220 return cancelled;
221 }
222
223 public Exception getLastException() {
224 return lastException;
225 }
226}
Note: See TracBrowser for help on using the repository browser.