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

Last change on this file since 5707 was 5459, checked in by Don-vip, 12 years ago

fix #2961 - Improve usability of WMS Layer Saving/Loading

  • Replaced the unconventional method of creating a blank layer, then loading a .wms file to a standard File->Open approach
  • Fixed memory leaks with some actions registered as listeners but never destroyed
  • Layer interface modified to allow a generic approach of layer saving in SaveActionBase rather than the previous one restricted to OSM and GPX data
  • FileImporters and FileExporters can now be enabled/disabled at runtime, for example when the active layer changes
  • 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 System.out.println("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 System.out.println("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 public int compareTo(FileImporter other) {
106 return (new Double(this.getPriority())).compareTo(other.getPriority());
107 }
108
109 public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {
110 if (in == null) {
111 return null;
112 }
113 BufferedInputStream bis = new BufferedInputStream(in);
114 int b = bis.read();
115 if (b != 'B')
116 throw new IOException(tr("Invalid bz2 file."));
117 b = bis.read();
118 if (b != 'Z')
119 throw new IOException(tr("Invalid bz2 file."));
120 return new CBZip2InputStream(bis);
121 }
122
123 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
124 if (in == null) {
125 return null;
126 }
127 return new GZIPInputStream(in);
128 }
129
130 /**
131 * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File->Open" dialog.
132 * @return true if this {@code FileImporter} is enabled
133 * @since 5459
134 */
135 public final boolean isEnabled() {
136 return enabled;
137 }
138
139 /**
140 * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File->Open" dialog.
141 * @param enabled true to enable this {@code FileImporter}, false to disable it
142 * @since 5459
143 */
144 public final void setEnabled(boolean enabled) {
145 this.enabled = enabled;
146 }
147
148 @Override
149 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
150 // To be overriden by subclasses if their enabled state depends of the active layer nature
151 }
152
153 @Override
154 public void layerAdded(Layer newLayer) {
155 // To be overriden by subclasses if needed
156 }
157
158 @Override
159 public void layerRemoved(Layer oldLayer) {
160 // To be overriden by subclasses if needed
161 }
162}
Note: See TracBrowser for help on using the repository browser.