Ticket #10024: 10024_beta1.patch
| File 10024_beta1.patch, 65.6 KB (added by , 12 years ago) |
|---|
-
src/org/openstreetmap/josm/actions/DiskAccessAction.java
6 6 import javax.swing.JFileChooser; 7 7 import javax.swing.filechooser.FileFilter; 8 8 9 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 9 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 10 import org.openstreetmap.josm.gui.widgets.FileChooserManager; 10 11 import org.openstreetmap.josm.tools.Shortcut; 11 12 12 13 /** … … 45 46 } 46 47 47 48 /** 48 * Creates a new {@link JFileChooser} and makes it visible.49 * Creates a new {@link AbstractFileChooser} and makes it visible. 49 50 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 50 51 * @param multiple If true, makes the dialog allow multiple file selections 51 52 * @param title The string that goes in the dialog window's title bar 52 * @return The {@code JFileChooser}.53 * @return The {@code AbstractFileChooser}. 53 54 * @since 1646 54 55 */ 55 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {56 public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) { 56 57 return createAndOpenFileChooser(open, multiple, title, null); 57 58 } 58 59 59 60 /** 60 * Creates a new {@link JFileChooser} and makes it visible.61 * Creates a new {@link AbstractFileChooser} and makes it visible. 61 62 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 62 63 * @param multiple If true, makes the dialog allow multiple file selections 63 64 * @param title The string that goes in the dialog window's title bar 64 65 * @param extension The file extension that will be selected as the default file filter 65 * @return The {@code JFileChooser}.66 * @return The {@code AbstractFileChooser}. 66 67 * @since 2020 67 68 */ 68 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) {69 public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) { 69 70 return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null); 70 71 } 71 72 72 73 /** 73 * Creates a new {@link JFileChooser} and makes it visible.74 * Creates a new {@link AbstractFileChooser} and makes it visible. 74 75 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 75 76 * @param multiple If true, makes the dialog allow multiple file selections 76 77 * @param title The string that goes in the dialog window's title bar … … 81 82 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul> 82 83 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 83 84 * If false, only the file filters that include {@code extension} will be proposed 84 * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory.85 * @param lastDirProperty The name of the property used to setup the AbstractFileChooser initial directory. 85 86 * This property will then be updated to the new "last directory" chosen by the user. If null, the default property "lastDirectory" will be used. 86 * @return The {@code JFileChooser}.87 * @return The {@code AbstractFileChooser}. 87 88 * @since 5438 88 89 */ 89 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension, int selectionMode, boolean allTypes, String lastDirProperty) {90 return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, extension, allTypes, selectionMode).openFileChooser();90 public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension, int selectionMode, boolean allTypes, String lastDirProperty) { 91 return new FileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, extension, allTypes, selectionMode).openFileChooser(); 91 92 } 92 93 93 94 /** 94 * Creates a new {@link JFileChooser} for a single {@link FileFilter} and makes it visible.95 * Creates a new {@link AbstractFileChooser} for a single {@link FileFilter} and makes it visible. 95 96 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 96 97 * @param multiple If true, makes the dialog allow multiple file selections 97 98 * @param title The string that goes in the dialog window's title bar … … 100 101 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 101 102 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 102 103 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul> 103 * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the user104 * @return The {@code JFileChooser}.104 * @param lastDirProperty The name of the property used to setup the AbstractFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the user 105 * @return The {@code AbstractFileChooser}. 105 106 * @since 5438 106 107 */ 107 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, FileFilter filter, int selectionMode, String lastDirProperty) {108 return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filter, selectionMode).openFileChooser();108 public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, FileFilter filter, int selectionMode, String lastDirProperty) { 109 return new FileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filter, selectionMode).openFileChooser(); 109 110 } 110 111 111 112 /** 112 * Creates a new {@link JFileChooser} for several {@link FileFilter}s and makes it visible.113 * Creates a new {@link AbstractFileChooser} for several {@link FileFilter}s and makes it visible. 113 114 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 114 115 * @param multiple If true, makes the dialog allow multiple file selections 115 116 * @param title The string that goes in the dialog window's title bar … … 120 121 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 121 122 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul> 122 123 * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the user 123 * @return The {@code JFileChooser}.124 * @return The {@code AbstractFileChooser}. 124 125 * @since 5438 125 126 */ 126 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title,127 public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, 127 128 Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode, String lastDirProperty) { 128 return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filters, defaultFilter, selectionMode).openFileChooser();129 return new FileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filters, defaultFilter, selectionMode).openFileChooser(); 129 130 } 130 131 } -
src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
9 9 import java.util.List; 10 10 import java.util.ServiceConfigurationError; 11 11 12 import javax.swing.JFileChooser;13 12 import javax.swing.filechooser.FileFilter; 14 13 15 14 import org.openstreetmap.josm.Main; 16 15 import org.openstreetmap.josm.gui.MapView; 16 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 17 17 import org.openstreetmap.josm.io.AllFormatsImporter; 18 18 import org.openstreetmap.josm.io.FileExporter; 19 19 import org.openstreetmap.josm.io.FileImporter; … … 223 223 } 224 224 225 225 /** 226 * Applies the choosable {@link FileFilter} to a {@link JFileChooser} before using the226 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the 227 227 * file chooser for selecting a file for reading. 228 228 * 229 229 * @param fileChooser the file chooser … … 232 232 * If false, only the file filters that include {@code extension} will be proposed 233 233 * @since 5438 234 234 */ 235 public static void applyChoosableImportFileFilters( JFileChooser fileChooser, String extension, boolean allTypes) {235 public static void applyChoosableImportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) { 236 236 for (ExtensionFileFilter filter: getImportExtensionFileFilters()) { 237 237 if (allTypes || filter.acceptName("file."+extension)) { 238 238 fileChooser.addChoosableFileFilter(filter); … … 242 242 } 243 243 244 244 /** 245 * Applies the choosable {@link FileFilter} to a {@link JFileChooser} before using the245 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the 246 246 * file chooser for selecting a file for writing. 247 247 * 248 248 * @param fileChooser the file chooser … … 251 251 * If false, only the file filters that include {@code extension} will be proposed 252 252 * @since 5438 253 253 */ 254 public static void applyChoosableExportFileFilters( JFileChooser fileChooser, String extension, boolean allTypes) {254 public static void applyChoosableExportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) { 255 255 for (ExtensionFileFilter filter: getExportExtensionFileFilters()) { 256 256 if (allTypes || filter.acceptName("file."+extension)) { 257 257 fileChooser.addChoosableFileFilter(filter); -
src/org/openstreetmap/josm/actions/OpenFileAction.java
24 24 import java.util.regex.Matcher; 25 25 import java.util.regex.Pattern; 26 26 27 import javax.swing.JFileChooser;28 27 import javax.swing.JOptionPane; 29 28 import javax.swing.SwingUtilities; 30 29 import javax.swing.filechooser.FileFilter; … … 33 32 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 34 33 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 35 34 import org.openstreetmap.josm.gui.help.HelpUtil; 35 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 36 36 import org.openstreetmap.josm.io.AllFormatsImporter; 37 37 import org.openstreetmap.josm.io.FileImporter; 38 38 import org.openstreetmap.josm.io.OsmTransferException; … … 64 64 65 65 @Override 66 66 public void actionPerformed(ActionEvent e) { 67 JFileChooser fc = createAndOpenFileChooser(true, true, null);67 AbstractFileChooser fc = createAndOpenFileChooser(true, true, null); 68 68 if (fc == null) 69 69 return; 70 70 File[] files = fc.getSelectedFiles(); -
src/org/openstreetmap/josm/actions/SaveActionBase.java
18 18 import org.openstreetmap.josm.gui.ExtendedDialog; 19 19 import org.openstreetmap.josm.gui.layer.Layer; 20 20 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 21 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 21 22 import org.openstreetmap.josm.io.FileExporter; 22 23 import org.openstreetmap.josm.tools.Shortcut; 23 24 … … 135 136 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 136 137 */ 137 138 public static File createAndOpenSaveFileChooser(String title, ExtensionFileFilter filter) { 138 JFileChooser fc = createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, null);139 AbstractFileChooser fc = createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, null); 139 140 return checkFileAndConfirmOverWrite(fc, filter.getDefaultExtension()); 140 141 } 141 142 … … 149 150 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, String) 150 151 */ 151 152 public static File createAndOpenSaveFileChooser(String title, String extension) { 152 JFileChooser fc = createAndOpenFileChooser(false, false, title, extension);153 AbstractFileChooser fc = createAndOpenFileChooser(false, false, title, extension); 153 154 return checkFileAndConfirmOverWrite(fc, extension); 154 155 } 155 156 156 private static File checkFileAndConfirmOverWrite( JFileChooser fc, String extension) {157 private static File checkFileAndConfirmOverWrite(AbstractFileChooser fc, String extension) { 157 158 if (fc == null) return null; 158 159 File file = fc.getSelectedFile(); 159 160 -
src/org/openstreetmap/josm/actions/SessionLoadAction.java
24 24 import org.openstreetmap.josm.gui.layer.Layer; 25 25 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 26 26 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 27 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 27 28 import org.openstreetmap.josm.io.IllegalDataException; 28 29 import org.openstreetmap.josm.io.session.SessionImporter; 29 30 import org.openstreetmap.josm.io.session.SessionReader; … … 46 47 47 48 @Override 48 49 public void actionPerformed(ActionEvent e) { 49 JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"),50 AbstractFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"), 50 51 Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()), 51 52 SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory"); 52 53 if (fc == null) return; -
src/org/openstreetmap/josm/actions/SessionSaveAsAction.java
35 35 import org.openstreetmap.josm.gui.ExtendedDialog; 36 36 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 37 37 import org.openstreetmap.josm.gui.layer.Layer; 38 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 38 39 import org.openstreetmap.josm.io.session.SessionLayerExporter; 39 40 import org.openstreetmap.josm.io.session.SessionWriter; 40 41 import org.openstreetmap.josm.tools.GBC; … … 81 82 FileFilter joz = new ExtensionFileFilter("joz", "joz", tr("Session file (archive) (*.joz)")); 82 83 FileFilter jos = new ExtensionFileFilter("jos", "jos", tr("Session file (*.jos)")); 83 84 84 JFileChooser fc;85 AbstractFileChooser fc; 85 86 86 87 if (zipRequired) { 87 88 fc = createAndOpenFileChooser(false, false, tr("Save session"), joz, JFileChooser.FILES_ONLY, "lastDirectory"); -
src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java
72 72 import org.openstreetmap.josm.gui.preferences.SourceEntry; 73 73 import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference; 74 74 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 75 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 76 import org.openstreetmap.josm.gui.widgets.FileChooserManager; 75 77 import org.openstreetmap.josm.gui.widgets.HtmlPanel; 76 import org.openstreetmap.josm.gui.widgets.JFileChooserManager;77 78 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 78 79 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 79 80 import org.openstreetmap.josm.tools.GBC; … … 442 443 return; 443 444 final StyleSource s = model.getRow(sel); 444 445 445 JFileChooserManager fcm = new JFileChooserManager(false, "mappaint.clone-style.lastDirectory", System.getProperty("user.home"));446 FileChooserManager fcm = new FileChooserManager(false, "mappaint.clone-style.lastDirectory", System.getProperty("user.home")); 446 447 String suggestion = fcm.getInitialDirectory() + File.separator + s.getFileNamePart(); 447 448 448 449 FileFilter ff; … … 453 454 } 454 455 fcm.createFileChooser(false, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY) 455 456 .getFileChooser().setSelectedFile(new File(suggestion)); 456 JFileChooser fc = fcm.openFileChooser();457 AbstractFileChooser fc = fcm.openFileChooser(); 457 458 if (fc == null) 458 459 return; 459 460 Main.worker.submit(new SaveToFileTask(s, fc.getSelectedFile())); -
src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java
71 71 import org.openstreetmap.josm.gui.ExtendedDialog; 72 72 import org.openstreetmap.josm.gui.layer.GpxLayer; 73 73 import org.openstreetmap.josm.gui.layer.Layer; 74 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 74 75 import org.openstreetmap.josm.gui.widgets.JosmComboBox; 75 76 import org.openstreetmap.josm.gui.widgets.JosmTextField; 76 77 import org.openstreetmap.josm.io.GpxReader; … … 150 151 return tr("GPX Files (*.gpx *.gpx.gz)"); 151 152 } 152 153 }; 153 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, filter, JFileChooser.FILES_ONLY, null);154 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, filter, JFileChooser.FILES_ONLY, null); 154 155 if (fc == null) 155 156 return; 156 157 File sel = fc.getSelectedFile(); … … 382 383 383 384 @Override 384 385 public void actionPerformed(ActionEvent ae) { 385 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, JpegFileFilter.getInstance(),386 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, JpegFileFilter.getInstance(), 386 387 JFileChooser.FILES_ONLY, "geoimage.lastdirectory"); 387 388 if (fc == null) 388 389 return; -
src/org/openstreetmap/josm/gui/layer/gpx/ImportAudioAction.java
29 29 import org.openstreetmap.josm.gui.layer.GpxLayer; 30 30 import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker; 31 31 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 32 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 32 33 import org.openstreetmap.josm.tools.AudioUtil; 33 34 import org.openstreetmap.josm.tools.ImageProvider; 34 35 import org.openstreetmap.josm.tools.Utils; … … 77 78 return tr("Wave Audio files (*.wav)"); 78 79 } 79 80 }; 80 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, true, null, filter, JFileChooser.FILES_ONLY, "markers.lastaudiodirectory");81 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, true, null, filter, JFileChooser.FILES_ONLY, "markers.lastaudiodirectory"); 81 82 if (fc != null) { 82 83 File[] sel = fc.getSelectedFiles(); 83 84 // sort files in increasing order of timestamp (this is the end time, but so -
src/org/openstreetmap/josm/gui/layer/gpx/ImportImagesAction.java
17 17 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 18 18 import org.openstreetmap.josm.gui.layer.GpxLayer; 19 19 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 20 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 20 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 21 import org.openstreetmap.josm.gui.widgets.FileChooserManager; 21 22 import org.openstreetmap.josm.io.JpgImporter; 22 23 import org.openstreetmap.josm.tools.ImageProvider; 23 24 … … 52 53 return; 53 54 } 54 55 JpgImporter importer = new JpgImporter(layer); 55 JFileChooser fc = new JFileChooserManager(true, "geoimage.lastdirectory", Main.pref.get("lastDirectory")).createFileChooser(true, null, importer.filter, JFileChooser.FILES_AND_DIRECTORIES).openFileChooser(); 56 AbstractFileChooser fc = new FileChooserManager(true, "geoimage.lastdirectory", Main.pref.get("lastDirectory")). 57 createFileChooser(true, null, importer.filter, JFileChooser.FILES_AND_DIRECTORIES).openFileChooser(); 56 58 if (fc != null) { 57 59 File[] sel = fc.getSelectedFiles(); 58 60 if (sel != null && sel.length > 0) { -
src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
88 88 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 89 89 import org.openstreetmap.josm.gui.util.GuiHelper; 90 90 import org.openstreetmap.josm.gui.util.TableHelper; 91 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 91 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 92 import org.openstreetmap.josm.gui.widgets.FileChooserManager; 92 93 import org.openstreetmap.josm.gui.widgets.JosmTextField; 93 94 import org.openstreetmap.josm.io.CachedFile; 94 95 import org.openstreetmap.josm.io.OnlineResource; … … 700 701 } 701 702 } 702 703 703 private static void prepareFileChooser(String url, JFileChooser fc) {704 private static void prepareFileChooser(String url, AbstractFileChooser fc) { 704 705 if (url == null || url.trim().length() == 0) return; 705 706 URL sourceUrl = null; 706 707 try { … … 812 813 Main.error("Unsupported source type: "+sourceType); 813 814 return; 814 815 } 815 JFileChooserManager fcm = new JFileChooserManager(true)816 FileChooserManager fcm = new FileChooserManager(true) 816 817 .createFileChooser(true, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY); 817 818 prepareFileChooser(tfURL.getText(), fcm.getFileChooser()); 818 JFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));819 AbstractFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this)); 819 820 if (fc != null) { 820 821 tfURL.setText(fc.getSelectedFile().toString()); 821 822 } … … 1511 1512 1512 1513 @Override 1513 1514 public void actionPerformed(ActionEvent e) { 1514 JFileChooserManager fcm = new JFileChooserManager(true).createFileChooser();1515 FileChooserManager fcm = new FileChooserManager(true).createFileChooser(); 1515 1516 if (!isFile) { 1516 1517 fcm.getFileChooser().setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 1517 1518 } 1518 1519 prepareFileChooser(tfFileName.getText(), fcm.getFileChooser()); 1519 JFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));1520 AbstractFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this)); 1520 1521 if (fc != null) { 1521 1522 tfFileName.setText(fc.getSelectedFile().toString()); 1522 1523 } -
src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java
45 45 import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory; 46 46 import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; 47 47 import org.openstreetmap.josm.gui.util.GuiHelper; 48 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 48 49 import org.openstreetmap.josm.gui.widgets.JosmTextField; 49 50 import org.openstreetmap.josm.tools.GBC; 50 51 … … 196 197 return tr("JOSM custom settings files (*.xml)"); 197 198 } 198 199 }; 199 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(!saveFileFlag, !saveFileFlag, title, filter, JFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 200 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(!saveFileFlag, !saveFileFlag, title, filter, 201 JFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 200 202 if (fc != null) { 201 203 File[] sel = fc.isMultiSelectionEnabled() ? fc.getSelectedFiles() : (new File[]{fc.getSelectedFile()}); 202 204 if (sel.length==1 && !sel[0].getName().contains(".")) sel[0]=new File(sel[0].getAbsolutePath()+".xml"); -
src/org/openstreetmap/josm/gui/preferences/advanced/ExportProfileAction.java
19 19 import org.openstreetmap.josm.data.CustomConfigurator; 20 20 import org.openstreetmap.josm.data.Preferences; 21 21 import org.openstreetmap.josm.data.Preferences.Setting; 22 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 22 23 23 24 /** 24 25 * Action that exports some fragment of settings to custom configuration file … … 70 71 return tr("JOSM custom settings files (*.xml)"); 71 72 } 72 73 }; 73 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, "customsettings.lastDirectory");74 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 74 75 if (fc != null) { 75 76 File sel = fc.getSelectedFile(); 76 77 if (!sel.getName().endsWith(".xml")) sel=new File(sel.getAbsolutePath()+".xml"); -
src/org/openstreetmap/josm/gui/widgets/AbstractFileChooser.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.widgets; 3 4 import java.awt.Component; 5 import java.awt.HeadlessException; 6 import java.io.File; 7 import java.util.Locale; 8 9 import javax.swing.filechooser.FileFilter; 10 11 /** 12 * Abstract class to allow different file chooser implementations. 13 */ 14 public abstract class AbstractFileChooser { 15 16 /** The locale for both implementations */ 17 protected static Locale locale; 18 19 /** 20 * Sets the default locale for all implementations. 21 * @param l locale 22 */ 23 public static void setDefaultLocale(Locale l) { 24 locale = l; 25 } 26 27 /** 28 * Adds a filter to the list of user choosable file filters. 29 * For information on setting the file selection mode, see 30 * {@link #setFileSelectionMode setFileSelectionMode}. 31 * 32 * @param filter the <code>FileFilter</code> to add to the choosable file 33 * filter list 34 * 35 * @see #getChoosableFileFilters 36 * @see #setFileSelectionMode 37 */ 38 public abstract void addChoosableFileFilter(FileFilter filter); 39 40 /** 41 * Gets the list of user choosable file filters. 42 * 43 * @return a <code>FileFilter</code> array containing all the choosable 44 * file filters 45 * 46 * @see #addChoosableFileFilter 47 */ 48 public abstract FileFilter[] getChoosableFileFilters(); 49 50 /** 51 * Returns the current directory. 52 * 53 * @return the current directory 54 * @see #setCurrentDirectory 55 */ 56 public abstract File getCurrentDirectory(); 57 58 /** 59 * Returns the currently selected file filter. 60 * 61 * @return the current file filter 62 * @see #setFileFilter 63 * @see #addChoosableFileFilter 64 */ 65 public abstract FileFilter getFileFilter(); 66 67 /** 68 * Returns the selected file. This can be set either by the 69 * programmer via <code>setSelectedFile</code> or by a user action, such as 70 * either typing the filename into the UI or selecting the 71 * file from a list in the UI. 72 * 73 * @see #setSelectedFile 74 * @return the selected file 75 */ 76 public abstract File getSelectedFile(); 77 78 /** 79 * Returns a list of selected files if the file chooser is 80 * set to allow multiple selection. 81 * @return a list of selected files if the file chooser is 82 * set to allow multiple selection, or an empty array otherwise. 83 */ 84 public abstract File[] getSelectedFiles(); 85 86 /** 87 * Returns true if multiple files can be selected. 88 * @return true if multiple files can be selected 89 * @see #setMultiSelectionEnabled 90 */ 91 public abstract boolean isMultiSelectionEnabled(); 92 93 /** 94 * Determines whether the <code>AcceptAll FileFilter</code> is used 95 * as an available choice in the choosable filter list. 96 * If false, the <code>AcceptAll</code> file filter is removed from 97 * the list of available file filters. 98 * If true, the <code>AcceptAll</code> file filter will become the 99 * the actively used file filter. 100 * @param b whether the <code>AcceptAll FileFilter</code> is used 101 * as an available choice in the choosable filter list 102 * 103 * @see #setFileFilter 104 */ 105 public abstract void setAcceptAllFileFilterUsed(boolean b); 106 107 /** 108 * Sets the current directory. Passing in <code>null</code> sets the 109 * file chooser to point to the user's default directory. 110 * This default depends on the operating system. It is 111 * typically the "My Documents" folder on Windows, and the user's 112 * home directory on Unix. 113 * 114 * If the file passed in as <code>currentDirectory</code> is not a 115 * directory, the parent of the file will be used as the currentDirectory. 116 * If the parent is not traversable, then it will walk up the parent tree 117 * until it finds a traversable directory, or hits the root of the 118 * file system. 119 * 120 * @param dir the current directory to point to 121 * @see #getCurrentDirectory 122 */ 123 public abstract void setCurrentDirectory(File dir); 124 125 /** 126 * Sets the string that goes in the <code>JFileChooser</code> window's 127 * title bar. 128 * 129 * @param title the new <code>String</code> for the title bar 130 */ 131 public abstract void setDialogTitle(String title); 132 133 /** 134 * Sets the current file filter. The file filter is used by the 135 * file chooser to filter out files from the user's view. 136 * 137 * @param filter the new current file filter to use 138 * @see #getFileFilter 139 */ 140 public abstract void setFileFilter(final FileFilter filter); 141 142 /** 143 * Sets the <code>JFileChooser</code> to allow the user to just 144 * select files, just select 145 * directories, or select both files and directories. The default is 146 * <code>JFilesChooser.FILES_ONLY</code>. 147 * 148 * @param selectionMode the type of files to be displayed: 149 * <ul> 150 * <li>JFileChooser.FILES_ONLY 151 * <li>JFileChooser.DIRECTORIES_ONLY 152 * <li>JFileChooser.FILES_AND_DIRECTORIES 153 * </ul> 154 * 155 * @exception IllegalArgumentException if <code>mode</code> is an 156 * illegal file selection mode 157 */ 158 public abstract void setFileSelectionMode(int selectionMode); 159 160 /** 161 * Sets the file chooser to allow multiple file selections. 162 * 163 * @param multiple true if multiple files may be selected 164 * @beaninfo 165 * bound: true 166 * description: Sets multiple file selection mode. 167 * 168 * @see #isMultiSelectionEnabled 169 */ 170 public abstract void setMultiSelectionEnabled(boolean multiple); 171 172 /** 173 * Sets the selected file. If the file's parent directory is 174 * not the current directory, changes the current directory 175 * to be the file's parent directory. 176 * 177 * @see #getSelectedFile 178 * 179 * @param file the selected file 180 */ 181 public abstract void setSelectedFile(File file); 182 183 /** 184 * Pops up an "Open File" file chooser dialog. Note that the 185 * text that appears in the approve button is determined by 186 * the L&F. 187 * 188 * @param parent the parent component of the dialog, 189 * can be <code>null</code>; 190 * see <code>showDialog</code> for details 191 * @return the return state of the file chooser on popdown: 192 * <ul> 193 * <li>JFileChooser.CANCEL_OPTION 194 * <li>JFileChooser.APPROVE_OPTION 195 * <li>JFileChooser.ERROR_OPTION if an error occurs or the 196 * dialog is dismissed 197 * </ul> 198 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 199 * returns true. 200 * @see java.awt.GraphicsEnvironment#isHeadless 201 */ 202 public abstract int showOpenDialog(Component parent); 203 204 /** 205 * Pops up a "Save File" file chooser dialog. Note that the 206 * text that appears in the approve button is determined by 207 * the L&F. 208 * 209 * @param parent the parent component of the dialog, 210 * can be <code>null</code>; 211 * see <code>showDialog</code> for details 212 * @return the return state of the file chooser on popdown: 213 * <ul> 214 * <li>JFileChooser.CANCEL_OPTION 215 * <li>JFileChooser.APPROVE_OPTION 216 * <li>JFileChooser.ERROR_OPTION if an error occurs or the 217 * dialog is dismissed 218 * </ul> 219 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 220 * returns true. 221 * @see java.awt.GraphicsEnvironment#isHeadless 222 */ 223 public abstract int showSaveDialog(Component parent); 224 } -
src/org/openstreetmap/josm/gui/widgets/FileChooserManager.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.widgets; 3 4 import java.awt.Component; 5 import java.io.File; 6 import java.util.Collection; 7 import java.util.Collections; 8 9 import javax.swing.JFileChooser; 10 import javax.swing.filechooser.FileFilter; 11 12 import org.openstreetmap.josm.Main; 13 import org.openstreetmap.josm.actions.DiskAccessAction; 14 import org.openstreetmap.josm.actions.ExtensionFileFilter; 15 import org.openstreetmap.josm.actions.SaveActionBase; 16 17 /** 18 * A chained utility class used to create and open {@link AbstractFileChooser} dialogs.<br> 19 * Use only this class if you need to control specifically your AbstractFileChooser dialog.<br> 20 * <p> 21 * A simpler usage is to call the {@link DiskAccessAction#createAndOpenFileChooser} methods. 22 * 23 * @since 5438 24 */ 25 public class FileChooserManager { 26 private final boolean open; 27 private final String lastDirProperty; 28 private final String curDir; 29 30 private AbstractFileChooser fc; 31 32 /** 33 * Creates a new {@code FileChooserManager}. 34 * @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created. 35 * @see #createFileChooser 36 */ 37 public FileChooserManager(boolean open) { 38 this(open, null); 39 } 40 41 /** 42 * Creates a new {@code FileChooserManager}. 43 * @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created. 44 * @param lastDirProperty The name of the property used to get the last directory. This directory is used to initialize the AbstractFileChooser. 45 * Then, if the user effectively chooses a file or a directory, this property will be updated to the directory path. 46 * @see #createFileChooser 47 */ 48 public FileChooserManager(boolean open, String lastDirProperty) { 49 this(open, lastDirProperty, null); 50 } 51 52 /** 53 * Creates a new {@code FileChooserManager}. 54 * @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created. 55 * @param lastDirProperty The name of the property used to get the last directory. This directory is used to initialize the AbstractFileChooser. 56 * Then, if the user effectively chooses a file or a directory, this property will be updated to the directory path. 57 * @param defaultDir The default directory used to initialize the AbstractFileChooser if the {@code lastDirProperty} property value is missing. 58 * @see #createFileChooser 59 */ 60 public FileChooserManager(boolean open, String lastDirProperty, String defaultDir) { 61 this.open = open; 62 this.lastDirProperty = lastDirProperty == null || lastDirProperty.isEmpty() ? "lastDirectory" : lastDirProperty; 63 this.curDir = Main.pref.get(this.lastDirProperty).isEmpty() ? 64 (defaultDir == null || defaultDir.isEmpty() ? "." : defaultDir) 65 : Main.pref.get(this.lastDirProperty); 66 } 67 68 /** 69 * Replies the {@code AbstractFileChooser} that has been previously created. 70 * @return The {@code AbstractFileChooser} that has been previously created, or {@code null} if it has not been created yet. 71 * @see #createFileChooser 72 */ 73 public final AbstractFileChooser getFileChooser() { 74 return fc; 75 } 76 77 /** 78 * Replies the initial directory used to construct the {@code AbstractFileChooser}. 79 * @return The initial directory used to construct the {@code AbstractFileChooser}. 80 */ 81 public final String getInitialDirectory() { 82 return curDir; 83 } 84 85 /** 86 * Creates a new {@link AbstractFileChooser} with default settings. All files will be accepted. 87 * @return this 88 */ 89 public final FileChooserManager createFileChooser() { 90 return doCreateFileChooser(false, null, null, null, null, JFileChooser.FILES_ONLY, false); 91 } 92 93 /** 94 * Creates a new {@link AbstractFileChooser} with given settings for a single {@code FileFilter}. 95 * 96 * @param multiple If true, makes the dialog allow multiple file selections 97 * @param title The string that goes in the dialog window's title bar 98 * @param filter The only file filter that will be proposed by the dialog 99 * @param selectionMode The selection mode that allows the user to:<br><ul> 100 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 101 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 102 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul> 103 * @return this 104 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 105 */ 106 public final FileChooserManager createFileChooser(boolean multiple, String title, FileFilter filter, int selectionMode) { 107 doCreateFileChooser(multiple, title, Collections.singleton(filter), filter, null, selectionMode, false); 108 getFileChooser().setAcceptAllFileFilterUsed(false); 109 return this; 110 } 111 112 /** 113 * Creates a new {@link AbstractFileChooser} with given settings for a collection of {@code FileFilter}s. 114 * 115 * @param multiple If true, makes the dialog allow multiple file selections 116 * @param title The string that goes in the dialog window's title bar 117 * @param filters The file filters that will be proposed by the dialog 118 * @param defaultFilter The file filter that will be selected by default 119 * @param selectionMode The selection mode that allows the user to:<br><ul> 120 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 121 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 122 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul> 123 * @return this 124 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, Collection, FileFilter, int, String) 125 */ 126 public final FileChooserManager createFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode) { 127 return doCreateFileChooser(multiple, title, filters, defaultFilter, null, selectionMode, false); 128 } 129 130 /** 131 * Creates a new {@link AbstractFileChooser} with given settings for a file extension. 132 * 133 * @param multiple If true, makes the dialog allow multiple file selections 134 * @param title The string that goes in the dialog window's title bar 135 * @param extension The file extension that will be selected as the default file filter 136 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 137 * If false, only the file filters that include {@code extension} will be proposed 138 * @param selectionMode The selection mode that allows the user to:<br><ul> 139 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 140 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 141 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul> 142 * @return this 143 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 144 */ 145 public final FileChooserManager createFileChooser(boolean multiple, String title, String extension, boolean allTypes, int selectionMode) { 146 return doCreateFileChooser(multiple, title, null, null, extension, selectionMode, allTypes); 147 } 148 149 private final FileChooserManager doCreateFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, String extension, int selectionMode, boolean allTypes) { 150 File file = new File(curDir); 151 if (Main.pref.getBoolean("use.native.file.dialog", true)) { 152 fc = new NativeFileChooser(file); 153 } else { 154 fc = new SwingFileChooser(file); 155 } 156 157 if (title != null) { 158 fc.setDialogTitle(title); 159 } 160 161 fc.setFileSelectionMode(selectionMode); 162 fc.setMultiSelectionEnabled(multiple); 163 fc.setAcceptAllFileFilterUsed(false); 164 165 if (filters != null) { 166 for (FileFilter filter : filters) { 167 fc.addChoosableFileFilter(filter); 168 } 169 if (defaultFilter != null) { 170 fc.setFileFilter(defaultFilter); 171 } 172 } else if (open) { 173 ExtensionFileFilter.applyChoosableImportFileFilters(fc, extension, allTypes); 174 } else { 175 ExtensionFileFilter.applyChoosableExportFileFilters(fc, extension, allTypes); 176 } 177 return this; 178 } 179 180 /** 181 * Opens the {@code AbstractFileChooser} that has been created. Nothing happens if it has not been created yet. 182 * @return the {@code AbstractFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog. 183 */ 184 public final AbstractFileChooser openFileChooser() { 185 return openFileChooser(null); 186 } 187 188 /** 189 * Opens the {@code AbstractFileChooser} that has been created and waits for the user to choose a file/directory, or cancel the dialog.<br> 190 * Nothing happens if the dialog has not been created yet.<br> 191 * When the user choses a file or directory, the {@code lastDirProperty} is updated to the chosen directory path. 192 * 193 * @param parent The Component used as the parent of the AbstractFileChooser. If null, uses {@code Main.parent}. 194 * @return the {@code AbstractFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog. 195 */ 196 public AbstractFileChooser openFileChooser(Component parent) { 197 if (fc != null) { 198 if (parent == null) { 199 parent = Main.parent; 200 } 201 202 int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent); 203 if (answer != JFileChooser.APPROVE_OPTION) { 204 return null; 205 } 206 207 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 208 Main.pref.put(lastDirProperty, fc.getCurrentDirectory().getAbsolutePath()); 209 } 210 211 if (!open) { 212 File file = fc.getSelectedFile(); 213 if (!SaveActionBase.confirmOverwrite(file)) { 214 return null; 215 } 216 } 217 } 218 return fc; 219 } 220 } -
src/org/openstreetmap/josm/gui/widgets/JFileChooserManager.java
1 // License: GPL. For details, see LICENSE file.2 package org.openstreetmap.josm.gui.widgets;3 4 import java.awt.Component;5 import java.io.File;6 import java.util.Collection;7 import java.util.Collections;8 9 import javax.swing.JFileChooser;10 import javax.swing.filechooser.FileFilter;11 12 import org.openstreetmap.josm.Main;13 import org.openstreetmap.josm.actions.DiskAccessAction;14 import org.openstreetmap.josm.actions.ExtensionFileFilter;15 import org.openstreetmap.josm.actions.SaveActionBase;16 17 /**18 * A chained utility class used to create and open {@link JFileChooser} dialogs.<br>19 * Use only this class if you need to control specifically your JFileChooser dialog.<br>20 * <p>21 * A simpler usage is to call the {@link DiskAccessAction#createAndOpenFileChooser} methods.22 *23 * @since 543824 */25 public class JFileChooserManager {26 private final boolean open;27 private final String lastDirProperty;28 private final String curDir;29 30 private JFileChooser fc;31 32 /**33 * Creates a new {@code JFileChooserManager}.34 * @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.35 * @see #createFileChooser36 */37 public JFileChooserManager(boolean open) {38 this(open, null);39 }40 41 /**42 * Creates a new {@code JFileChooserManager}.43 * @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.44 * @param lastDirProperty The name of the property used to get the last directory. This directory is used to initialize the JFileChooser.45 * Then, if the user effectively chooses a file or a directory, this property will be updated to the directory path.46 * @see #createFileChooser47 */48 public JFileChooserManager(boolean open, String lastDirProperty) {49 this(open, lastDirProperty, null);50 }51 52 /**53 * Creates a new {@code JFileChooserManager}.54 * @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.55 * @param lastDirProperty The name of the property used to get the last directory. This directory is used to initialize the JFileChooser.56 * Then, if the user effectively chooses a file or a directory, this property will be updated to the directory path.57 * @param defaultDir The default directory used to initialize the JFileChooser if the {@code lastDirProperty} property value is missing.58 * @see #createFileChooser59 */60 public JFileChooserManager(boolean open, String lastDirProperty, String defaultDir) {61 this.open = open;62 this.lastDirProperty = lastDirProperty == null || lastDirProperty.isEmpty() ? "lastDirectory" : lastDirProperty;63 this.curDir = Main.pref.get(this.lastDirProperty).isEmpty() ?64 (defaultDir == null || defaultDir.isEmpty() ? "." : defaultDir)65 : Main.pref.get(this.lastDirProperty);66 }67 68 /**69 * Replies the {@code JFileChooser} that has been previously created.70 * @return The {@code JFileChooser} that has been previously created, or {@code null} if it has not been created yet.71 * @see #createFileChooser72 */73 public final JFileChooser getFileChooser() {74 return fc;75 }76 77 /**78 * Replies the initial directory used to construct the {@code JFileChooser}.79 * @return The initial directory used to construct the {@code JFileChooser}.80 */81 public final String getInitialDirectory() {82 return curDir;83 }84 85 /**86 * Creates a new {@link JFileChooser} with default settings. All files will be accepted.87 * @return this88 */89 public final JFileChooserManager createFileChooser() {90 return doCreateFileChooser(false, null, null, null, null, JFileChooser.FILES_ONLY, false);91 }92 93 /**94 * Creates a new {@link JFileChooser} with given settings for a single {@code FileFilter}.95 *96 * @param multiple If true, makes the dialog allow multiple file selections97 * @param title The string that goes in the dialog window's title bar98 * @param filter The only file filter that will be proposed by the dialog99 * @param selectionMode The selection mode that allows the user to:<br><ul>100 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>101 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>102 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>103 * @return this104 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String)105 */106 public final JFileChooserManager createFileChooser(boolean multiple, String title, FileFilter filter, int selectionMode) {107 doCreateFileChooser(multiple, title, Collections.singleton(filter), filter, null, selectionMode, false);108 getFileChooser().setAcceptAllFileFilterUsed(false);109 return this;110 }111 112 /**113 * Creates a new {@link JFileChooser} with given settings for a collection of {@code FileFilter}s.114 *115 * @param multiple If true, makes the dialog allow multiple file selections116 * @param title The string that goes in the dialog window's title bar117 * @param filters The file filters that will be proposed by the dialog118 * @param defaultFilter The file filter that will be selected by default119 * @param selectionMode The selection mode that allows the user to:<br><ul>120 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>121 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>122 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>123 * @return this124 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, Collection, FileFilter, int, String)125 */126 public final JFileChooserManager createFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode) {127 return doCreateFileChooser(multiple, title, filters, defaultFilter, null, selectionMode, false);128 }129 130 /**131 * Creates a new {@link JFileChooser} with given settings for a file extension.132 *133 * @param multiple If true, makes the dialog allow multiple file selections134 * @param title The string that goes in the dialog window's title bar135 * @param extension The file extension that will be selected as the default file filter136 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.137 * If false, only the file filters that include {@code extension} will be proposed138 * @param selectionMode The selection mode that allows the user to:<br><ul>139 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>140 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>141 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>142 * @return this143 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String)144 */145 public final JFileChooserManager createFileChooser(boolean multiple, String title, String extension, boolean allTypes, int selectionMode) {146 return doCreateFileChooser(multiple, title, null, null, extension, selectionMode, allTypes);147 }148 149 private final JFileChooserManager doCreateFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, String extension, int selectionMode, boolean allTypes) {150 fc = new JFileChooser(new File(curDir));151 if (title != null) {152 fc.setDialogTitle(title);153 }154 155 fc.setFileSelectionMode(selectionMode);156 fc.setMultiSelectionEnabled(multiple);157 fc.setAcceptAllFileFilterUsed(false);158 159 if (filters != null) {160 for (FileFilter filter : filters) {161 fc.addChoosableFileFilter(filter);162 }163 if (defaultFilter != null) {164 fc.setFileFilter(defaultFilter);165 }166 } else if (open) {167 ExtensionFileFilter.applyChoosableImportFileFilters(fc, extension, allTypes);168 } else {169 ExtensionFileFilter.applyChoosableExportFileFilters(fc, extension, allTypes);170 }171 return this;172 }173 174 /**175 * Opens the {@code JFileChooser} that has been created. Nothing happens if it has not been created yet.176 * @return the {@code JFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog.177 */178 public final JFileChooser openFileChooser() {179 return openFileChooser(null);180 }181 182 /**183 * Opens the {@code JFileChooser} that has been created and waits for the user to choose a file/directory, or cancel the dialog.<br>184 * Nothing happens if the dialog has not been created yet.<br>185 * When the user choses a file or directory, the {@code lastDirProperty} is updated to the chosen directory path.186 *187 * @param parent The Component used as the parent of the JFileChooser. If null, uses {@code Main.parent}.188 * @return the {@code JFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog.189 */190 public JFileChooser openFileChooser(Component parent) {191 if (fc != null) {192 if (parent == null) {193 parent = Main.parent;194 }195 196 int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent);197 if (answer != JFileChooser.APPROVE_OPTION) {198 return null;199 }200 201 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {202 Main.pref.put(lastDirProperty, fc.getCurrentDirectory().getAbsolutePath());203 }204 205 if (!open) {206 File file = fc.getSelectedFile();207 if (!SaveActionBase.confirmOverwrite(file)) {208 return null;209 }210 }211 }212 return fc;213 }214 } -
src/org/openstreetmap/josm/gui/widgets/NativeFileChooser.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.widgets; 3 4 import java.awt.Component; 5 import java.awt.FileDialog; 6 import java.awt.Frame; 7 import java.io.File; 8 import java.io.FilenameFilter; 9 10 import javax.swing.JFileChooser; 11 import javax.swing.filechooser.FileFilter; 12 13 import org.openstreetmap.josm.Main; 14 15 /** 16 * File chooser based on the AWT's {@link FileDialog} implementation, 17 * which looks like more a native file chooser than the Swing implementation. 18 */ 19 public class NativeFileChooser extends AbstractFileChooser { 20 21 /** The instance of the fileDialog */ 22 private final FileDialog fileDialog; 23 private FileFilter fileFilter; 24 25 /** 26 * Constructs a new {@code NativeFileChooser}. 27 * @param file the current file/directory to point to 28 */ 29 public NativeFileChooser(File file) { 30 fileDialog = new FileDialog((Frame) Main.parent); 31 if (file != null) { 32 fileDialog.setDirectory(file.getAbsolutePath()); 33 fileDialog.setFile(file.toString()); 34 } 35 } 36 37 @Override 38 public void addChoosableFileFilter(FileFilter filter) { 39 // TODO is this ok ? 40 setFileFilter(filter); 41 } 42 43 @Override 44 public FileFilter[] getChoosableFileFilters() { 45 // TODO is this ok ? 46 return new FileFilter[]{fileFilter}; 47 } 48 49 @Override 50 public File getCurrentDirectory() { 51 return new File(fileDialog.getDirectory()); 52 } 53 54 @Override 55 public FileFilter getFileFilter() { 56 return fileFilter; 57 } 58 59 @Override 60 public File getSelectedFile() { 61 return new File(fileDialog.getDirectory() + fileDialog.getFile()); 62 } 63 64 @Override 65 public File[] getSelectedFiles() { 66 return fileDialog.getFiles(); 67 } 68 69 @Override 70 public boolean isMultiSelectionEnabled() { 71 return fileDialog.isMultipleMode(); 72 } 73 74 @Override 75 public void setAcceptAllFileFilterUsed(boolean b) { 76 // TODO Auto-generated method stub 77 } 78 79 @Override 80 public void setCurrentDirectory(File f) { 81 fileDialog.setDirectory(f.toString()); 82 } 83 84 @Override 85 public void setDialogTitle(String title) { 86 fileDialog.setTitle(title); 87 } 88 89 @Override 90 public void setFileFilter(final FileFilter cff) { 91 FilenameFilter filter = new FilenameFilter() { 92 public boolean accept(File Directory, String fileName) { 93 return cff.accept(new File(Directory.getAbsolutePath() + fileName)); 94 } 95 }; 96 fileDialog.setFilenameFilter(filter); 97 fileFilter = cff; 98 } 99 100 @Override 101 public void setFileSelectionMode(int selectionMode) { 102 // TODO Auto-generated method stub 103 } 104 105 @Override 106 public void setMultiSelectionEnabled(boolean multiple) { 107 fileDialog.setMultipleMode(multiple); 108 } 109 110 @Override 111 public void setSelectedFile(File file) { 112 fileDialog.setDirectory(file.getAbsolutePath()); 113 fileDialog.setFile(file.getName()); 114 } 115 116 @Override 117 public int showOpenDialog(Component parent) { 118 fileDialog.setLocale(locale); 119 fileDialog.setMode(FileDialog.LOAD); 120 fileDialog.setVisible(true); 121 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION; 122 } 123 124 @Override 125 public int showSaveDialog(Component parent) { 126 fileDialog.setLocale(locale); 127 fileDialog.setMode(FileDialog.SAVE); 128 fileDialog.setVisible(true); 129 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION; 130 } 131 } -
src/org/openstreetmap/josm/gui/widgets/SwingFileChooser.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.widgets; 3 4 import java.awt.Component; 5 import java.io.File; 6 7 import javax.swing.JFileChooser; 8 import javax.swing.filechooser.FileFilter; 9 10 /** 11 * File chooser based on the Swing's {@link JFileChooser} implementation. 12 */ 13 public class SwingFileChooser extends AbstractFileChooser { 14 15 /** The JFileChooser which we use for this. */ 16 private final JFileChooser jFileChooser; 17 18 /** 19 * Constructs a new {@code SwingFileChooser}. 20 * @param file the current file/directory to point to 21 */ 22 public SwingFileChooser(File file) { 23 jFileChooser = new JFileChooser(file); 24 } 25 26 @Override 27 public void addChoosableFileFilter(FileFilter filter) { 28 jFileChooser.addChoosableFileFilter(filter); 29 } 30 31 @Override 32 public FileFilter[] getChoosableFileFilters() { 33 return jFileChooser.getChoosableFileFilters(); 34 } 35 36 @Override 37 public File getCurrentDirectory() { 38 return jFileChooser.getCurrentDirectory(); 39 } 40 41 @Override 42 public FileFilter getFileFilter() { 43 return jFileChooser.getFileFilter(); 44 } 45 46 @Override 47 public File getSelectedFile() { 48 return jFileChooser.getSelectedFile(); 49 } 50 51 @Override 52 public File[] getSelectedFiles() { 53 return jFileChooser.getSelectedFiles(); 54 } 55 56 @Override 57 public boolean isMultiSelectionEnabled() { 58 return jFileChooser.isMultiSelectionEnabled(); 59 } 60 61 @Override 62 public void setAcceptAllFileFilterUsed(boolean b) { 63 jFileChooser.setAcceptAllFileFilterUsed(b); 64 } 65 66 @Override 67 public void setCurrentDirectory(File f) { 68 jFileChooser.setCurrentDirectory(f); 69 } 70 71 @Override 72 public void setDialogTitle(String title) { 73 jFileChooser.setDialogTitle(title); 74 } 75 76 @Override 77 public void setFileFilter(FileFilter cff) { 78 jFileChooser.setFileFilter(cff); 79 } 80 81 @Override 82 public void setFileSelectionMode(int selectionMode) { 83 jFileChooser.setFileSelectionMode(selectionMode); 84 } 85 86 @Override 87 public void setMultiSelectionEnabled(boolean multiple) { 88 jFileChooser.setMultiSelectionEnabled(multiple); 89 } 90 91 @Override 92 public void setSelectedFile(File file) { 93 jFileChooser.setSelectedFile(file); 94 } 95 96 @Override 97 public int showOpenDialog(Component parent) { 98 jFileChooser.setLocale(locale); 99 return jFileChooser.showOpenDialog(parent); 100 } 101 102 @Override 103 public int showSaveDialog(Component parent) { 104 jFileChooser.setLocale(locale); 105 return jFileChooser.showSaveDialog(parent); 106 } 107 } -
src/org/openstreetmap/josm/tools/I18n.java
25 25 26 26 import org.openstreetmap.gui.jmapviewer.FeatureAdapter.TranslationAdapter; 27 27 import org.openstreetmap.josm.Main; 28 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 28 29 29 30 /** 30 31 * Internationalisation support. … … 640 641 public static void translateJavaInternalMessages() { 641 642 Locale l = Locale.getDefault(); 642 643 644 AbstractFileChooser.setDefaultLocale(l); 643 645 JFileChooser.setDefaultLocale(l); 644 646 JColorChooser.setDefaultLocale(l); 645 647 for (String key : javaInternalMessageKeys) {
