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

Last change on this file since 6360 was 6248, checked in by Don-vip, 11 years ago

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
  • Property svn:eol-style set to native
File size: 5.3 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.layer.Layer;
21import org.openstreetmap.josm.gui.progress.ProgressMonitor;
22
23public abstract class FileImporter implements Comparable<FileImporter>, LayerChangeListener {
24
25 public final ExtensionFileFilter filter;
26
27 private boolean enabled;
28
29 public FileImporter(ExtensionFileFilter filter) {
30 this.filter = filter;
31 this.enabled = true;
32 }
33
34 public boolean acceptFile(File pathname) {
35 return filter.acceptName(pathname.getName());
36 }
37
38 /**
39 * A batch importer is a file importer that prefers to read multiple files at the same time.
40 */
41 public boolean isBatchImporter() {
42 return false;
43 }
44
45 /**
46 * Needs to be implemented if isBatchImporter() returns false.
47 */
48 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
49 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
50 }
51
52 /**
53 * Needs to be implemented if isBatchImporter() returns true.
54 */
55 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
56 throw new IOException(tr("Could not import files."));
57 }
58
59 /**
60 * Wrapper to give meaningful output if things go wrong.
61 * @return true if data import was successful
62 */
63 public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
64 try {
65 Main.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
66 importData(f, progressMonitor);
67 return true;
68 } catch (Exception e) {
69 e.printStackTrace();
70 HelpAwareOptionPane.showMessageDialogInEDT(
71 Main.parent,
72 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
73 tr("Error"),
74 JOptionPane.ERROR_MESSAGE, null
75 );
76 return false;
77 }
78 }
79 public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
80 try {
81 Main.info("Open "+files.size()+" files");
82 importData(files, progressMonitor);
83 return true;
84 } catch (Exception e) {
85 e.printStackTrace();
86 HelpAwareOptionPane.showMessageDialogInEDT(
87 Main.parent,
88 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
89 tr("Error"),
90 JOptionPane.ERROR_MESSAGE, null
91 );
92 return false;
93 }
94 }
95
96 /**
97 * If multiple files (with multiple file formats) are selected,
98 * they are opened in the order of their priorities.
99 * Highest priority comes first.
100 */
101 public double getPriority() {
102 return 0;
103 }
104
105 @Override
106 public int compareTo(FileImporter other) {
107 return Double.compare(this.getPriority(), other.getPriority());
108 }
109
110 public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {
111 if (in == null) {
112 return null;
113 }
114 BufferedInputStream bis = new BufferedInputStream(in);
115 int b = bis.read();
116 if (b != 'B')
117 throw new IOException(tr("Invalid bz2 file."));
118 b = bis.read();
119 if (b != 'Z')
120 throw new IOException(tr("Invalid bz2 file."));
121 return new CBZip2InputStream(bis);
122 }
123
124 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
125 if (in == null) {
126 return null;
127 }
128 return new GZIPInputStream(in);
129 }
130
131 /**
132 * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File->Open" dialog.
133 * @return true if this {@code FileImporter} is enabled
134 * @since 5459
135 */
136 public final boolean isEnabled() {
137 return enabled;
138 }
139
140 /**
141 * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File->Open" dialog.
142 * @param enabled true to enable this {@code FileImporter}, false to disable it
143 * @since 5459
144 */
145 public final void setEnabled(boolean enabled) {
146 this.enabled = enabled;
147 }
148
149 @Override
150 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
151 // To be overriden by subclasses if their enabled state depends of the active layer nature
152 }
153
154 @Override
155 public void layerAdded(Layer newLayer) {
156 // To be overriden by subclasses if needed
157 }
158
159 @Override
160 public void layerRemoved(Layer oldLayer) {
161 // To be overriden by subclasses if needed
162 }
163}
Note: See TracBrowser for help on using the repository browser.