source: josm/trunk/src/org/openstreetmap/josm/gui/io/SaveLayerTask.java@ 14010

Last change on this file since 14010 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Optional;
7
8import org.openstreetmap.josm.actions.SaveAction;
9import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
10import org.openstreetmap.josm.gui.progress.ProgressMonitor;
11import org.openstreetmap.josm.tools.CheckParameterUtil;
12import org.openstreetmap.josm.tools.JosmRuntimeException;
13import org.openstreetmap.josm.tools.Logging;
14
15/**
16 * SaveLayerTask saves the data managed by an {@link org.openstreetmap.josm.gui.layer.AbstractModifiableLayer} to the
17 * {@link org.openstreetmap.josm.gui.layer.Layer#getAssociatedFile()}.
18 *
19 * <pre>
20 * ExecutorService executorService = ...
21 * SaveLayerTask task = new SaveLayerTask(layer, monitor);
22 * Future&lt;?&gt; taskFuture = executorService.submit(task)
23 * try {
24 * // wait for the task to complete
25 * taskFuture.get();
26 * } catch (Exception e) {
27 * e.printStackTrace();
28 * }
29 * </pre>
30 */
31public class SaveLayerTask extends AbstractIOTask {
32 private final SaveLayerInfo layerInfo;
33 private final ProgressMonitor parentMonitor;
34
35 /**
36 *
37 * @param layerInfo information about the layer to be saved to save. Must not be null.
38 * @param monitor the monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
39 * @throws IllegalArgumentException if layer is null
40 */
41 protected SaveLayerTask(SaveLayerInfo layerInfo, ProgressMonitor monitor) {
42 CheckParameterUtil.ensureParameterNotNull(layerInfo, "layerInfo");
43 this.layerInfo = layerInfo;
44 this.parentMonitor = Optional.ofNullable(monitor).orElse(NullProgressMonitor.INSTANCE);
45 }
46
47 @Override
48 public void run() {
49 try {
50 parentMonitor.subTask(tr("Saving layer to ''{0}'' ...", layerInfo.getFile().toString()));
51 if (!SaveAction.doSave(layerInfo.getLayer(), layerInfo.getFile(), layerInfo.isDoCheckSaveConditions())) {
52 setFailed(true);
53 return;
54 }
55 if (!isCanceled()) {
56 layerInfo.getLayer().onPostSaveToFile();
57 }
58 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
59 Logging.error(e);
60 setLastException(e);
61 }
62 }
63
64 @Override
65 public void cancel() {
66 setCanceled(true);
67 }
68}
Note: See TracBrowser for help on using the repository browser.