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

Last change on this file since 2284 was 2070, checked in by Gubaer, 15 years ago

new: rewrite of CombineWay action
new: conflict resolution dialog for CombineWay, including conflicts for different relation memberships
cleanup: cleanup in OsmReader, reduces memory footprint and reduces parsing time
cleanup: made most of the public fields in OsmPrimitive @deprecated, added accessors and changed the code
cleanup: replaced usages of @deprecated constructors for ExtendedDialog
fixed #3208: Combine ways brokes relation order

WARNING: this changeset touches a lot of code all over the code base. "latest" might become slightly unstable in the next days. Also experience incompatibility issues with plugins in the next few days.

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