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;
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
014public abstract class FileExporter implements ActiveLayerChangeListener {
015
016    public final ExtensionFileFilter filter;
017
018    private boolean enabled;
019    private boolean canceled;
020
021    /**
022     * Constructs a new {@code FileExporter}.
023     * @param filter The extension file filter
024     */
025    public FileExporter(ExtensionFileFilter filter) {
026        this.filter = filter;
027        this.enabled = true;
028    }
029
030    public boolean acceptFile(File pathname, Layer layer) {
031        return filter.acceptName(pathname.getName());
032    }
033
034    public void exportData(File file, Layer layer) throws IOException {
035        throw new IOException(tr("Could not export ''{0}''.", file.getName()));
036    }
037
038    /**
039     * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
040     * @return true if this {@code FileExporter} is enabled
041     * @since 5459
042     */
043    public final boolean isEnabled() {
044        return enabled;
045    }
046
047    /**
048     * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
049     * @param enabled true to enable this {@code FileExporter}, false to disable it
050     * @since 5459
051     */
052    public final void setEnabled(boolean enabled) {
053        this.enabled = enabled;
054    }
055
056    @Override
057    public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
058        // To be overriden by subclasses if their enabled state depends of the active layer nature
059    }
060
061    /**
062     * Determines if this exporter has been canceled during export.
063     * @return true if this {@code FileExporter} has been canceled
064     * @since 6815
065     */
066    public final boolean isCanceled() {
067        return canceled;
068    }
069
070    /**
071     * Marks this exporter as canceled.
072     * @param canceled true to mark this exporter as canceled, {@code false} otherwise
073     * @since 6815
074     */
075    public final void setCanceled(boolean canceled) {
076        this.canceled = canceled;
077    }
078}