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 public final ExtensionFileFilter filter; 020 021 private boolean enabled; 022 private boolean canceled; 023 024 /** 025 * Constructs a new {@code FileExporter}. 026 * @param filter The extension file filter 027 */ 028 public FileExporter(ExtensionFileFilter filter) { 029 this.filter = filter; 030 this.enabled = true; 031 } 032 033 /** 034 * Check if this exporter can export a certain layer to a certain file. 035 * 036 * Most exporters support just a single layer type. 037 * @param pathname the target file name (check file extension using the {@link #filter} 038 * @param layer the layer requested for export 039 * @return true, if the exporter can handle the layer and filename is okay 040 */ 041 public boolean acceptFile(File pathname, Layer layer) { 042 return filter.acceptName(pathname.getName()); 043 } 044 045 /** 046 * Execute the data export. (To be overridden by subclasses.) 047 * 048 * @param file target file 049 * @param layer the layer to export 050 * @throws IOException in case of an IO error 051 */ 052 public void exportData(File file, Layer layer) throws IOException { 053 throw new IOException(tr("Could not export ''{0}''.", file.getName())); 054 } 055 056 /** 057 * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs. 058 * @return true if this {@code FileExporter} is enabled 059 * @since 5459 060 */ 061 public final boolean isEnabled() { 062 return enabled; 063 } 064 065 /** 066 * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs. 067 * @param enabled true to enable this {@code FileExporter}, false to disable it 068 * @since 5459 069 */ 070 public final void setEnabled(boolean enabled) { 071 this.enabled = enabled; 072 } 073 074 @Override 075 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) { 076 // To be overriden by subclasses if their enabled state depends of the active layer nature 077 } 078 079 /** 080 * Determines if this exporter has been canceled during export. 081 * @return true if this {@code FileExporter} has been canceled 082 * @since 6815 083 */ 084 public final boolean isCanceled() { 085 return canceled; 086 } 087 088 /** 089 * Marks this exporter as canceled. 090 * @param canceled true to mark this exporter as canceled, {@code false} otherwise 091 * @since 6815 092 */ 093 public final void setCanceled(boolean canceled) { 094 this.canceled = canceled; 095 } 096}