1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import javax.swing.JFileChooser;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.SwingUtilities;
|
---|
22 | import javax.swing.filechooser.FileFilter;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
26 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
27 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
28 | import org.openstreetmap.josm.io.AllFormatsImporter;
|
---|
29 | import org.openstreetmap.josm.io.FileImporter;
|
---|
30 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
31 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
32 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
33 | import org.xml.sax.SAXException;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Open a file chooser dialog and select an file to import. Then call the gpx-import driver. Finally
|
---|
37 | * open an internal frame into the main window with the gpx data shown.
|
---|
38 | *
|
---|
39 | * @author imi
|
---|
40 | */
|
---|
41 | public class OpenFileAction extends DiskAccessAction {
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Create an open action. The name is "Open a file".
|
---|
45 | */
|
---|
46 | public OpenFileAction() {
|
---|
47 | super(tr("Open..."), "open", tr("Open a file."),
|
---|
48 | Shortcut.registerShortcut("system:open", tr("File: {0}", tr("Open...")), KeyEvent.VK_O, Shortcut.GROUP_MENU));
|
---|
49 | putValue("help", ht("/Action/OpenFile"));
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void actionPerformed(ActionEvent e) {
|
---|
54 | JFileChooser fc = createAndOpenFileChooser(true, true, null);
|
---|
55 | if (fc == null)
|
---|
56 | return;
|
---|
57 | File[] files = fc.getSelectedFiles();
|
---|
58 | OpenFileTask task = new OpenFileTask(Arrays.asList(files), fc.getFileFilter());
|
---|
59 | Main.worker.submit(task);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | protected void updateEnabledState() {
|
---|
64 | setEnabled(! Main.applet);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Open a list of files. The complete list will be passed to batch importers.
|
---|
69 | * @param fileList A list of files
|
---|
70 | */
|
---|
71 | static public void openFiles(List<File> fileList) {
|
---|
72 | OpenFileTask task = new OpenFileTask(fileList, null);
|
---|
73 | Main.worker.submit(task);
|
---|
74 | }
|
---|
75 |
|
---|
76 | static public class OpenFileTask extends PleaseWaitRunnable {
|
---|
77 | private List<File> files;
|
---|
78 | private List<File> successfullyOpenedFiles = new ArrayList<File>();
|
---|
79 | private FileFilter fileFilter;
|
---|
80 | private boolean cancelled;
|
---|
81 |
|
---|
82 | public OpenFileTask(List<File> files, FileFilter fileFilter, String title) {
|
---|
83 | super(title, false /* don't ignore exception */);
|
---|
84 | this.files = new ArrayList<File>(files);
|
---|
85 | this.fileFilter = fileFilter;
|
---|
86 | }
|
---|
87 |
|
---|
88 | public OpenFileTask(List<File> files, FileFilter fileFilter) {
|
---|
89 | this(files, fileFilter, tr("Opening files"));
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | protected void cancel() {
|
---|
94 | this.cancelled = true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | protected void finish() {
|
---|
99 | // do nothing
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void alertFilesNotMatchingWithImporter(Collection<File> files, FileImporter importer) {
|
---|
103 | final StringBuffer msg = new StringBuffer();
|
---|
104 | msg.append("<html>");
|
---|
105 | msg.append(
|
---|
106 | trn(
|
---|
107 | "Cannot open {0} file with the file importer ''{1}''.",
|
---|
108 | "Cannot open {0} files with the file importer ''{1}''.",
|
---|
109 | files.size(),
|
---|
110 | files.size(),
|
---|
111 | importer.filter.getDescription()
|
---|
112 | )
|
---|
113 | ).append("<br>");
|
---|
114 | msg.append("<ul>");
|
---|
115 | for (File f: files) {
|
---|
116 | msg.append("<li>").append(f.getAbsolutePath()).append("</li>");
|
---|
117 | }
|
---|
118 | msg.append("</ul>");
|
---|
119 |
|
---|
120 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
121 | Main.parent,
|
---|
122 | msg.toString(),
|
---|
123 | tr("Warning"),
|
---|
124 | JOptionPane.WARNING_MESSAGE,
|
---|
125 | HelpUtil.ht("/Action/OpenFile#ImporterCantImportFiles")
|
---|
126 | );
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected void alertFilesWithUnknownImporter(Collection<File> files) {
|
---|
130 | final StringBuffer msg = new StringBuffer();
|
---|
131 | msg.append("<html>");
|
---|
132 | msg.append(
|
---|
133 | trn(
|
---|
134 | "Cannot open {0} file because no suitable file importer is available.",
|
---|
135 | "Cannot open {0} files because no suitable file importer is available.",
|
---|
136 | files.size(),
|
---|
137 | files.size()
|
---|
138 | )
|
---|
139 | ).append("<br>");
|
---|
140 | msg.append("<ul>");
|
---|
141 | for (File f: files) {
|
---|
142 | msg.append("<li>").append(f.getAbsolutePath()).append("</li>");
|
---|
143 | }
|
---|
144 | msg.append("</ul>");
|
---|
145 |
|
---|
146 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
147 | Main.parent,
|
---|
148 | msg.toString(),
|
---|
149 | tr("Warning"),
|
---|
150 | JOptionPane.WARNING_MESSAGE,
|
---|
151 | HelpUtil.ht("/Action/OpenFile#MissingImporterForFiles")
|
---|
152 | );
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
157 | if (files == null || files.isEmpty()) return;
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Find the importer with the chosen file filter
|
---|
161 | */
|
---|
162 | FileImporter chosenImporter = null;
|
---|
163 | for (FileImporter importer : ExtensionFileFilter.importers) {
|
---|
164 | if (fileFilter == importer.filter) {
|
---|
165 | chosenImporter = importer;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | /**
|
---|
169 | * If the filter wasn't changed in the dialog, chosenImporter is null now.
|
---|
170 | * When the filter was explicitly set to AllFormatsImporter, treat this the same.
|
---|
171 | */
|
---|
172 | if (chosenImporter instanceof AllFormatsImporter) {
|
---|
173 | chosenImporter = null;
|
---|
174 | }
|
---|
175 | getProgressMonitor().setTicksCount(files.size());
|
---|
176 |
|
---|
177 | if (chosenImporter != null) {
|
---|
178 | // The importer was expicitely chosen, so use it.
|
---|
179 | List<File> filesNotMatchingWithImporter = new LinkedList<File>();
|
---|
180 | List<File> filesMatchingWithImporter = new LinkedList<File>();
|
---|
181 | for (final File f : files) {
|
---|
182 | if (!chosenImporter.acceptFile(f)) {
|
---|
183 | if (f.isDirectory()) {
|
---|
184 | SwingUtilities.invokeLater(new Runnable() {
|
---|
185 | public void run() {
|
---|
186 | JOptionPane.showMessageDialog(Main.parent, tr(
|
---|
187 | "<html>Cannot open directory ''{0}''.<br>Please select a file.</html>",
|
---|
188 | f.getAbsolutePath()), tr("Open file"), JOptionPane.ERROR_MESSAGE);
|
---|
189 | }
|
---|
190 | });
|
---|
191 | // TODO when changing to Java 6: Don't cancel the
|
---|
192 | // task here but use different modality. (Currently 2 dialogs
|
---|
193 | // would block each other.)
|
---|
194 | return;
|
---|
195 | } else {
|
---|
196 | filesNotMatchingWithImporter.add(f);
|
---|
197 | }
|
---|
198 | } else {
|
---|
199 | filesMatchingWithImporter.add(f);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (!filesNotMatchingWithImporter.isEmpty()) {
|
---|
204 | alertFilesNotMatchingWithImporter(filesNotMatchingWithImporter, chosenImporter);
|
---|
205 | }
|
---|
206 | if (!filesMatchingWithImporter.isEmpty()) {
|
---|
207 | importData(chosenImporter, filesMatchingWithImporter);
|
---|
208 | }
|
---|
209 | } else {
|
---|
210 | // find appropriate importer
|
---|
211 | MultiMap<FileImporter, File> map = new MultiMap<FileImporter, File>();
|
---|
212 | List<File> filesWithKnownImporter = new LinkedList<File>();
|
---|
213 | List<File> filesWithUnknownImporter = new LinkedList<File>();
|
---|
214 | FILES: for (File f : files) {
|
---|
215 | for (FileImporter importer : ExtensionFileFilter.importers) {
|
---|
216 | if (importer.acceptFile(f)) {
|
---|
217 | map.put(importer, f);
|
---|
218 | filesWithKnownImporter.add(f);
|
---|
219 | continue FILES;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | filesWithUnknownImporter.add(f);
|
---|
223 | }
|
---|
224 | if (!filesWithUnknownImporter.isEmpty()) {
|
---|
225 | alertFilesWithUnknownImporter(filesWithUnknownImporter);
|
---|
226 | }
|
---|
227 | List<FileImporter> ims = new ArrayList<FileImporter>(map.keySet());
|
---|
228 | Collections.sort(ims);
|
---|
229 | Collections.reverse(ims);
|
---|
230 | for (FileImporter importer : ims) {
|
---|
231 | List<File> files = new ArrayList<File>(map.get(importer));
|
---|
232 | importData(importer, files);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | public void importData(FileImporter importer, List<File> files) {
|
---|
238 | if (importer.isBatchImporter()) {
|
---|
239 | if (cancelled) return;
|
---|
240 | String msg;
|
---|
241 | if (files.size() == 1) {
|
---|
242 | msg = tr("Opening 1 file...");
|
---|
243 | } else {
|
---|
244 | msg = trn("Opening {0} file...", "Opening {0} files...", files.size(), files.size());
|
---|
245 | }
|
---|
246 | getProgressMonitor().setCustomText(msg);
|
---|
247 | getProgressMonitor().indeterminateSubTask(msg);
|
---|
248 | if (importer.importDataHandleExceptions(files, getProgressMonitor().createSubTaskMonitor(files.size(), false))) {
|
---|
249 | successfullyOpenedFiles.addAll(files);
|
---|
250 | }
|
---|
251 | } else {
|
---|
252 | for (File f : files) {
|
---|
253 | if (cancelled) return;
|
---|
254 | getProgressMonitor().indeterminateSubTask(tr("Opening file ''{0}'' ...", f.getAbsolutePath()));
|
---|
255 | if (importer.importDataHandleExceptions(f, getProgressMonitor().createSubTaskMonitor(1, false))) {
|
---|
256 | successfullyOpenedFiles.add(f);
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public List<File> getSuccessfullyOpenedFiles() {
|
---|
263 | return successfullyOpenedFiles;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|