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