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