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

Last change on this file since 10436 was 10386, checked in by Don-vip, 8 years ago

gsoc-core - remove more deprecation warnings

  • Property svn:eol-style set to native
File size: 6.1 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.Notification;
16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
17import org.openstreetmap.josm.gui.util.GuiHelper;
18
19/**
20 * Abstract file importer.
21 * @since 1637
22 * @since 10386 (signature)
23 */
24public abstract class FileImporter implements Comparable<FileImporter> {
25
26 /**
27 * The extension file filter used to accept files.
28 */
29 public final ExtensionFileFilter filter;
30
31 private boolean enabled;
32
33 /**
34 * Constructs a new {@code FileImporter} with the given extension file filter.
35 * @param filter The extension file filter
36 */
37 public FileImporter(ExtensionFileFilter filter) {
38 this.filter = filter;
39 this.enabled = true;
40 }
41
42 /**
43 * Determines if this file importer accepts the given file.
44 * @param pathname The file to test
45 * @return {@code true} if this file importer accepts the given file, {@code false} otherwise
46 */
47 public boolean acceptFile(File pathname) {
48 return filter.acceptName(pathname.getName());
49 }
50
51 /**
52 * A batch importer is a file importer that prefers to read multiple files at the same time.
53 * @return {@code true} if this importer is a batch importer
54 */
55 public boolean isBatchImporter() {
56 return false;
57 }
58
59 /**
60 * Needs to be implemented if isBatchImporter() returns false.
61 * @param file file to import
62 * @param progressMonitor progress monitor
63 * @throws IOException if any I/O error occurs
64 * @throws IllegalDataException if invalid data is read
65 */
66 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
67 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
68 }
69
70 /**
71 * Needs to be implemented if isBatchImporter() returns true.
72 * @param files files to import
73 * @param progressMonitor progress monitor
74 * @throws IOException if any I/O error occurs
75 * @throws IllegalDataException if invalid data is read
76 */
77 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
78 throw new IOException(tr("Could not import files."));
79 }
80
81 /**
82 * Wrapper to {@link #importData(File, ProgressMonitor)} to give meaningful output if things go wrong.
83 * @param f data file to import
84 * @param progressMonitor progress monitor
85 * @return true if data import was successful
86 */
87 public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
88 try {
89 Main.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
90 importData(f, progressMonitor);
91 return true;
92 } catch (IllegalDataException e) {
93 Throwable cause = e.getCause();
94 if (cause instanceof ImportCancelException) {
95 displayCancel(cause);
96 } else {
97 displayError(f, e);
98 }
99 return false;
100 } catch (IOException e) {
101 displayError(f, e);
102 return false;
103 }
104 }
105
106 private static void displayError(File f, Exception e) {
107 Main.error(e);
108 HelpAwareOptionPane.showMessageDialogInEDT(
109 Main.parent,
110 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
111 tr("Error"),
112 JOptionPane.ERROR_MESSAGE, null
113 );
114 }
115
116 private static void displayCancel(final Throwable t) {
117 GuiHelper.runInEDTAndWait(new Runnable() {
118 @Override
119 public void run() {
120 Notification note = new Notification(t.getMessage());
121 note.setIcon(JOptionPane.INFORMATION_MESSAGE);
122 note.setDuration(Notification.TIME_SHORT);
123 note.show();
124 }
125 });
126 }
127
128 /**
129 * Wrapper to {@link #importData(List, ProgressMonitor)} to give meaningful output if things go wrong.
130 * @param files data files to import
131 * @param progressMonitor progress monitor
132 * @return true if data import was successful
133 */
134 public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
135 try {
136 Main.info("Open "+files.size()+" files");
137 importData(files, progressMonitor);
138 return true;
139 } catch (IOException | IllegalDataException e) {
140 Main.error(e);
141 HelpAwareOptionPane.showMessageDialogInEDT(
142 Main.parent,
143 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
144 tr("Error"),
145 JOptionPane.ERROR_MESSAGE, null
146 );
147 return false;
148 }
149 }
150
151 /**
152 * If multiple files (with multiple file formats) are selected,
153 * they are opened in the order of their priorities.
154 * Highest priority comes first.
155 * @return priority
156 */
157 public double getPriority() {
158 return 0;
159 }
160
161 @Override
162 public int compareTo(FileImporter other) {
163 return Double.compare(this.getPriority(), other.getPriority());
164 }
165
166 /**
167 * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
168 * @return true if this {@code FileImporter} is enabled
169 * @since 5459
170 */
171 public final boolean isEnabled() {
172 return enabled;
173 }
174
175 /**
176 * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
177 * @param enabled true to enable this {@code FileImporter}, false to disable it
178 * @since 5459
179 */
180 public final void setEnabled(boolean enabled) {
181 this.enabled = enabled;
182 }
183}
Note: See TracBrowser for help on using the repository browser.