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

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

see #15229 - deprecate all Main methods related to projections. New ProjectionRegistry class

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