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 | import javax.swing.filechooser.FileFilter;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
19 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
23 | import org.openstreetmap.josm.io.session.SessionReader;
|
---|
24 |
|
---|
25 | public class SessionLoadAction extends JosmAction {
|
---|
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 | String curDir = Main.pref.get("lastDirectory");
|
---|
33 | if (curDir.equals("")) {
|
---|
34 | curDir = ".";
|
---|
35 | }
|
---|
36 | JFileChooser fc = new JFileChooser(new File(curDir));
|
---|
37 | fc.setDialogTitle(tr("Open session"));
|
---|
38 | fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
---|
39 | fc.setMultiSelectionEnabled(false);
|
---|
40 | fc.setAcceptAllFileFilterUsed(true);
|
---|
41 | FileFilter ff = new ExtensionFileFilter("jos,joz", "jos", tr("Session file (*.jos, *.joz)"));
|
---|
42 | fc.addChoosableFileFilter(ff);
|
---|
43 | int answer = fc.showOpenDialog(Main.parent);
|
---|
44 | if (answer != JFileChooser.APPROVE_OPTION)
|
---|
45 | return;
|
---|
46 |
|
---|
47 | if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
|
---|
48 | Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
|
---|
49 | }
|
---|
50 | File file = fc.getSelectedFile();
|
---|
51 | boolean zip = true;
|
---|
52 | if (file.getName().toLowerCase().endsWith(".jos")) {
|
---|
53 | zip = false;
|
---|
54 | }
|
---|
55 | Main.worker.submit(new Loader(file, zip));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static class Loader extends PleaseWaitRunnable {
|
---|
59 |
|
---|
60 | private boolean canceled;
|
---|
61 | private File file;
|
---|
62 | private boolean zip;
|
---|
63 | private List<Layer> layers;
|
---|
64 | private List<Runnable> postLoadTasks;
|
---|
65 |
|
---|
66 | public Loader(File file, boolean zip) {
|
---|
67 | super(tr("Loading session ''{0}''", file.getName()));
|
---|
68 | this.file = file;
|
---|
69 | this.zip = zip;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | protected void cancel() {
|
---|
74 | Thread.currentThread().dumpStack();
|
---|
75 | canceled = true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | protected void finish() {
|
---|
80 | SwingUtilities.invokeLater(new Runnable() {
|
---|
81 | @Override
|
---|
82 | public void run() {
|
---|
83 | if (canceled) return;
|
---|
84 | for (Layer l : layers) {
|
---|
85 | if (canceled) return;
|
---|
86 | Main.main.addLayer(l);
|
---|
87 | }
|
---|
88 | for (Runnable task : postLoadTasks) {
|
---|
89 | if (canceled) return;
|
---|
90 | if (task == null) {
|
---|
91 | continue;
|
---|
92 | }
|
---|
93 | task.run();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | });
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | protected void realRun() {
|
---|
101 | try {
|
---|
102 | ProgressMonitor monitor = getProgressMonitor();
|
---|
103 | SessionReader reader = new SessionReader();
|
---|
104 | reader.loadSession(file, zip, monitor);
|
---|
105 | layers = reader.getLayers();
|
---|
106 | postLoadTasks = reader.getPostLoadTasks();
|
---|
107 | } catch (IllegalDataException e) {
|
---|
108 | e.printStackTrace();
|
---|
109 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
110 | Main.parent,
|
---|
111 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", file.getName(), e.getMessage()),
|
---|
112 | tr("Data Error"),
|
---|
113 | JOptionPane.ERROR_MESSAGE,
|
---|
114 | null
|
---|
115 | );
|
---|
116 | cancel();
|
---|
117 | } catch (IOException e) {
|
---|
118 | e.printStackTrace();
|
---|
119 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
120 | Main.parent,
|
---|
121 | tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", file.getName(), e.getMessage()),
|
---|
122 | tr("IO Error"),
|
---|
123 | JOptionPane.ERROR_MESSAGE,
|
---|
124 | null
|
---|
125 | );
|
---|
126 | cancel();
|
---|
127 | } catch (RuntimeException e) {
|
---|
128 | cancel();
|
---|
129 | throw e;
|
---|
130 | } catch (Error e) {
|
---|
131 | cancel();
|
---|
132 | throw e;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|