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
019    public FileExporter(ExtensionFileFilter filter) {
020        this.filter = filter;
021        this.enabled = true;
022    }
023
024    public boolean acceptFile(File pathname, Layer layer) {
025        return filter.acceptName(pathname.getName());
026    }
027
028    public void exportData(File file, Layer layer) throws IOException {
029        throw new IOException(tr("Could not export ''{0}''.", file.getName()));
030    }
031
032    /**
033     * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
034     * @return true if this {@code FileExporter} is enabled
035     * @since 5459
036     */
037    public final boolean isEnabled() {
038        return enabled;
039    }
040
041    /**
042     * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
043     * @param enabled true to enable this {@code FileExporter}, false to disable it
044     * @since 5459
045     */
046    public final void setEnabled(boolean enabled) {
047        this.enabled = enabled;
048    }
049
050    @Override
051    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
052        // To be overriden by subclasses if their enabled state depends of the active layer nature
053    }
054
055    @Override
056    public void layerAdded(Layer newLayer) {
057        // To be overriden by subclasses if needed
058    }
059
060    @Override
061    public void layerRemoved(Layer oldLayer) {
062        // To be overriden by subclasses if needed
063    }
064}