source: josm/trunk/src/org/openstreetmap/josm/actions/SessionLoadAction.java@ 13777

Last change on this file since 13777 was 13437, checked in by Don-vip, 6 years ago

fix #15967 - proper loading of notes layers

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.io.File;
9import java.io.IOException;
10import java.io.InputStream;
11import java.net.URI;
12import java.nio.file.Files;
13import java.nio.file.StandardCopyOption;
14import java.util.Arrays;
15import java.util.List;
16
17import javax.swing.JFileChooser;
18import javax.swing.JOptionPane;
19import javax.swing.SwingUtilities;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.gui.HelpAwareOptionPane;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.PleaseWaitRunnable;
25import org.openstreetmap.josm.gui.layer.Layer;
26import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
27import org.openstreetmap.josm.gui.progress.ProgressMonitor;
28import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
29import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
30import org.openstreetmap.josm.io.IllegalDataException;
31import org.openstreetmap.josm.io.session.SessionImporter;
32import org.openstreetmap.josm.io.session.SessionReader;
33import org.openstreetmap.josm.io.session.SessionReader.SessionProjectionChoiceData;
34import org.openstreetmap.josm.io.session.SessionReader.SessionViewportData;
35import org.openstreetmap.josm.tools.CheckParameterUtil;
36import org.openstreetmap.josm.tools.JosmRuntimeException;
37import org.openstreetmap.josm.tools.Logging;
38import org.openstreetmap.josm.tools.Utils;
39
40/**
41 * Loads a JOSM session.
42 * @since 4668
43 */
44public 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}
Note: See TracBrowser for help on using the repository browser.