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.FileOutputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.net.URI;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import javax.swing.JFileChooser;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.SwingUtilities;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.ViewportData;
|
---|
22 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
23 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
24 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
25 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
26 | import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
28 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
29 | import org.openstreetmap.josm.io.session.SessionImporter;
|
---|
30 | import org.openstreetmap.josm.io.session.SessionReader;
|
---|
31 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Loads a JOSM session.
|
---|
36 | * @since 4668
|
---|
37 | */
|
---|
38 | public class SessionLoadAction extends DiskAccessAction {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code SessionLoadAction}.
|
---|
42 | */
|
---|
43 | public SessionLoadAction() {
|
---|
44 | super(tr("Load Session"), "open", tr("Load a session from file."), null, true, "load-session", true);
|
---|
45 | putValue("help", ht("/Action/SessionLoad"));
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void actionPerformed(ActionEvent e) {
|
---|
50 | AbstractFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"),
|
---|
51 | Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()),
|
---|
52 | SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory");
|
---|
53 | if (fc == null) return;
|
---|
54 | File file = fc.getSelectedFile();
|
---|
55 | boolean zip = file.getName().toLowerCase().endsWith(".joz");
|
---|
56 | Main.worker.submit(new Loader(file, zip));
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * JOSM session loader
|
---|
61 | */
|
---|
62 | public static class Loader extends PleaseWaitRunnable {
|
---|
63 |
|
---|
64 | private boolean canceled;
|
---|
65 | private File file;
|
---|
66 | private final URI uri;
|
---|
67 | private final InputStream is;
|
---|
68 | private final boolean zip;
|
---|
69 | private List<Layer> layers;
|
---|
70 | private Layer active;
|
---|
71 | private List<Runnable> postLoadTasks;
|
---|
72 | private ViewportData viewport;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Constructs a new {@code Loader} for local session file.
|
---|
76 | * @param file The JOSM session file
|
---|
77 | * @param zip {@code true} if the file is a session archive file (*.joz)
|
---|
78 | */
|
---|
79 | public Loader(File file, boolean zip) {
|
---|
80 | super(tr("Loading session ''{0}''", file.getName()));
|
---|
81 | CheckParameterUtil.ensureParameterNotNull(file, "file");
|
---|
82 | this.file = file;
|
---|
83 | this.uri = null;
|
---|
84 | this.is = null;
|
---|
85 | this.zip = zip;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Constructs a new {@code Loader} for session file input stream (may be a remote file).
|
---|
90 | * @param is The input stream to session file
|
---|
91 | * @param uri The file URI
|
---|
92 | * @param zip {@code true} if the file is a session archive file (*.joz)
|
---|
93 | * @since 6245
|
---|
94 | */
|
---|
95 | public Loader(InputStream is, URI uri, boolean zip) {
|
---|
96 | super(tr("Loading session ''{0}''", uri));
|
---|
97 | CheckParameterUtil.ensureParameterNotNull(is, "is");
|
---|
98 | CheckParameterUtil.ensureParameterNotNull(uri, "uri");
|
---|
99 | this.file = null;
|
---|
100 | this.uri = uri;
|
---|
101 | this.is = is;
|
---|
102 | this.zip = zip;
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public void cancel() {
|
---|
107 | canceled = true;
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | protected void finish() {
|
---|
112 | SwingUtilities.invokeLater(new Runnable() {
|
---|
113 | @Override
|
---|
114 | public void run() {
|
---|
115 | if (canceled) return;
|
---|
116 | if (!layers.isEmpty()) {
|
---|
117 | Layer firstLayer = layers.iterator().next();
|
---|
118 | boolean noMap = Main.map == null;
|
---|
119 | if (noMap) {
|
---|
120 | Main.main.createMapFrame(firstLayer, viewport);
|
---|
121 | }
|
---|
122 | for (Layer l : layers) {
|
---|
123 | if (canceled) return;
|
---|
124 | Main.main.addLayer(l);
|
---|
125 | }
|
---|
126 | if (active != null) {
|
---|
127 | Main.map.mapView.setActiveLayer(active);
|
---|
128 | }
|
---|
129 | if (noMap) {
|
---|
130 | Main.map.setVisible(true);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | for (Runnable task : postLoadTasks) {
|
---|
134 | if (canceled) return;
|
---|
135 | if (task == null) {
|
---|
136 | continue;
|
---|
137 | }
|
---|
138 | task.run();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | });
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | protected void realRun() {
|
---|
146 | try {
|
---|
147 | ProgressMonitor monitor = getProgressMonitor();
|
---|
148 | SessionReader reader = new SessionReader();
|
---|
149 | boolean tempFile = false;
|
---|
150 | try {
|
---|
151 | if (file == null) {
|
---|
152 | // Download and write entire joz file as a temp file on disk as we need random access later
|
---|
153 | file = File.createTempFile("session_", ".joz", Utils.getJosmTempDir());
|
---|
154 | tempFile = true;
|
---|
155 | try (FileOutputStream out = new FileOutputStream(file)) {
|
---|
156 | Utils.copyStream(is, out);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | reader.loadSession(file, zip, monitor);
|
---|
160 | layers = reader.getLayers();
|
---|
161 | active = reader.getActive();
|
---|
162 | postLoadTasks = reader.getPostLoadTasks();
|
---|
163 | viewport = reader.getViewport();
|
---|
164 | } finally {
|
---|
165 | if (tempFile) {
|
---|
166 | if (!file.delete()) {
|
---|
167 | file.deleteOnExit();
|
---|
168 | }
|
---|
169 | file = null;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | } catch (IllegalDataException e) {
|
---|
173 | handleException(tr("Data Error"), e);
|
---|
174 | } catch (IOException e) {
|
---|
175 | handleException(tr("IO Error"), e);
|
---|
176 | } catch (Exception e) {
|
---|
177 | cancel();
|
---|
178 | throw e;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void handleException(String dialogTitle, Exception e) {
|
---|
183 | Main.error(e);
|
---|
184 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
185 | Main.parent,
|
---|
186 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", uri != null ? uri : file.getName(), e.getMessage()),
|
---|
187 | dialogTitle,
|
---|
188 | JOptionPane.ERROR_MESSAGE,
|
---|
189 | null
|
---|
190 | );
|
---|
191 | cancel();
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|