source: josm/trunk/src/org/openstreetmap/josm/io/FileImporter.java@ 3513

Last change on this file since 3513 was 3501, checked in by bastiK, 14 years ago

fixed #4632 - Button Help puts help window under main window

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.IOException;
8import java.util.List;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.ExtensionFileFilter;
14import org.openstreetmap.josm.gui.HelpAwareOptionPane;
15import org.openstreetmap.josm.gui.progress.ProgressMonitor;
16
17public abstract class FileImporter implements Comparable<FileImporter> {
18
19 public final ExtensionFileFilter filter;
20
21 public FileImporter(ExtensionFileFilter filter) {
22 this.filter = filter;
23 }
24
25 public boolean acceptFile(File pathname) {
26 return filter.acceptName(pathname.getName());
27 }
28
29 /**
30 * A batch importer is a file importer that prefers to read multiple files at the same time.
31 */
32 public boolean isBatchImporter() {
33 return false;
34 }
35
36 /**
37 * Needs to be implemented if isBatchImporter() returns false.
38 * @throws IllegalDataException
39 */
40 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
41 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
42 }
43
44 /**
45 * Needs to be implemented if isBatchImporter() returns true.
46 * @throws IllegalDataException
47 */
48 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
49 throw new IOException(tr("Could not import files."));
50 }
51
52 /**
53 * Wrapper to give meaningful output if things go wrong.
54 */
55 public void importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
56 try {
57 System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
58 importData(f, progressMonitor);
59 } catch (Exception e) {
60 e.printStackTrace();
61 HelpAwareOptionPane.showMessageDialogInEDT(
62 Main.parent,
63 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
64 tr("Error"),
65 JOptionPane.ERROR_MESSAGE, null
66 );
67 }
68 }
69 public void importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
70 try {
71 System.out.println("Open "+files.size()+" files");
72 importData(files, progressMonitor);
73 } catch (Exception e) {
74 e.printStackTrace();
75 HelpAwareOptionPane.showMessageDialogInEDT(
76 Main.parent,
77 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
78 tr("Error"),
79 JOptionPane.ERROR_MESSAGE, null
80 );
81 }
82 }
83
84 /**
85 * If multiple files (with multiple file formats) are selected,
86 * they are opened in the order of their priorities.
87 * Highest priority comes first.
88 */
89 public double getPriority() {
90 return 0;
91 }
92
93 public int compareTo(FileImporter other) {
94 return (new Double(this.getPriority())).compareTo(other.getPriority());
95 }
96
97}
Note: See TracBrowser for help on using the repository browser.