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.IOException;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.JFileChooser;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
22 | import org.openstreetmap.josm.io.session.SessionReader;
|
---|
23 |
|
---|
24 | public class SessionLoadAction extends DiskAccessAction {
|
---|
25 |
|
---|
26 | public SessionLoadAction() {
|
---|
27 | super(tr("Load Session"), "open", tr("Load a session from file."), null, true, "load-session", true);
|
---|
28 | putValue("help", ht("/Action/SessionLoad"));
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void actionPerformed(ActionEvent e) {
|
---|
32 | ExtensionFileFilter ff = new ExtensionFileFilter("jos,joz", "jos", tr("Session file (*.jos, *.joz)"));
|
---|
33 | JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"), ff, JFileChooser.FILES_ONLY, "lastDirectory");
|
---|
34 | if (fc == null) return;
|
---|
35 | File file = fc.getSelectedFile();
|
---|
36 | boolean zip = file.getName().toLowerCase().endsWith(".joz");
|
---|
37 | Main.worker.submit(new Loader(file, zip));
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static class Loader extends PleaseWaitRunnable {
|
---|
41 |
|
---|
42 | private boolean canceled;
|
---|
43 | private File file;
|
---|
44 | private boolean zip;
|
---|
45 | private List<Layer> layers;
|
---|
46 | private List<Runnable> postLoadTasks;
|
---|
47 |
|
---|
48 | public Loader(File file, boolean zip) {
|
---|
49 | super(tr("Loading session ''{0}''", file.getName()));
|
---|
50 | this.file = file;
|
---|
51 | this.zip = zip;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | protected void cancel() {
|
---|
56 | Thread.dumpStack();
|
---|
57 | canceled = true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | protected void finish() {
|
---|
62 | SwingUtilities.invokeLater(new Runnable() {
|
---|
63 | @Override
|
---|
64 | public void run() {
|
---|
65 | if (canceled) return;
|
---|
66 | for (Layer l : layers) {
|
---|
67 | if (canceled) return;
|
---|
68 | Main.main.addLayer(l);
|
---|
69 | }
|
---|
70 | for (Runnable task : postLoadTasks) {
|
---|
71 | if (canceled) return;
|
---|
72 | if (task == null) {
|
---|
73 | continue;
|
---|
74 | }
|
---|
75 | task.run();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | });
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | protected void realRun() {
|
---|
83 | try {
|
---|
84 | ProgressMonitor monitor = getProgressMonitor();
|
---|
85 | SessionReader reader = new SessionReader();
|
---|
86 | reader.loadSession(file, zip, monitor);
|
---|
87 | layers = reader.getLayers();
|
---|
88 | postLoadTasks = reader.getPostLoadTasks();
|
---|
89 | } catch (IllegalDataException e) {
|
---|
90 | e.printStackTrace();
|
---|
91 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
92 | Main.parent,
|
---|
93 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", file.getName(), e.getMessage()),
|
---|
94 | tr("Data Error"),
|
---|
95 | JOptionPane.ERROR_MESSAGE,
|
---|
96 | null
|
---|
97 | );
|
---|
98 | cancel();
|
---|
99 | } catch (IOException e) {
|
---|
100 | e.printStackTrace();
|
---|
101 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
102 | Main.parent,
|
---|
103 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", file.getName(), e.getMessage()),
|
---|
104 | tr("IO Error"),
|
---|
105 | JOptionPane.ERROR_MESSAGE,
|
---|
106 | null
|
---|
107 | );
|
---|
108 | cancel();
|
---|
109 | } catch (RuntimeException e) {
|
---|
110 | cancel();
|
---|
111 | throw e;
|
---|
112 | } catch (Error e) {
|
---|
113 | cancel();
|
---|
114 | throw e;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|