[1949] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.File;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 |
|
---|
| 9 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
| 10 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[10345] | 11 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
| 12 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
[1949] | 13 |
|
---|
[12470] | 14 | /**
|
---|
| 15 | * Abstract base class for file exporters - IO classes that save layers to a file.
|
---|
| 16 | */
|
---|
[10345] | 17 | public abstract class FileExporter implements ActiveLayerChangeListener {
|
---|
[6070] | 18 |
|
---|
[5459] | 19 | public final ExtensionFileFilter filter;
|
---|
[1949] | 20 |
|
---|
[5459] | 21 | private boolean enabled;
|
---|
[6815] | 22 | private boolean canceled;
|
---|
[5459] | 23 |
|
---|
[6815] | 24 | /**
|
---|
| 25 | * Constructs a new {@code FileExporter}.
|
---|
| 26 | * @param filter The extension file filter
|
---|
| 27 | */
|
---|
[1949] | 28 | public FileExporter(ExtensionFileFilter filter) {
|
---|
| 29 | this.filter = filter;
|
---|
[5459] | 30 | this.enabled = true;
|
---|
[1949] | 31 | }
|
---|
| 32 |
|
---|
[12470] | 33 | /**
|
---|
| 34 | * Check if this exporter can export a certain layer to a certain file.
|
---|
| 35 | *
|
---|
| 36 | * Most exporters support just a single layer type.
|
---|
| 37 | * @param pathname the target file name (check file extension using the {@link #filter}
|
---|
| 38 | * @param layer the layer requested for export
|
---|
| 39 | * @return true, if the exporter can handle the layer and filename is okay
|
---|
| 40 | */
|
---|
[1949] | 41 | public boolean acceptFile(File pathname, Layer layer) {
|
---|
| 42 | return filter.acceptName(pathname.getName());
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[12470] | 45 | /**
|
---|
| 46 | * Execute the data export. (To be overridden by subclasses.)
|
---|
| 47 | *
|
---|
| 48 | * @param file target file
|
---|
| 49 | * @param layer the layer to export
|
---|
| 50 | * @throws IOException in case of an IO error
|
---|
| 51 | */
|
---|
[1949] | 52 | public void exportData(File file, Layer layer) throws IOException {
|
---|
[2181] | 53 | throw new IOException(tr("Could not export ''{0}''.", file.getName()));
|
---|
[1949] | 54 | }
|
---|
[5459] | 55 |
|
---|
| 56 | /**
|
---|
[6830] | 57 | * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
|
---|
[5459] | 58 | * @return true if this {@code FileExporter} is enabled
|
---|
| 59 | * @since 5459
|
---|
| 60 | */
|
---|
| 61 | public final boolean isEnabled() {
|
---|
| 62 | return enabled;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
[6830] | 66 | * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
|
---|
[5459] | 67 | * @param enabled true to enable this {@code FileExporter}, false to disable it
|
---|
| 68 | * @since 5459
|
---|
| 69 | */
|
---|
| 70 | public final void setEnabled(boolean enabled) {
|
---|
| 71 | this.enabled = enabled;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | @Override
|
---|
[10345] | 75 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
[5459] | 76 | // To be overriden by subclasses if their enabled state depends of the active layer nature
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[6815] | 79 | /**
|
---|
| 80 | * Determines if this exporter has been canceled during export.
|
---|
| 81 | * @return true if this {@code FileExporter} has been canceled
|
---|
| 82 | * @since 6815
|
---|
| 83 | */
|
---|
| 84 | public final boolean isCanceled() {
|
---|
| 85 | return canceled;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Marks this exporter as canceled.
|
---|
| 90 | * @param canceled true to mark this exporter as canceled, {@code false} otherwise
|
---|
| 91 | * @since 6815
|
---|
| 92 | */
|
---|
| 93 | public final void setCanceled(boolean canceled) {
|
---|
| 94 | this.canceled = canceled;
|
---|
| 95 | }
|
---|
[1949] | 96 | }
|
---|