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

Last change on this file since 5926 was 5926, checked in by bastiK, 11 years ago

clean up imports

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