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.gui.HelpAwareOptionPane;
|
---|
23 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
24 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
25 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
27 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
28 | import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
30 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
31 | import org.openstreetmap.josm.io.session.SessionImporter;
|
---|
32 | import org.openstreetmap.josm.io.session.SessionReader;
|
---|
33 | import org.openstreetmap.josm.io.session.SessionReader.SessionProjectionChoiceData;
|
---|
34 | import org.openstreetmap.josm.io.session.SessionReader.SessionViewportData;
|
---|
35 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
36 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
37 | import org.openstreetmap.josm.tools.Logging;
|
---|
38 | import org.openstreetmap.josm.tools.Utils;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Loads a JOSM session.
|
---|
42 | * @since 4668
|
---|
43 | */
|
---|
44 | public class SessionLoadAction extends DiskAccessAction {
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructs a new {@code SessionLoadAction}.
|
---|
48 | */
|
---|
49 | public SessionLoadAction() {
|
---|
50 | super(tr("Load Session"), "open", tr("Load a session from file."), null, true, "load-session", true);
|
---|
51 | putValue("help", ht("/Action/SessionLoad"));
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void actionPerformed(ActionEvent e) {
|
---|
56 | AbstractFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"),
|
---|
57 | Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()),
|
---|
58 | SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory");
|
---|
59 | if (fc == null)
|
---|
60 | return;
|
---|
61 | File file = fc.getSelectedFile();
|
---|
62 | boolean zip = Utils.hasExtension(file, "joz");
|
---|
63 | MainApplication.worker.submit(new Loader(file, zip));
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * JOSM session loader
|
---|
68 | */
|
---|
69 | public static class Loader extends PleaseWaitRunnable {
|
---|
70 |
|
---|
71 | private boolean canceled;
|
---|
72 | private File file;
|
---|
73 | private final URI uri;
|
---|
74 | private final InputStream is;
|
---|
75 | private final boolean zip;
|
---|
76 | private List<Layer> layers;
|
---|
77 | private Layer active;
|
---|
78 | private List<Runnable> postLoadTasks;
|
---|
79 | private SessionViewportData viewport;
|
---|
80 | private SessionProjectionChoiceData projectionChoice;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Constructs a new {@code Loader} for local session file.
|
---|
84 | * @param file The JOSM session file
|
---|
85 | * @param zip {@code true} if the file is a session archive file (*.joz)
|
---|
86 | */
|
---|
87 | public Loader(File file, boolean zip) {
|
---|
88 | super(tr("Loading session ''{0}''", file.getName()));
|
---|
89 | CheckParameterUtil.ensureParameterNotNull(file, "file");
|
---|
90 | this.file = file;
|
---|
91 | this.uri = null;
|
---|
92 | this.is = null;
|
---|
93 | this.zip = zip;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Constructs a new {@code Loader} for session file input stream (may be a remote file).
|
---|
98 | * @param is The input stream to session file
|
---|
99 | * @param uri The file URI
|
---|
100 | * @param zip {@code true} if the file is a session archive file (*.joz)
|
---|
101 | * @since 6245
|
---|
102 | */
|
---|
103 | public Loader(InputStream is, URI uri, boolean zip) {
|
---|
104 | super(tr("Loading session ''{0}''", uri));
|
---|
105 | CheckParameterUtil.ensureParameterNotNull(is, "is");
|
---|
106 | CheckParameterUtil.ensureParameterNotNull(uri, "uri");
|
---|
107 | this.file = null;
|
---|
108 | this.uri = uri;
|
---|
109 | this.is = is;
|
---|
110 | this.zip = zip;
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public void cancel() {
|
---|
115 | canceled = true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | protected void finish() {
|
---|
120 | SwingUtilities.invokeLater(() -> {
|
---|
121 | if (canceled)
|
---|
122 | return;
|
---|
123 | if (projectionChoice != null) {
|
---|
124 | ProjectionPreference.setProjection(
|
---|
125 | projectionChoice.getProjectionChoiceId(),
|
---|
126 | projectionChoice.getSubPreferences(),
|
---|
127 | false);
|
---|
128 | }
|
---|
129 | addLayers();
|
---|
130 | runPostLoadTasks();
|
---|
131 | });
|
---|
132 | }
|
---|
133 |
|
---|
134 | private void addLayers() {
|
---|
135 | if (layers != null && !layers.isEmpty()) {
|
---|
136 | boolean noMap = MainApplication.getMap() == null;
|
---|
137 | for (Layer l : layers) {
|
---|
138 | if (canceled)
|
---|
139 | return;
|
---|
140 | // NoteImporter directly loads notes into current note layer
|
---|
141 | if (!MainApplication.getLayerManager().containsLayer(l)) {
|
---|
142 | MainApplication.getLayerManager().addLayer(l);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | if (active != null) {
|
---|
146 | MainApplication.getLayerManager().setActiveLayer(active);
|
---|
147 | }
|
---|
148 | if (noMap && viewport != null) {
|
---|
149 | MainApplication.getMap().mapView.scheduleZoomTo(viewport.getEastNorthViewport(Main.getProjection()));
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void runPostLoadTasks() {
|
---|
155 | if (postLoadTasks != null) {
|
---|
156 | for (Runnable task : postLoadTasks) {
|
---|
157 | if (canceled)
|
---|
158 | return;
|
---|
159 | if (task == null) {
|
---|
160 | continue;
|
---|
161 | }
|
---|
162 | task.run();
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Override
|
---|
168 | protected void realRun() {
|
---|
169 | try {
|
---|
170 | ProgressMonitor monitor = getProgressMonitor();
|
---|
171 | SessionReader reader = new SessionReader();
|
---|
172 | boolean tempFile = false;
|
---|
173 | try {
|
---|
174 | if (file == null) {
|
---|
175 | // Download and write entire joz file as a temp file on disk as we need random access later
|
---|
176 | file = File.createTempFile("session_", ".joz", Utils.getJosmTempDir());
|
---|
177 | tempFile = true;
|
---|
178 | Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
---|
179 | }
|
---|
180 | reader.loadSession(file, zip, monitor);
|
---|
181 | layers = reader.getLayers();
|
---|
182 | active = reader.getActive();
|
---|
183 | postLoadTasks = reader.getPostLoadTasks();
|
---|
184 | viewport = reader.getViewport();
|
---|
185 | projectionChoice = reader.getProjectionChoice();
|
---|
186 | } finally {
|
---|
187 | if (tempFile) {
|
---|
188 | Utils.deleteFile(file);
|
---|
189 | file = null;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | } catch (IllegalDataException e) {
|
---|
193 | handleException(tr("Data Error"), e);
|
---|
194 | } catch (IOException e) {
|
---|
195 | handleException(tr("IO Error"), e);
|
---|
196 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
197 | cancel();
|
---|
198 | throw e;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void handleException(String dialogTitle, Exception e) {
|
---|
203 | Logging.error(e);
|
---|
204 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
205 | Main.parent,
|
---|
206 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>",
|
---|
207 | uri != null ? uri : file.getName(), Utils.escapeReservedCharactersHTML(e.getMessage())),
|
---|
208 | dialogTitle,
|
---|
209 | JOptionPane.ERROR_MESSAGE,
|
---|
210 | null
|
---|
211 | );
|
---|
212 | cancel();
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|