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

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

javadoc fixes for jdk8 compatibility

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