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;
|
---|
11 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
12 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Abstract base class for file exporters - IO classes that save layers to a file.
|
---|
16 | */
|
---|
17 | public abstract class FileExporter implements ActiveLayerChangeListener {
|
---|
18 |
|
---|
19 | public final ExtensionFileFilter filter;
|
---|
20 |
|
---|
21 | private boolean enabled;
|
---|
22 | private boolean canceled;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code FileExporter}.
|
---|
26 | * @param filter The extension file filter
|
---|
27 | */
|
---|
28 | public FileExporter(ExtensionFileFilter filter) {
|
---|
29 | this.filter = filter;
|
---|
30 | this.enabled = true;
|
---|
31 | }
|
---|
32 |
|
---|
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 | */
|
---|
41 | public boolean acceptFile(File pathname, Layer layer) {
|
---|
42 | return filter.acceptName(pathname.getName());
|
---|
43 | }
|
---|
44 |
|
---|
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 | */
|
---|
52 | public void exportData(File file, Layer layer) throws IOException {
|
---|
53 | throw new IOException(tr("Could not export ''{0}''.", file.getName()));
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
|
---|
58 | * @return true if this {@code FileExporter} is enabled
|
---|
59 | * @since 5459
|
---|
60 | */
|
---|
61 | public final boolean isEnabled() {
|
---|
62 | return enabled;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
|
---|
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
|
---|
75 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
76 | // To be overriden by subclasses if their enabled state depends of the active layer nature
|
---|
77 | }
|
---|
78 |
|
---|
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 | }
|
---|
96 | }
|
---|