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

Last change on this file since 5050 was 4310, checked in by stoecker, 13 years ago

fix #6680, fix #6677 - i18n issues

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