001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io.importexport;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.File;
007import java.io.IOException;
008
009import org.openstreetmap.josm.actions.ExtensionFileFilter;
010import org.openstreetmap.josm.gui.layer.Layer;
011import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
012import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
013
014/**
015 * Abstract base class for file exporters - IO classes that save layers to a file.
016 */
017public abstract class FileExporter implements ActiveLayerChangeListener {
018
019    /** the  ExtensionFileFilter filter used by this exporter */
020    public final ExtensionFileFilter filter;
021
022    private boolean enabled;
023    private boolean canceled;
024
025    /**
026     * Constructs a new {@code FileExporter}.
027     * @param filter The extension file filter
028     */
029    public FileExporter(ExtensionFileFilter filter) {
030        this.filter = filter;
031        this.enabled = true;
032    }
033
034    /**
035     * Check if this exporter can export a certain layer to a certain file.
036     *
037     * Most exporters support just a single layer type.
038     * @param pathname the target file name (check file extension using the {@link #filter}
039     * @param layer the layer requested for export
040     * @return true, if the exporter can handle the layer and filename is okay
041     */
042    public boolean acceptFile(File pathname, Layer layer) {
043        return filter.acceptName(pathname.getName());
044    }
045
046    /**
047     * Execute the data export. (To be overridden by subclasses.)
048     *
049     * @param file target file
050     * @param layer the layer to export
051     * @throws IOException in case of an IO error
052     */
053    public void exportData(File file, Layer layer) throws IOException {
054        throw new IOException(tr("Could not export ''{0}''.", file.getName()));
055    }
056
057    /**
058     * Execute the data export without prompting the user. (To be overridden by subclasses.)
059     *
060     * @param file target file
061     * @param layer the layer to export
062     * @throws IOException in case of an IO error
063     * @since 15496
064     */
065    public void exportDataQuiet(File file, Layer layer) throws IOException {
066        exportData(file, layer); //backwards compatibility
067    }
068
069    /**
070     * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
071     * @return true if this {@code FileExporter} is enabled
072     * @since 5459
073     */
074    public final boolean isEnabled() {
075        return enabled;
076    }
077
078    /**
079     * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
080     * @param enabled true to enable this {@code FileExporter}, false to disable it
081     * @since 5459
082     */
083    public final void setEnabled(boolean enabled) {
084        this.enabled = enabled;
085    }
086
087    @Override
088    public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
089        // To be overriden by subclasses if their enabled state depends of the active layer nature
090    }
091
092    /**
093     * Determines if this exporter has been canceled during export.
094     * @return true if this {@code FileExporter} has been canceled
095     * @since 6815
096     */
097    public final boolean isCanceled() {
098        return canceled;
099    }
100
101    /**
102     * Marks this exporter as canceled.
103     * @param canceled true to mark this exporter as canceled, {@code false} otherwise
104     * @since 6815
105     */
106    public final void setCanceled(boolean canceled) {
107        this.canceled = canceled;
108    }
109}