[4668] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[4874] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[4668] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.io.File;
|
---|
[6245] | 9 | import java.io.FileOutputStream;
|
---|
[4668] | 10 | import java.io.IOException;
|
---|
[6245] | 11 | import java.io.InputStream;
|
---|
| 12 | import java.net.URI;
|
---|
[5573] | 13 | import java.util.Arrays;
|
---|
[4668] | 14 | import java.util.List;
|
---|
| 15 |
|
---|
| 16 | import javax.swing.JFileChooser;
|
---|
| 17 | import javax.swing.JOptionPane;
|
---|
| 18 | import javax.swing.SwingUtilities;
|
---|
| 19 |
|
---|
| 20 | import org.openstreetmap.josm.Main;
|
---|
| 21 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
[5670] | 22 | import org.openstreetmap.josm.gui.NavigatableComponent.ViewportData;
|
---|
[4668] | 23 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 24 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 25 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[5573] | 26 | import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
|
---|
[4668] | 27 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
[6245] | 28 | import org.openstreetmap.josm.io.session.SessionImporter;
|
---|
[4668] | 29 | import org.openstreetmap.josm.io.session.SessionReader;
|
---|
[6245] | 30 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 31 | import org.openstreetmap.josm.tools.Utils;
|
---|
[4668] | 32 |
|
---|
[6245] | 33 | /**
|
---|
| 34 | * Loads a JOSM session
|
---|
| 35 | * @since 4668
|
---|
| 36 | */
|
---|
[5438] | 37 | public class SessionLoadAction extends DiskAccessAction {
|
---|
[6069] | 38 |
|
---|
[6245] | 39 | /**
|
---|
| 40 | * Constructs a new {@code SessionLoadAction}.
|
---|
| 41 | */
|
---|
[4668] | 42 | public SessionLoadAction() {
|
---|
[4733] | 43 | super(tr("Load Session"), "open", tr("Load a session from file."), null, true, "load-session", true);
|
---|
[4668] | 44 | putValue("help", ht("/Action/SessionLoad"));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[6084] | 47 | @Override
|
---|
[4668] | 48 | public void actionPerformed(ActionEvent e) {
|
---|
[6245] | 49 | JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"),
|
---|
| 50 | Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()),
|
---|
| 51 | SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory");
|
---|
[5438] | 52 | if (fc == null) return;
|
---|
[4668] | 53 | File file = fc.getSelectedFile();
|
---|
[5438] | 54 | boolean zip = file.getName().toLowerCase().endsWith(".joz");
|
---|
[4668] | 55 | Main.worker.submit(new Loader(file, zip));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[6245] | 58 | /**
|
---|
| 59 | * JOSM session loader
|
---|
| 60 | */
|
---|
[4874] | 61 | public static class Loader extends PleaseWaitRunnable {
|
---|
[4668] | 62 |
|
---|
| 63 | private boolean canceled;
|
---|
| 64 | private File file;
|
---|
[6245] | 65 | private final URI uri;
|
---|
| 66 | private final InputStream is;
|
---|
| 67 | private final boolean zip;
|
---|
[4668] | 68 | private List<Layer> layers;
|
---|
[6271] | 69 | private Layer active;
|
---|
[4668] | 70 | private List<Runnable> postLoadTasks;
|
---|
[5670] | 71 | private ViewportData viewport;
|
---|
[4668] | 72 |
|
---|
[6245] | 73 | /**
|
---|
| 74 | * Constructs a new {@code Loader} for local session file.
|
---|
| 75 | * @param file The JOSM session file
|
---|
| 76 | * @param zip {@code true} if the file is a session archive file (*.joz)
|
---|
| 77 | */
|
---|
[4668] | 78 | public Loader(File file, boolean zip) {
|
---|
| 79 | super(tr("Loading session ''{0}''", file.getName()));
|
---|
[6245] | 80 | CheckParameterUtil.ensureParameterNotNull(file, "file");
|
---|
[4668] | 81 | this.file = file;
|
---|
[6245] | 82 | this.uri = null;
|
---|
| 83 | this.is = null;
|
---|
[4668] | 84 | this.zip = zip;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[6245] | 87 | /**
|
---|
| 88 | * Constructs a new {@code Loader} for session file input stream (may be a remote file).
|
---|
| 89 | * @param is The input stream to session file
|
---|
| 90 | * @param uri The file URI
|
---|
| 91 | * @param zip {@code true} if the file is a session archive file (*.joz)
|
---|
| 92 | * @since 6245
|
---|
| 93 | */
|
---|
| 94 | public Loader(InputStream is, URI uri, boolean zip) {
|
---|
| 95 | super(tr("Loading session ''{0}''", uri));
|
---|
| 96 | CheckParameterUtil.ensureParameterNotNull(is, "is");
|
---|
| 97 | CheckParameterUtil.ensureParameterNotNull(uri, "uri");
|
---|
| 98 | this.file = null;
|
---|
| 99 | this.uri = uri;
|
---|
| 100 | this.is = is;
|
---|
| 101 | this.zip = zip;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[4668] | 104 | @Override
|
---|
[6245] | 105 | public void cancel() {
|
---|
[5438] | 106 | Thread.dumpStack();
|
---|
[4668] | 107 | canceled = true;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | @Override
|
---|
| 111 | protected void finish() {
|
---|
| 112 | SwingUtilities.invokeLater(new Runnable() {
|
---|
| 113 | @Override
|
---|
| 114 | public void run() {
|
---|
| 115 | if (canceled) return;
|
---|
[5670] | 116 | if (!layers.isEmpty()) {
|
---|
| 117 | Layer firstLayer = layers.iterator().next();
|
---|
| 118 | boolean noMap = Main.map == null;
|
---|
| 119 | if (noMap) {
|
---|
| 120 | Main.main.createMapFrame(firstLayer, viewport);
|
---|
| 121 | }
|
---|
| 122 | for (Layer l : layers) {
|
---|
| 123 | if (canceled) return;
|
---|
| 124 | Main.main.addLayer(l);
|
---|
| 125 | }
|
---|
[6271] | 126 | if (active != null) {
|
---|
| 127 | Main.map.mapView.setActiveLayer(active);
|
---|
| 128 | }
|
---|
[5670] | 129 | if (noMap) {
|
---|
| 130 | Main.map.setVisible(true);
|
---|
| 131 | }
|
---|
[4668] | 132 | }
|
---|
| 133 | for (Runnable task : postLoadTasks) {
|
---|
| 134 | if (canceled) return;
|
---|
[4874] | 135 | if (task == null) {
|
---|
| 136 | continue;
|
---|
| 137 | }
|
---|
[4668] | 138 | task.run();
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | });
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | @Override
|
---|
| 145 | protected void realRun() {
|
---|
| 146 | try {
|
---|
| 147 | ProgressMonitor monitor = getProgressMonitor();
|
---|
| 148 | SessionReader reader = new SessionReader();
|
---|
[6245] | 149 | boolean tempFile = false;
|
---|
| 150 | try {
|
---|
| 151 | if (file == null) {
|
---|
| 152 | // Download and write entire joz file as a temp file on disk as we need random access later
|
---|
| 153 | file = File.createTempFile("session_", ".joz", Utils.getJosmTempDir());
|
---|
| 154 | tempFile = true;
|
---|
| 155 | FileOutputStream out = new FileOutputStream(file);
|
---|
| 156 | try {
|
---|
| 157 | Utils.copyStream(is, out);
|
---|
| 158 | } finally {
|
---|
| 159 | Utils.close(out);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | reader.loadSession(file, zip, monitor);
|
---|
| 163 | layers = reader.getLayers();
|
---|
[6271] | 164 | active = reader.getActive();
|
---|
[6245] | 165 | postLoadTasks = reader.getPostLoadTasks();
|
---|
| 166 | viewport = reader.getViewport();
|
---|
| 167 | } finally {
|
---|
| 168 | if (tempFile) {
|
---|
| 169 | if (!file.delete()) {
|
---|
| 170 | file.deleteOnExit();
|
---|
| 171 | }
|
---|
| 172 | file = null;
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
[4668] | 175 | } catch (IllegalDataException e) {
|
---|
| 176 | e.printStackTrace();
|
---|
| 177 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
| 178 | Main.parent,
|
---|
[6245] | 179 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", uri != null ? uri : file.getName(), e.getMessage()),
|
---|
[4668] | 180 | tr("Data Error"),
|
---|
| 181 | JOptionPane.ERROR_MESSAGE,
|
---|
| 182 | null
|
---|
[4874] | 183 | );
|
---|
[4668] | 184 | cancel();
|
---|
| 185 | } catch (IOException e) {
|
---|
| 186 | e.printStackTrace();
|
---|
| 187 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
| 188 | Main.parent,
|
---|
[6245] | 189 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", uri != null ? uri : file.getName(), e.getMessage()),
|
---|
[4668] | 190 | tr("IO Error"),
|
---|
| 191 | JOptionPane.ERROR_MESSAGE,
|
---|
| 192 | null
|
---|
[4874] | 193 | );
|
---|
[4668] | 194 | cancel();
|
---|
| 195 | } catch (RuntimeException e) {
|
---|
| 196 | cancel();
|
---|
| 197 | throw e;
|
---|
[4695] | 198 | } catch (Error e) {
|
---|
[4668] | 199 | cancel();
|
---|
[4695] | 200 | throw e;
|
---|
[4668] | 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|