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

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

code cleanup, javadoc update

  • Property svn:eol-style set to native
File size: 5.7 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 */
51 public boolean isBatchImporter() {
52 return false;
53 }
54
55 /**
56 * Needs to be implemented if isBatchImporter() returns false.
57 */
58 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
59 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
60 }
61
62 /**
63 * Needs to be implemented if isBatchImporter() returns true.
64 */
65 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
66 throw new IOException(tr("Could not import files."));
67 }
68
69 /**
70 * Wrapper to give meaningful output if things go wrong.
71 * @return true if data import was successful
72 */
73 public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
74 try {
75 Main.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
76 importData(f, progressMonitor);
77 return true;
78 } catch (IllegalDataException e) {
79 Throwable cause = e.getCause();
80 if (cause instanceof ImportCancelException) {
81 displayCancel(cause);
82 } else {
83 displayError(f, e);
84 }
85 return false;
86 } catch (Exception e) {
87 displayError(f, e);
88 return false;
89 }
90 }
91
92 private static void displayError(File f, Exception e) {
93 Main.error(e);
94 HelpAwareOptionPane.showMessageDialogInEDT(
95 Main.parent,
96 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
97 tr("Error"),
98 JOptionPane.ERROR_MESSAGE, null
99 );
100 }
101
102 private static void displayCancel(final Throwable t) {
103 GuiHelper.runInEDTAndWait(new Runnable() {
104 @Override
105 public void run() {
106 Notification note = new Notification(t.getMessage());
107 note.setIcon(JOptionPane.INFORMATION_MESSAGE);
108 note.setDuration(Notification.TIME_SHORT);
109 note.show();
110 }
111 });
112 }
113
114 public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
115 try {
116 Main.info("Open "+files.size()+" files");
117 importData(files, progressMonitor);
118 return true;
119 } catch (Exception e) {
120 Main.error(e);
121 HelpAwareOptionPane.showMessageDialogInEDT(
122 Main.parent,
123 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
124 tr("Error"),
125 JOptionPane.ERROR_MESSAGE, null
126 );
127 return false;
128 }
129 }
130
131 /**
132 * If multiple files (with multiple file formats) are selected,
133 * they are opened in the order of their priorities.
134 * Highest priority comes first.
135 */
136 public double getPriority() {
137 return 0;
138 }
139
140 @Override
141 public int compareTo(FileImporter other) {
142 return Double.compare(this.getPriority(), other.getPriority());
143 }
144
145 /**
146 * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
147 * @return true if this {@code FileImporter} is enabled
148 * @since 5459
149 */
150 public final boolean isEnabled() {
151 return enabled;
152 }
153
154 /**
155 * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
156 * @param enabled true to enable this {@code FileImporter}, false to disable it
157 * @since 5459
158 */
159 public final void setEnabled(boolean enabled) {
160 this.enabled = enabled;
161 }
162
163 @Override
164 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
165 // To be overriden by subclasses if their enabled state depends of the active layer nature
166 }
167
168 @Override
169 public void layerAdded(Layer newLayer) {
170 // To be overriden by subclasses if needed
171 }
172
173 @Override
174 public void layerRemoved(Layer oldLayer) {
175 // To be overriden by subclasses if needed
176 }
177}
Note: See TracBrowser for help on using the repository browser.