001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.File;
007import java.io.IOException;
008import java.util.List;
009
010import javax.swing.JOptionPane;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.actions.ExtensionFileFilter;
014import org.openstreetmap.josm.gui.HelpAwareOptionPane;
015import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
016import org.openstreetmap.josm.gui.Notification;
017import org.openstreetmap.josm.gui.layer.Layer;
018import org.openstreetmap.josm.gui.progress.ProgressMonitor;
019import org.openstreetmap.josm.gui.util.GuiHelper;
020
021public abstract class FileImporter implements Comparable<FileImporter>, LayerChangeListener {
022
023    /**
024     * The extension file filter used to accept files.
025     */
026    public final ExtensionFileFilter filter;
027
028    private boolean enabled;
029
030    /**
031     * Constructs a new {@code FileImporter} with the given extension file filter.
032     * @param filter The extension file filter
033     */
034    public FileImporter(ExtensionFileFilter filter) {
035        this.filter = filter;
036        this.enabled = true;
037    }
038
039    /**
040     * Determines if this file importer accepts the given file.
041     * @param pathname The file to test
042     * @return {@code true} if this file importer accepts the given file, {@code false} otherwise
043     */
044    public boolean acceptFile(File pathname) {
045        return filter.acceptName(pathname.getName());
046    }
047
048    /**
049     * A batch importer is a file importer that prefers to read multiple files at the same time.
050     * @return {@code true} if this importer is a batch importer
051     */
052    public boolean isBatchImporter() {
053        return false;
054    }
055
056    /**
057     * Needs to be implemented if isBatchImporter() returns false.
058     * @param file file to import
059     * @param progressMonitor progress monitor
060     * @throws IOException if any I/O error occurs
061     * @throws IllegalDataException if invalid data is read
062     */
063    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
064        throw new IOException(tr("Could not import ''{0}''.", file.getName()));
065    }
066
067    /**
068     * Needs to be implemented if isBatchImporter() returns true.
069     * @param files files to import
070     * @param progressMonitor progress monitor
071     * @throws IOException if any I/O error occurs
072     * @throws IllegalDataException if invalid data is read
073     */
074    public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
075        throw new IOException(tr("Could not import files."));
076    }
077
078    /**
079     * Wrapper to give meaningful output if things go wrong.
080     * @return true if data import was successful
081     */
082    public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
083        try {
084            Main.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
085            importData(f, progressMonitor);
086            return true;
087        } catch (IllegalDataException e) {
088            Throwable cause = e.getCause();
089            if (cause instanceof ImportCancelException) {
090                displayCancel(cause);
091            } else {
092                displayError(f, e);
093            }
094            return false;
095        } catch (Exception e) {
096            displayError(f, e);
097            return false;
098        }
099    }
100
101    private static void displayError(File f, Exception e) {
102        Main.error(e);
103        HelpAwareOptionPane.showMessageDialogInEDT(
104                Main.parent,
105                tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
106                tr("Error"),
107                JOptionPane.ERROR_MESSAGE, null
108        );
109    }
110
111    private static void displayCancel(final Throwable t) {
112        GuiHelper.runInEDTAndWait(new Runnable() {
113            @Override
114            public void run() {
115                Notification note = new Notification(t.getMessage());
116                note.setIcon(JOptionPane.INFORMATION_MESSAGE);
117                note.setDuration(Notification.TIME_SHORT);
118                note.show();
119            }
120        });
121    }
122
123    public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
124        try {
125            Main.info("Open "+files.size()+" files");
126            importData(files, progressMonitor);
127            return true;
128        } catch (Exception e) {
129            Main.error(e);
130            HelpAwareOptionPane.showMessageDialogInEDT(
131                    Main.parent,
132                    tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
133                    tr("Error"),
134                    JOptionPane.ERROR_MESSAGE, null
135            );
136            return false;
137        }
138    }
139
140    /**
141     * If multiple files (with multiple file formats) are selected,
142     * they are opened in the order of their priorities.
143     * Highest priority comes first.
144     */
145    public double getPriority() {
146        return 0;
147    }
148
149    @Override
150    public int compareTo(FileImporter other) {
151        return Double.compare(this.getPriority(), other.getPriority());
152    }
153
154    /**
155     * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
156     * @return true if this {@code FileImporter} is enabled
157     * @since 5459
158     */
159    public final boolean isEnabled() {
160        return enabled;
161    }
162
163    /**
164     * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
165     * @param enabled true to enable this {@code FileImporter}, false to disable it
166     * @since 5459
167     */
168    public final void setEnabled(boolean enabled) {
169        this.enabled = enabled;
170    }
171
172    @Override
173    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
174        // To be overriden by subclasses if their enabled state depends of the active layer nature
175    }
176
177    @Override
178    public void layerAdded(Layer newLayer) {
179        // To be overriden by subclasses if needed
180    }
181
182    @Override
183    public void layerRemoved(Layer oldLayer) {
184        // To be overriden by subclasses if needed
185    }
186}