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

Last change on this file since 2275 was 2275, checked in by Gubaer, 15 years ago

fixed #3694: History dialog for relations does not work

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