source: josm/trunk/src/org/openstreetmap/josm/gui/io/importexport/FileExporter.java@ 15496

Last change on this file since 15496 was 15496, checked in by Don-vip, 4 years ago

fix #16796 - Rework of GPX track colors / layer preferences (patch by Bjoeni)

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io.importexport;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.IOException;
8
9import org.openstreetmap.josm.actions.ExtensionFileFilter;
10import org.openstreetmap.josm.gui.layer.Layer;
11import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
12import 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 */
17public abstract class FileExporter implements ActiveLayerChangeListener {
18
19 /** the ExtensionFileFilter filter used by this exporter */
20 public final ExtensionFileFilter filter;
21
22 private boolean enabled;
23 private boolean canceled;
24
25 /**
26 * Constructs a new {@code FileExporter}.
27 * @param filter The extension file filter
28 */
29 public FileExporter(ExtensionFileFilter filter) {
30 this.filter = filter;
31 this.enabled = true;
32 }
33
34 /**
35 * Check if this exporter can export a certain layer to a certain file.
36 *
37 * Most exporters support just a single layer type.
38 * @param pathname the target file name (check file extension using the {@link #filter}
39 * @param layer the layer requested for export
40 * @return true, if the exporter can handle the layer and filename is okay
41 */
42 public boolean acceptFile(File pathname, Layer layer) {
43 return filter.acceptName(pathname.getName());
44 }
45
46 /**
47 * Execute the data export. (To be overridden by subclasses.)
48 *
49 * @param file target file
50 * @param layer the layer to export
51 * @throws IOException in case of an IO error
52 */
53 public void exportData(File file, Layer layer) throws IOException {
54 throw new IOException(tr("Could not export ''{0}''.", file.getName()));
55 }
56
57 /**
58 * Execute the data export without prompting the user. (To be overridden by subclasses.)
59 *
60 * @param file target file
61 * @param layer the layer to export
62 * @throws IOException in case of an IO error
63 * @since 15496
64 */
65 public void exportDataQuiet(File file, Layer layer) throws IOException {
66 exportData(file, layer); //backwards compatibility
67 }
68
69 /**
70 * Returns the enabled state of this {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
71 * @return true if this {@code FileExporter} is enabled
72 * @since 5459
73 */
74 public final boolean isEnabled() {
75 return enabled;
76 }
77
78 /**
79 * Sets the enabled state of the {@code FileExporter}. When enabled, it is listed and usable in "File->Save" dialogs.
80 * @param enabled true to enable this {@code FileExporter}, false to disable it
81 * @since 5459
82 */
83 public final void setEnabled(boolean enabled) {
84 this.enabled = enabled;
85 }
86
87 @Override
88 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
89 // To be overriden by subclasses if their enabled state depends of the active layer nature
90 }
91
92 /**
93 * Determines if this exporter has been canceled during export.
94 * @return true if this {@code FileExporter} has been canceled
95 * @since 6815
96 */
97 public final boolean isCanceled() {
98 return canceled;
99 }
100
101 /**
102 * Marks this exporter as canceled.
103 * @param canceled true to mark this exporter as canceled, {@code false} otherwise
104 * @since 6815
105 */
106 public final void setCanceled(boolean canceled) {
107 this.canceled = canceled;
108 }
109}
Note: See TracBrowser for help on using the repository browser.