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

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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