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

Last change on this file since 8390 was 7596, checked in by Don-vip, 10 years ago

fix various warnings

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