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     * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
059     * @return true if this {@code FileExporter} is enabled
060     * @since 5459
061     */
062    public final boolean isEnabled() {
063        return enabled;
064    }
065
066    /**
067     * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
068     * @param enabled true to enable this {@code FileExporter}, false to disable it
069     * @since 5459
070     */
071    public final void setEnabled(boolean enabled) {
072        this.enabled = enabled;
073    }
074
075    @Override
076    public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
077        // To be overriden by subclasses if their enabled state depends of the active layer nature
078    }
079
080    /**
081     * Determines if this exporter has been canceled during export.
082     * @return true if this {@code FileExporter} has been canceled
083     * @since 6815
084     */
085    public final boolean isCanceled() {
086        return canceled;
087    }
088
089    /**
090     * Marks this exporter as canceled.
091     * @param canceled true to mark this exporter as canceled, {@code false} otherwise
092     * @since 6815
093     */
094    public final void setCanceled(boolean canceled) {
095        this.canceled = canceled;
096    }
097}