1 | //License: GPL.
|
---|
2 | package org.openstreetmap.hot.sds;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.io.UnsupportedEncodingException;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import javax.xml.parsers.ParserConfigurationException;
|
---|
17 | import javax.xml.parsers.SAXParserFactory;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.SwingFileChooser;
|
---|
22 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
23 | import org.xml.sax.InputSource;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 |
|
---|
26 | @SuppressWarnings("serial")
|
---|
27 | public class SdsLoadAction extends SdsDiskAccessAction {
|
---|
28 |
|
---|
29 | private SeparateDataStorePlugin plugin;
|
---|
30 |
|
---|
31 | public SdsLoadAction(SeparateDataStorePlugin p) {
|
---|
32 | super(tr("Load..."), "sds_load", tr("Load separate data store data from a file."), null);
|
---|
33 | plugin = p;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void actionPerformed(ActionEvent e) {
|
---|
37 | SwingFileChooser fc = createAndOpenFileChooser(true, true, null);
|
---|
38 | if (fc == null)
|
---|
39 | return;
|
---|
40 | File[] files = fc.getSelectedFiles();
|
---|
41 | openFiles(Arrays.asList(files));
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void openFiles(List<File> fileList) {
|
---|
45 | OpenFileTask task = new OpenFileTask(fileList, plugin);
|
---|
46 | Main.worker.submit(task);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public class OpenFileTask extends PleaseWaitRunnable {
|
---|
50 | private List<File> files;
|
---|
51 | private boolean canceled;
|
---|
52 | private SeparateDataStorePlugin plugin;
|
---|
53 |
|
---|
54 | public OpenFileTask(List<File> files, SeparateDataStorePlugin p) {
|
---|
55 | super(tr("Loading files"), false /* don't ignore exception */);
|
---|
56 | this.files = new ArrayList<File>(files);
|
---|
57 | plugin = p;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | protected void cancel() {
|
---|
62 | this.canceled = true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | protected void finish() {
|
---|
67 | // do nothing
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
72 | if (files == null || files.isEmpty()) return;
|
---|
73 |
|
---|
74 | getProgressMonitor().setTicksCount(files.size());
|
---|
75 |
|
---|
76 | for (File f : files) {
|
---|
77 | if (canceled) return;
|
---|
78 | getProgressMonitor().indeterminateSubTask(tr("Opening file ''{0}'' ...", f.getAbsolutePath()));
|
---|
79 |
|
---|
80 | InputStream fileStream;
|
---|
81 | try {
|
---|
82 | fileStream = new FileInputStream(f);
|
---|
83 | SdsParser p = new SdsParser(Main.main.getCurrentDataSet(), plugin, false);
|
---|
84 | InputSource inputSource = new InputSource(fileStream);
|
---|
85 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, p);
|
---|
86 | } catch (UnsupportedEncodingException e1) {
|
---|
87 | // TODO Auto-generated catch block
|
---|
88 | e1.printStackTrace();
|
---|
89 | } catch (SAXException e) {
|
---|
90 | // TODO Auto-generated catch block
|
---|
91 | e.printStackTrace();
|
---|
92 | } catch (IOException e) {
|
---|
93 | // TODO Auto-generated catch block
|
---|
94 | e.printStackTrace();
|
---|
95 | } catch (ParserConfigurationException e) {
|
---|
96 | // TODO Auto-generated catch block
|
---|
97 | e.printStackTrace();
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|