1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
14 | import org.openstreetmap.josm.data.osm.history.History;
|
---|
15 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
19 | import org.openstreetmap.josm.io.OsmApi;
|
---|
20 | import org.openstreetmap.josm.io.OsmServerHistoryReader;
|
---|
21 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
22 | import org.xml.sax.SAXException;
|
---|
23 |
|
---|
24 | public class HistoryLoadTask extends PleaseWaitRunnable {
|
---|
25 |
|
---|
26 | private boolean cancelled = false;
|
---|
27 | private Exception lastException = null;
|
---|
28 | private Map<Long, OsmPrimitiveType> toLoad;
|
---|
29 | private HistoryDataSet loadedData;
|
---|
30 |
|
---|
31 | public HistoryLoadTask() {
|
---|
32 | super(tr("Load history"), true);
|
---|
33 | toLoad = new HashMap<Long, OsmPrimitiveType>();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public HistoryLoadTask add(long id, OsmPrimitiveType type) {
|
---|
37 | if (id <= 0)
|
---|
38 | throw new IllegalArgumentException(tr("ID > 0 expected. Got {0}.", id));
|
---|
39 | if (type == null)
|
---|
40 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "type"));
|
---|
41 | if (!toLoad.containsKey(id)) {
|
---|
42 | toLoad.put(id, type);
|
---|
43 | }
|
---|
44 | return this;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public HistoryLoadTask add(HistoryOsmPrimitive primitive) {
|
---|
48 | if (primitive == null)
|
---|
49 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "primitive"));
|
---|
50 | if (!toLoad.containsKey(primitive.getId())) {
|
---|
51 | toLoad.put(primitive.getId(), primitive.getType());
|
---|
52 | }
|
---|
53 | return this;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public HistoryLoadTask add(History history) {
|
---|
57 | if (history == null)
|
---|
58 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "history"));
|
---|
59 | if (!toLoad.containsKey(history.getId())) {
|
---|
60 | toLoad.put(history.getId(), history.getEarliest().getType());
|
---|
61 | }
|
---|
62 | return this;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public HistoryLoadTask add(OsmPrimitive primitive) {
|
---|
66 | if (primitive == null)
|
---|
67 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "primitive"));
|
---|
68 | return add(primitive.getId(), OsmPrimitiveType.from(primitive));
|
---|
69 | }
|
---|
70 |
|
---|
71 | public HistoryLoadTask add(Collection<? extends OsmPrimitive> primitives) {
|
---|
72 | if (primitives == null)
|
---|
73 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "primitives"));
|
---|
74 | for (OsmPrimitive primitive: primitives) {
|
---|
75 | if (primitive == null) {
|
---|
76 | continue;
|
---|
77 | }
|
---|
78 | add(primitive);
|
---|
79 | }
|
---|
80 | return this;
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | protected void cancel() {
|
---|
85 | OsmApi.getOsmApi().cancel();
|
---|
86 | cancelled = true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | protected void finish() {
|
---|
91 | if (isCancelled())
|
---|
92 | return;
|
---|
93 | if (lastException != null) {
|
---|
94 | ExceptionDialogUtil.explainException(lastException);
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | HistoryDataSet.getInstance().mergeInto(loadedData);
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
102 | loadedData = new HistoryDataSet();
|
---|
103 | try {
|
---|
104 | for(Map.Entry<Long, OsmPrimitiveType> entry: toLoad.entrySet()) {
|
---|
105 | if (cancelled) {
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | if (entry.getKey() == 0) {
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 | String msg = "";
|
---|
112 | switch(entry.getValue()) {
|
---|
113 | case NODE: msg = marktr("Loading history for node {0}"); break;
|
---|
114 | case WAY: msg = marktr("Loading history for way {0}"); break;
|
---|
115 | case RELATION: msg = marktr("Loading history for relation {0}"); break;
|
---|
116 | }
|
---|
117 | progressMonitor.indeterminateSubTask(tr(msg,
|
---|
118 | Long.toString(entry.getKey())));
|
---|
119 | OsmServerHistoryReader reader = null;
|
---|
120 | HistoryDataSet ds = null;
|
---|
121 | try {
|
---|
122 | reader = new OsmServerHistoryReader(entry.getValue(), entry.getKey());
|
---|
123 | ds = reader.parseHistory(progressMonitor.createSubTaskMonitor(1, false));
|
---|
124 | } catch(OsmTransferException e) {
|
---|
125 | if (cancelled)
|
---|
126 | return;
|
---|
127 | throw e;
|
---|
128 | }
|
---|
129 | loadedData.mergeInto(ds);
|
---|
130 | }
|
---|
131 | } catch(OsmTransferException e) {
|
---|
132 | lastException = e;
|
---|
133 | return;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public boolean isCancelled() {
|
---|
138 | return cancelled;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public Exception getLastException() {
|
---|
142 | return lastException;
|
---|
143 | }
|
---|
144 | }
|
---|