001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.io;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.io.File;
007    import java.io.IOException;
008    
009    import org.openstreetmap.josm.actions.ExtensionFileFilter;
010    import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
011    import org.openstreetmap.josm.gui.layer.Layer;
012    
013    public 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    }