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