source: josm/trunk/src/org/openstreetmap/josm/io/FileImporter.java@ 7315

Last change on this file since 7315 was 7119, checked in by Don-vip, 10 years ago

code refactoring/cleanup/javadoc + fix bug in preset text comparator in menu

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.IOException;
8import java.util.List;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.ExtensionFileFilter;
14import org.openstreetmap.josm.gui.HelpAwareOptionPane;
15import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
16import org.openstreetmap.josm.gui.Notification;
17import org.openstreetmap.josm.gui.layer.Layer;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20
21public abstract class FileImporter implements Comparable<FileImporter>, LayerChangeListener {
22
23 public final ExtensionFileFilter filter;
24
25 private boolean enabled;
26
27 public FileImporter(ExtensionFileFilter filter) {
28 this.filter = filter;
29 this.enabled = true;
30 }
31
32 public boolean acceptFile(File pathname) {
33 return filter.acceptName(pathname.getName());
34 }
35
36 /**
37 * A batch importer is a file importer that prefers to read multiple files at the same time.
38 */
39 public boolean isBatchImporter() {
40 return false;
41 }
42
43 /**
44 * Needs to be implemented if isBatchImporter() returns false.
45 */
46 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
47 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
48 }
49
50 /**
51 * Needs to be implemented if isBatchImporter() returns true.
52 */
53 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
54 throw new IOException(tr("Could not import files."));
55 }
56
57 /**
58 * Wrapper to give meaningful output if things go wrong.
59 * @return true if data import was successful
60 */
61 public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
62 try {
63 Main.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
64 importData(f, progressMonitor);
65 return true;
66 } catch (IllegalDataException e) {
67 Throwable cause = e.getCause();
68 if (cause instanceof ImportCancelException) {
69 displayCancel(cause);
70 } else {
71 displayError(f, e);
72 }
73 return false;
74 } catch (Exception e) {
75 displayError(f, e);
76 return false;
77 }
78 }
79
80 private static void displayError(File f, Exception e) {
81 Main.error(e);
82 HelpAwareOptionPane.showMessageDialogInEDT(
83 Main.parent,
84 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
85 tr("Error"),
86 JOptionPane.ERROR_MESSAGE, null
87 );
88 }
89
90 private static void displayCancel(final Throwable t) {
91 GuiHelper.runInEDTAndWait(new Runnable() {
92 @Override
93 public void run() {
94 Notification note = new Notification(t.getMessage());
95 note.setIcon(JOptionPane.INFORMATION_MESSAGE);
96 note.setDuration(Notification.TIME_SHORT);
97 note.show();
98 }
99 });
100 }
101
102 public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
103 try {
104 Main.info("Open "+files.size()+" files");
105 importData(files, progressMonitor);
106 return true;
107 } catch (Exception e) {
108 Main.error(e);
109 HelpAwareOptionPane.showMessageDialogInEDT(
110 Main.parent,
111 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
112 tr("Error"),
113 JOptionPane.ERROR_MESSAGE, null
114 );
115 return false;
116 }
117 }
118
119 /**
120 * If multiple files (with multiple file formats) are selected,
121 * they are opened in the order of their priorities.
122 * Highest priority comes first.
123 */
124 public double getPriority() {
125 return 0;
126 }
127
128 @Override
129 public int compareTo(FileImporter other) {
130 return Double.compare(this.getPriority(), other.getPriority());
131 }
132
133 /**
134 * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
135 * @return true if this {@code FileImporter} is enabled
136 * @since 5459
137 */
138 public final boolean isEnabled() {
139 return enabled;
140 }
141
142 /**
143 * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
144 * @param enabled true to enable this {@code FileImporter}, false to disable it
145 * @since 5459
146 */
147 public final void setEnabled(boolean enabled) {
148 this.enabled = enabled;
149 }
150
151 @Override
152 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
153 // To be overriden by subclasses if their enabled state depends of the active layer nature
154 }
155
156 @Override
157 public void layerAdded(Layer newLayer) {
158 // To be overriden by subclasses if needed
159 }
160
161 @Override
162 public void layerRemoved(Layer oldLayer) {
163 // To be overriden by subclasses if needed
164 }
165}
Note: See TracBrowser for help on using the repository browser.