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

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

new: rewrite of CombineWay action
new: conflict resolution dialog for CombineWay, including conflicts for different relation memberships
cleanup: cleanup in OsmReader, reduces memory footprint and reduces parsing time
cleanup: made most of the public fields in OsmPrimitive @deprecated, added accessors and changed the code
cleanup: replaced usages of @deprecated constructors for ExtendedDialog
fixed #3208: Combine ways brokes relation order

WARNING: this changeset touches a lot of code all over the code base. "latest" might become slightly unstable in the next days. Also experience incompatibility issues with plugins in the next few days.

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