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