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

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

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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