source: josm/trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java@ 2512

Last change on this file since 2512 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

File size: 3.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.io.File;
10import java.io.IOException;
11import java.util.Arrays;
12import java.util.List;
13
14import javax.swing.JFileChooser;
15import javax.swing.JOptionPane;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.gui.PleaseWaitRunnable;
19import org.openstreetmap.josm.io.FileImporter;
20import org.openstreetmap.josm.io.IllegalDataException;
21import org.openstreetmap.josm.io.OsmTransferException;
22import org.openstreetmap.josm.tools.Shortcut;
23import org.xml.sax.SAXException;
24
25/**
26 * Open a file chooser dialog and select an file to import. Then call the gpx-import driver. Finally
27 * open an internal frame into the main window with the gpx data shown.
28 *
29 * @author imi
30 */
31public class OpenFileAction extends DiskAccessAction {
32
33 /**
34 * Create an open action. The name is "Open a file".
35 */
36 public OpenFileAction() {
37 super(tr("Open..."), "open", tr("Open a file."),
38 Shortcut.registerShortcut("system:open", tr("File: {0}", tr("Open...")), KeyEvent.VK_O, Shortcut.GROUP_MENU));
39 putValue("help", ht("/Action/OpenFile"));
40
41 }
42
43 public void actionPerformed(ActionEvent e) {
44 JFileChooser fc = createAndOpenFileChooser(true, true, null);
45 if (fc == null)
46 return;
47 File[] files = fc.getSelectedFiles();
48 OpenFileTask task = new OpenFileTask(Arrays.asList(files));
49 Main.worker.submit(task);
50 }
51
52 @Override
53 protected void updateEnabledState() {
54 setEnabled(! Main.applet);
55 }
56
57 static public void openFile(File f) throws IOException, IllegalDataException {
58 for (FileImporter importer : ExtensionFileFilter.importers)
59 if (importer.acceptFile(f)) {
60 importer.importData(f);
61 }
62 }
63
64 static public class OpenFileTask extends PleaseWaitRunnable {
65 private List<File> files;
66 private boolean cancelled;
67
68 public OpenFileTask(List<File> files) {
69 super(tr("Opening files"), false /* don't ignore exception */);
70 this.files = files;
71 }
72 @Override
73 protected void cancel() {
74 this.cancelled = true;
75 }
76
77 @Override
78 protected void finish() {
79 // do nothing
80 }
81
82 @Override
83 protected void realRun() throws SAXException, IOException, OsmTransferException {
84 if (files == null || files.isEmpty()) return;
85 getProgressMonitor().setTicks(files.size());
86 for (File f : files) {
87 if (cancelled) return;
88 getProgressMonitor().subTask(tr("Opening file ''{0}'' ...", f.getAbsolutePath()));
89 try {
90 System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
91 openFile(f);
92 } catch (Exception e) {
93 e.printStackTrace();
94 JOptionPane.showMessageDialog(
95 Main.parent,
96 tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()),
97 tr("Error"),
98 JOptionPane.ERROR_MESSAGE
99 );
100 }
101 getProgressMonitor().worked(1);
102 }
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.