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

Last change on this file since 5863 was 5863, checked in by stoecker, 11 years ago

javadoc fixes

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