Ticket #10024: NativeFileDialog.patch
File NativeFileDialog.patch, 77.6 KB (added by , 11 years ago) |
---|
-
src/org/openstreetmap/josm/actions/DiskAccessAction.java
3 3 4 4 import java.util.Collection; 5 5 6 import javax.swing.JFileChooser;7 6 import javax.swing.filechooser.FileFilter; 8 7 9 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 8 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 9 import org.openstreetmap.josm.gui.widgets.GenericFileChooserManager; 10 10 import org.openstreetmap.josm.tools.Shortcut; 11 11 12 12 /** … … 45 45 } 46 46 47 47 /** 48 * Creates a new {@link JFileChooser} and makes it visible.48 * Creates a new {@link GenericFileChooser} and makes it visible. 49 49 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 50 50 * @param multiple If true, makes the dialog allow multiple file selections 51 51 * @param title The string that goes in the dialog window's title bar 52 * @return The {@code JFileChooser}.52 * @return The {@code GenericFileChooser}. 53 53 * @since 1646 54 54 */ 55 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {55 public static GenericFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) { 56 56 return createAndOpenFileChooser(open, multiple, title, null); 57 57 } 58 58 59 59 /** 60 * Creates a new {@link JFileChooser} and makes it visible.60 * Creates a new {@link GenericFileChooser} and makes it visible. 61 61 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 62 62 * @param multiple If true, makes the dialog allow multiple file selections 63 63 * @param title The string that goes in the dialog window's title bar 64 64 * @param extension The file extension that will be selected as the default file filter 65 * @return The {@code JFileChooser}.65 * @return The {@code GenericFileChooser}. 66 66 * @since 2020 67 67 */ 68 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) {69 return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null);68 public static GenericFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) { 69 return createAndOpenFileChooser(open, multiple, title, extension, GenericFileChooser.FILES_ONLY, true, null); 70 70 } 71 71 72 72 /** 73 * Creates a new {@link JFileChooser} and makes it visible.73 * Creates a new {@link GenericFileChooser} and makes it visible. 74 74 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 75 75 * @param multiple If true, makes the dialog allow multiple file selections 76 76 * @param title The string that goes in the dialog window's title bar 77 77 * @param extension The file extension that will be selected as the default file filter 78 78 * @param selectionMode The selection mode that allows the user to:<br><ul> 79 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>80 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>81 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>79 * <li>just select files ({@code GenericFileChooser.FILES_ONLY})</li> 80 * <li>just select directories ({@code GenericFileChooser.DIRECTORIES_ONLY})</li> 81 * <li>select both files and directories ({@code GenericFileChooser.FILES_AND_DIRECTORIES})</li></ul> 82 82 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 83 83 * 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.84 * @param lastDirProperty The name of the property used to setup the GenericFileChooser initial directory. 85 85 * 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}.86 * @return The {@code GenericFileChooser}. 87 87 * @since 5438 88 88 */ 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();89 public static GenericFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension, int selectionMode, boolean allTypes, String lastDirProperty) { 90 return new GenericFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, extension, allTypes, selectionMode).openFileChooser(); 91 91 } 92 92 93 93 /** 94 * Creates a new {@link JFileChooser} for a single {@link FileFilter} and makes it visible.94 * Creates a new {@link GenericFileChooser} for a single {@link FileFilter} and makes it visible. 95 95 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 96 96 * @param multiple If true, makes the dialog allow multiple file selections 97 97 * @param title The string that goes in the dialog window's title bar 98 98 * @param filter The only file filter that will be proposed by the dialog 99 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 * @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}.100 * <li>just select files ({@code GenericFileChooser.FILES_ONLY})</li> 101 * <li>just select directories ({@code GenericFileChooser.DIRECTORIES_ONLY})</li> 102 * <li>select both files and directories ({@code GenericFileChooser.FILES_AND_DIRECTORIES})</li></ul> 103 * @param lastDirProperty The name of the property used to setup the GenericFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the user 104 * @return The {@code GenericFileChooser}. 105 105 * @since 5438 106 106 */ 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();107 public static GenericFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, FileFilter filter, int selectionMode, String lastDirProperty) { 108 return new GenericFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filter, selectionMode).openFileChooser(); 109 109 } 110 110 111 111 /** 112 * Creates a new {@link JFileChooser} for several {@link FileFilter}s and makes it visible.112 * Creates a new {@link GenericFileChooser} for several {@link FileFilter}s and makes it visible. 113 113 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 114 114 * @param multiple If true, makes the dialog allow multiple file selections 115 115 * @param title The string that goes in the dialog window's title bar 116 116 * @param filters The file filters that will be proposed by the dialog 117 117 * @param defaultFilter The file filter that will be selected by default 118 118 * @param selectionMode The selection mode that allows the user to:<br><ul> 119 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>120 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>121 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>122 * @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 user123 * @return The {@code JFileChooser}.119 * <li>just select files ({@code GenericFileChooser.FILES_ONLY})</li> 120 * <li>just select directories ({@code GenericFileChooser.DIRECTORIES_ONLY})</li> 121 * <li>select both files and directories ({@code GenericFileChooser.FILES_AND_DIRECTORIES})</li></ul> 122 * @param lastDirProperty The name of the property used to setup the GenericFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the user 123 * @return The {@code GenericFileChooser}. 124 124 * @since 5438 125 125 */ 126 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title,126 public static GenericFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, 127 127 Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode, String lastDirProperty) { 128 return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filters, defaultFilter, selectionMode).openFileChooser();128 return new GenericFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filters, defaultFilter, selectionMode).openFileChooser(); 129 129 } 130 130 } -
src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
8 8 import java.util.LinkedList; 9 9 import java.util.List; 10 10 11 import javax.swing.JFileChooser;12 11 import javax.swing.filechooser.FileFilter; 13 12 14 13 import org.openstreetmap.josm.Main; 15 14 import org.openstreetmap.josm.gui.MapView; 15 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 16 16 import org.openstreetmap.josm.io.AllFormatsImporter; 17 17 import org.openstreetmap.josm.io.FileExporter; 18 18 import org.openstreetmap.josm.io.FileImporter; … … 198 198 } 199 199 200 200 /** 201 * Applies the choosable {@link FileFilter} to a {@link JFileChooser} before using the201 * Applies the choosable {@link FileFilter} to a {@link GenericFileChooser} before using the 202 202 * file chooser for selecting a file for reading. 203 203 * 204 204 * @param fileChooser the file chooser … … 207 207 * If false, only the file filters that include {@code extension} will be proposed 208 208 * @since 5438 209 209 */ 210 public static void applyChoosableImportFileFilters( JFileChooser fileChooser, String extension, boolean allTypes) {210 public static void applyChoosableImportFileFilters(GenericFileChooser fileChooser, String extension, boolean allTypes) { 211 211 for (ExtensionFileFilter filter: getImportExtensionFileFilters()) { 212 212 if (allTypes || filter.acceptName("file."+extension)) { 213 213 fileChooser.addChoosableFileFilter(filter); … … 217 217 } 218 218 219 219 /** 220 * Applies the choosable {@link FileFilter} to a {@link JFileChooser} before using the220 * Applies the choosable {@link FileFilter} to a {@link GenericFileChooser} before using the 221 221 * file chooser for selecting a file for writing. 222 222 * 223 223 * @param fileChooser the file chooser … … 226 226 * If false, only the file filters that include {@code extension} will be proposed 227 227 * @since 5438 228 228 */ 229 public static void applyChoosableExportFileFilters( JFileChooser fileChooser, String extension, boolean allTypes) {229 public static void applyChoosableExportFileFilters(GenericFileChooser fileChooser, String extension, boolean allTypes) { 230 230 for (ExtensionFileFilter filter: getExportExtensionFileFilters()) { 231 231 if (allTypes || filter.acceptName("file."+extension)) { 232 232 fileChooser.addChoosableFileFilter(filter); -
src/org/openstreetmap/josm/actions/OpenFileAction.java
25 25 import java.util.regex.Matcher; 26 26 import java.util.regex.Pattern; 27 27 28 import javax.swing.JFileChooser;29 28 import javax.swing.JOptionPane; 30 29 import javax.swing.SwingUtilities; 31 30 import javax.swing.filechooser.FileFilter; … … 34 33 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 35 34 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 36 35 import org.openstreetmap.josm.gui.help.HelpUtil; 36 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 37 37 import org.openstreetmap.josm.io.AllFormatsImporter; 38 38 import org.openstreetmap.josm.io.FileImporter; 39 39 import org.openstreetmap.josm.io.OsmTransferException; … … 49 49 */ 50 50 public class OpenFileAction extends DiskAccessAction { 51 51 52 public static final String USE_NATIVE_FILE_DIALOG = "use.native.file.dialog"; 53 private static final boolean useNativeFileDialog = Main.pref.getBoolean(USE_NATIVE_FILE_DIALOG, false); 54 52 55 /** 53 56 * The {@link ExtensionFileFilter} matching .url files 54 57 */ … … 65 68 66 69 @Override 67 70 public void actionPerformed(ActionEvent e) { 68 JFileChooser fc = createAndOpenFileChooser(true, true, null); 71 72 GenericFileChooser fc = createAndOpenFileChooser(true, true, null); 69 73 if (fc == null) 70 74 return; 71 75 File[] files = fc.getSelectedFiles(); -
src/org/openstreetmap/josm/actions/SaveActionBase.java
10 10 import java.util.LinkedList; 11 11 import java.util.List; 12 12 13 import javax.swing.JFileChooser;14 13 import javax.swing.JOptionPane; 15 14 import javax.swing.filechooser.FileFilter; 16 15 … … 18 17 import org.openstreetmap.josm.gui.ExtendedDialog; 19 18 import org.openstreetmap.josm.gui.layer.Layer; 20 19 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 20 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 21 21 import org.openstreetmap.josm.io.FileExporter; 22 22 import org.openstreetmap.josm.tools.Shortcut; 23 23 … … 125 125 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 126 126 */ 127 127 public static File createAndOpenSaveFileChooser(String title, ExtensionFileFilter filter) { 128 JFileChooser fc = createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, null);128 GenericFileChooser fc = createAndOpenFileChooser(false, false, title, filter, GenericFileChooser.FILES_ONLY, null); 129 129 return checkFileAndConfirmOverWrite(fc, filter.getDefaultExtension()); 130 130 } 131 131 … … 139 139 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, String) 140 140 */ 141 141 public static File createAndOpenSaveFileChooser(String title, String extension) { 142 JFileChooser fc = createAndOpenFileChooser(false, false, title, extension);142 GenericFileChooser fc = createAndOpenFileChooser(false, false, title, extension); 143 143 return checkFileAndConfirmOverWrite(fc, extension); 144 144 } 145 145 146 private static File checkFileAndConfirmOverWrite( JFileChooser fc, String extension) {146 private static File checkFileAndConfirmOverWrite(GenericFileChooser fc, String extension) { 147 147 if (fc == null) return null; 148 148 File file = fc.getSelectedFile(); 149 149 -
src/org/openstreetmap/josm/actions/SessionLoadAction.java
13 13 import java.util.Arrays; 14 14 import java.util.List; 15 15 16 import javax.swing.JFileChooser;17 16 import javax.swing.JOptionPane; 18 17 import javax.swing.SwingUtilities; 19 18 … … 24 23 import org.openstreetmap.josm.gui.layer.Layer; 25 24 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 26 25 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 26 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 27 27 import org.openstreetmap.josm.io.IllegalDataException; 28 28 import org.openstreetmap.josm.io.session.SessionImporter; 29 29 import org.openstreetmap.josm.io.session.SessionReader; … … 46 46 47 47 @Override 48 48 public void actionPerformed(ActionEvent e) { 49 JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"),49 GenericFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"), 50 50 Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()), 51 SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory");51 SessionImporter.FILE_FILTER, GenericFileChooser.FILES_ONLY, "lastDirectory"); 52 52 if (fc == null) return; 53 53 File file = fc.getSelectedFile(); 54 54 boolean zip = file.getName().toLowerCase().endsWith(".joz"); … … 178 178 throw e; 179 179 } 180 180 } 181 181 182 182 private void handleException(String dialogTitle, Exception e) { 183 183 Main.error(e); 184 184 HelpAwareOptionPane.showMessageDialogInEDT( -
src/org/openstreetmap/josm/actions/SessionSaveAsAction.java
21 21 22 22 import javax.swing.BorderFactory; 23 23 import javax.swing.JCheckBox; 24 import javax.swing.JFileChooser;25 24 import javax.swing.JLabel; 26 25 import javax.swing.JOptionPane; 27 26 import javax.swing.JPanel; … … 35 34 import org.openstreetmap.josm.gui.ExtendedDialog; 36 35 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 37 36 import org.openstreetmap.josm.gui.layer.Layer; 37 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 38 38 import org.openstreetmap.josm.io.session.SessionLayerExporter; 39 39 import org.openstreetmap.josm.io.session.SessionWriter; 40 40 import org.openstreetmap.josm.tools.GBC; … … 81 81 FileFilter joz = new ExtensionFileFilter("joz", "joz", tr("Session file (archive) (*.joz)")); 82 82 FileFilter jos = new ExtensionFileFilter("jos", "jos", tr("Session file (*.jos)")); 83 83 84 JFileChooser fc;84 GenericFileChooser fc; 85 85 86 86 if (zipRequired) { 87 fc = createAndOpenFileChooser(false, false, tr("Save session"), joz, JFileChooser.FILES_ONLY, "lastDirectory");87 fc = createAndOpenFileChooser(false, false, tr("Save session"), joz, GenericFileChooser.FILES_ONLY, "lastDirectory"); 88 88 } else { 89 fc = createAndOpenFileChooser(false, false, tr("Save session"), Arrays.asList(new FileFilter[]{jos, joz}), jos, JFileChooser.FILES_ONLY, "lastDirectory");89 fc = createAndOpenFileChooser(false, false, tr("Save session"), Arrays.asList(new FileFilter[]{jos, joz}), jos, GenericFileChooser.FILES_ONLY, "lastDirectory"); 90 90 } 91 91 92 92 if (fc == null) … … 122 122 // TODO: resolve dependencies for layers excluded by the user 123 123 layersOut.add(layer); 124 124 } 125 125 126 126 int active = -1; 127 127 Layer activeLayer = Main.map.mapView.getActiveLayer(); 128 128 if (activeLayer != null) { -
src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java
33 33 import javax.swing.DefaultButtonModel; 34 34 import javax.swing.DefaultListSelectionModel; 35 35 import javax.swing.JCheckBox; 36 import javax.swing.JFileChooser;37 36 import javax.swing.JLabel; 38 37 import javax.swing.JPanel; 39 38 import javax.swing.JPopupMenu; … … 72 71 import org.openstreetmap.josm.gui.preferences.SourceEntry; 73 72 import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference; 74 73 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 74 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 75 import org.openstreetmap.josm.gui.widgets.GenericFileChooserManager; 75 76 import org.openstreetmap.josm.gui.widgets.HtmlPanel; 76 import org.openstreetmap.josm.gui.widgets.JFileChooserManager;77 77 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 78 78 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 79 79 import org.openstreetmap.josm.tools.GBC; … … 478 478 return; 479 479 final StyleSource s = model.getRow(sel); 480 480 481 JFileChooserManager fcm = new JFileChooserManager(false, "mappaint.clone-style.lastDirectory", System.getProperty("user.home"));481 GenericFileChooserManager fcm = new GenericFileChooserManager(false, "mappaint.clone-style.lastDirectory", System.getProperty("user.home")); 482 482 String suggestion = fcm.getInitialDirectory() + File.separator + s.getFileNamePart(); 483 483 484 484 FileFilter ff; … … 487 487 } else { 488 488 ff = new ExtensionFileFilter("xml,zip", "xml", tr("Map paint style file (*.xml, *.zip)")); 489 489 } 490 fcm.createFileChooser(false, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY)490 fcm.createFileChooser(false, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, GenericFileChooser.FILES_ONLY) 491 491 .getFileChooser().setSelectedFile(new File(suggestion)); 492 JFileChooser fc = fcm.openFileChooser();492 GenericFileChooser fc = fcm.openFileChooser(); 493 493 if (fc == null) 494 494 return; 495 495 Main.worker.submit(new SaveToFileTask(s, fc.getSelectedFile())); -
src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java
41 41 import javax.swing.BorderFactory; 42 42 import javax.swing.JButton; 43 43 import javax.swing.JCheckBox; 44 import javax.swing.JFileChooser;45 44 import javax.swing.JLabel; 46 45 import javax.swing.JList; 47 46 import javax.swing.JOptionPane; … … 69 68 import org.openstreetmap.josm.gui.ExtendedDialog; 70 69 import org.openstreetmap.josm.gui.layer.GpxLayer; 71 70 import org.openstreetmap.josm.gui.layer.Layer; 71 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 72 72 import org.openstreetmap.josm.gui.widgets.JosmComboBox; 73 73 import org.openstreetmap.josm.gui.widgets.JosmTextField; 74 74 import org.openstreetmap.josm.io.GpxReader; … … 147 147 return tr("GPX Files (*.gpx *.gpx.gz)"); 148 148 } 149 149 }; 150 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, filter, JFileChooser.FILES_ONLY, null);150 GenericFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, filter, GenericFileChooser.FILES_ONLY, null); 151 151 if (fc == null) 152 152 return; 153 153 File sel = fc.getSelectedFile(); … … 379 379 380 380 @Override 381 381 public void actionPerformed(ActionEvent ae) { 382 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, JpegFileFilter.getInstance(), JFileChooser.FILES_ONLY, "geoimage.lastdirectory");382 GenericFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, JpegFileFilter.getInstance(), GenericFileChooser.FILES_ONLY, "geoimage.lastdirectory"); 383 383 if (fc == null) 384 384 return; 385 385 File sel = fc.getSelectedFile(); -
src/org/openstreetmap/josm/gui/layer/gpx/ImportAudioAction.java
14 14 import java.util.Comparator; 15 15 16 16 import javax.swing.AbstractAction; 17 import javax.swing.JFileChooser;18 17 import javax.swing.JOptionPane; 19 18 import javax.swing.filechooser.FileFilter; 20 19 … … 28 27 import org.openstreetmap.josm.gui.layer.GpxLayer; 29 28 import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker; 30 29 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 30 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 31 31 import org.openstreetmap.josm.tools.AudioUtil; 32 32 import org.openstreetmap.josm.tools.ImageProvider; 33 33 import org.openstreetmap.josm.tools.Utils; … … 76 76 return tr("Wave Audio files (*.wav)"); 77 77 } 78 78 }; 79 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, true, null, filter, JFileChooser.FILES_ONLY, "markers.lastaudiodirectory");79 GenericFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, true, null, filter, GenericFileChooser.FILES_ONLY, "markers.lastaudiodirectory"); 80 80 if (fc != null) { 81 81 File[] sel = fc.getSelectedFiles(); 82 82 // sort files in increasing order of timestamp (this is the end time, but so … … 130 130 Collection<WayPoint> waypoints = new ArrayList<>(); 131 131 boolean timedMarkersOmitted = false; 132 132 boolean untimedMarkersOmitted = false; 133 double snapDistance = Main.pref.getDouble("marker.audiofromuntimedwaypoints.distance", 1.0e-3); 133 double snapDistance = Main.pref.getDouble("marker.audiofromuntimedwaypoints.distance", 1.0e-3); 134 134 // about 25 m 135 135 WayPoint wayPointFromTimeStamp = null; 136 136 -
src/org/openstreetmap/josm/gui/layer/gpx/ImportImagesAction.java
10 10 import java.util.List; 11 11 12 12 import javax.swing.AbstractAction; 13 import javax.swing.JFileChooser;14 13 import javax.swing.JOptionPane; 15 14 16 15 import org.openstreetmap.josm.Main; 17 16 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 18 17 import org.openstreetmap.josm.gui.layer.GpxLayer; 19 18 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 20 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 19 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 20 import org.openstreetmap.josm.gui.widgets.GenericFileChooserManager; 21 21 import org.openstreetmap.josm.io.JpgImporter; 22 22 import org.openstreetmap.josm.tools.ImageProvider; 23 23 … … 52 52 return; 53 53 } 54 54 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();55 GenericFileChooser fc = new GenericFileChooserManager(true, "geoimage.lastdirectory", Main.pref.get("lastDirectory")).createFileChooser(true, null, importer.filter, GenericFileChooser.FILES_AND_DIRECTORIES).openFileChooser(); 56 56 if (fc != null) { 57 57 File[] sel = fc.getSelectedFiles(); 58 58 if (sel != null && sel.length > 0) { -
src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
52 52 import javax.swing.JButton; 53 53 import javax.swing.JCheckBox; 54 54 import javax.swing.JComponent; 55 import javax.swing.JFileChooser;56 55 import javax.swing.JLabel; 57 56 import javax.swing.JList; 58 57 import javax.swing.JOptionPane; … … 86 85 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 87 86 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 88 87 import org.openstreetmap.josm.gui.util.TableHelper; 89 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 88 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 89 import org.openstreetmap.josm.gui.widgets.GenericFileChooserManager; 90 90 import org.openstreetmap.josm.gui.widgets.JosmTextField; 91 91 import org.openstreetmap.josm.io.MirroredInputStream; 92 92 import org.openstreetmap.josm.io.OsmTransferException; … … 697 697 } 698 698 } 699 699 700 private static void prepareFileChooser(String url, JFileChooser fc) {700 private static void prepareFileChooser(String url, GenericFileChooser fc) { 701 701 if (url == null || url.trim().length() == 0) return; 702 702 URL sourceUrl = null; 703 703 try { … … 809 809 Main.error("Unsupported source type: "+sourceType); 810 810 return; 811 811 } 812 JFileChooserManager fcm = new JFileChooserManager(true)813 .createFileChooser(true, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY);812 GenericFileChooserManager fcm = new GenericFileChooserManager(true) 813 .createFileChooser(true, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, GenericFileChooser.FILES_ONLY); 814 814 prepareFileChooser(tfURL.getText(), fcm.getFileChooser()); 815 JFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));815 GenericFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this)); 816 816 if (fc != null) { 817 817 tfURL.setText(fc.getSelectedFile().toString()); 818 818 } … … 1502 1502 1503 1503 @Override 1504 1504 public void actionPerformed(ActionEvent e) { 1505 JFileChooserManager fcm = new JFileChooserManager(true).createFileChooser();1505 GenericFileChooserManager fcm = new GenericFileChooserManager(true).createFileChooser(); 1506 1506 if (!isFile) { 1507 fcm.getFileChooser().setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY);1507 fcm.getFileChooser().setFileSelectionMode(GenericFileChooser.DIRECTORIES_ONLY); 1508 1508 } 1509 1509 prepareFileChooser(tfFileName.getText(), fcm.getFileChooser()); 1510 JFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));1510 GenericFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this)); 1511 1511 if (fc != null) { 1512 1512 tfFileName.setText(fc.getSelectedFile().toString()); 1513 1513 } -
src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java
20 20 import javax.swing.AbstractAction; 21 21 import javax.swing.Box; 22 22 import javax.swing.JButton; 23 import javax.swing.JFileChooser;24 23 import javax.swing.JLabel; 25 24 import javax.swing.JMenu; 26 25 import javax.swing.JOptionPane; … … 44 43 import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory; 45 44 import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; 46 45 import org.openstreetmap.josm.gui.util.GuiHelper; 46 //import javax.swing.JFileChooser; 47 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 47 48 import org.openstreetmap.josm.gui.widgets.JosmTextField; 48 49 import org.openstreetmap.josm.tools.GBC; 49 50 … … 195 196 return tr("JOSM custom settings files (*.xml)"); 196 197 } 197 198 }; 198 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(!saveFileFlag, !saveFileFlag, title, filter, JFileChooser.FILES_ONLY, "customsettings.lastDirectory");199 GenericFileChooser fc = DiskAccessAction.createAndOpenFileChooser(!saveFileFlag, !saveFileFlag, title, filter, GenericFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 199 200 if (fc != null) { 200 201 File[] sel = fc.isMultiSelectionEnabled() ? fc.getSelectedFiles() : (new File[]{fc.getSelectedFile()}); 201 202 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
10 10 import java.util.Map; 11 11 12 12 import javax.swing.AbstractAction; 13 import javax.swing.JFileChooser;14 13 import javax.swing.JOptionPane; 15 14 import javax.swing.filechooser.FileFilter; 16 15 … … 19 18 import org.openstreetmap.josm.data.CustomConfigurator; 20 19 import org.openstreetmap.josm.data.Preferences; 21 20 import org.openstreetmap.josm.data.Preferences.Setting; 21 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 22 22 23 23 /** 24 24 * Action that exports some fragment of settings to custom configuration file … … 70 70 return tr("JOSM custom settings files (*.xml)"); 71 71 } 72 72 }; 73 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, "customsettings.lastDirectory");73 GenericFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter, GenericFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 74 74 if (fc != null) { 75 75 File sel = fc.getSelectedFile(); 76 76 if (!sel.getName().endsWith(".xml")) sel=new File(sel.getAbsolutePath()+".xml"); -
src/org/openstreetmap/josm/gui/widgets/FileDialogManager.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.util.Collection; 9 import java.util.Collections; 10 11 import javax.swing.filechooser.FileFilter; 12 13 import org.openstreetmap.josm.Main; 14 import org.openstreetmap.josm.actions.DiskAccessAction; 15 import org.openstreetmap.josm.actions.SaveActionBase; 16 17 /** 18 * A chained utility class used to create and open {@link FileDialog} dialogs.<br> 19 * Use only this class if you need to control specifically your FileDialog dialog.<br> 20 * <p> 21 * A simpler usage is to call the {@link DiskAccessAction#createAndOpenFileChooser} methods. 22 * 23 * @since 5438 24 */ 25 public class FileDialogManager { 26 private final boolean open; 27 private final String lastDirProperty; 28 private final String curDir; 29 30 private FileDialog fc; 31 32 /** 33 * Creates a new {@code FileDialogManager}. 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 FileDialogManager(boolean open) { 38 this(open, null); 39 } 40 41 /** 42 * Creates a new {@code FileDialogManager}. 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 FileDialog. 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 FileDialogManager(boolean open, String lastDirProperty) { 49 this(open, lastDirProperty, null); 50 } 51 52 /** 53 * Creates a new {@code FileDialogManager}. 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 FileDialog. 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 FileDialog if the {@code lastDirProperty} property value is missing. 58 * @see #createFileChooser 59 */ 60 public FileDialogManager(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 FileDialog} that has been previously created. 70 * @return The {@code FileDialog} that has been previously created, or {@code null} if it has not been created yet. 71 * @see #createFileChooser 72 */ 73 public final FileDialog getFileChooser() { 74 return fc; 75 } 76 77 /** 78 * Replies the initial directory used to construct the {@code FileDialog}. 79 * @return The initial directory used to construct the {@code FileDialog}. 80 */ 81 public final String getInitialDirectory() { 82 return curDir; 83 } 84 85 /** 86 * Creates a new {@link FileDialog} with default settings. All files will be accepted. 87 * @return this 88 */ 89 public final FileDialogManager createFileChooser() { 90 // return doCreateFileChooser(false, null, null, null, null, FileDialog.FILES_ONLY, false); 91 return null; 92 } 93 94 /** 95 * Creates a new {@link FileDialog} with given settings for a single {@code FileFilter}. 96 * 97 * @param multiple If true, makes the dialog allow multiple file selections 98 * @param title The string that goes in the dialog window's title bar 99 * @param filter The only file filter that will be proposed by the dialog 100 * @param selectionMode The selection mode that allows the user to:<br><ul> 101 * <li>just select files ({@code FileDialog.FILES_ONLY})</li> 102 * <li>just select directories ({@code FileDialog.DIRECTORIES_ONLY})</li> 103 * <li>select both files and directories ({@code FileDialog.FILES_AND_DIRECTORIES})</li></ul> 104 * @return this 105 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 106 */ 107 public final FileDialogManager createFileChooser(boolean multiple, String title, FileFilter filter, int selectionMode) { 108 doCreateFileChooser(multiple, title, Collections.singleton(filter), filter, null, selectionMode, false); 109 // getFileChooser().setAcceptAllFileFilterUsed(false); 110 return this; 111 } 112 113 /** 114 * Creates a new {@link FileDialog} with given settings for a collection of {@code FileFilter}s. 115 * 116 * @param multiple If true, makes the dialog allow multiple file selections 117 * @param title The string that goes in the dialog window's title bar 118 * @param filters The file filters that will be proposed by the dialog 119 * @param defaultFilter The file filter that will be selected by default 120 * @param selectionMode The selection mode that allows the user to:<br><ul> 121 * <li>just select files ({@code FileDialog.FILES_ONLY})</li> 122 * <li>just select directories ({@code FileDialog.DIRECTORIES_ONLY})</li> 123 * <li>select both files and directories ({@code FileDialog.FILES_AND_DIRECTORIES})</li></ul> 124 * @return this 125 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, Collection, FileFilter, int, String) 126 */ 127 public final FileDialogManager createFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode) { 128 return doCreateFileChooser(multiple, title, filters, defaultFilter, null, selectionMode, false); 129 } 130 131 /** 132 * Creates a new {@link FileDialog} with given settings for a file extension. 133 * 134 * @param multiple If true, makes the dialog allow multiple file selections 135 * @param title The string that goes in the dialog window's title bar 136 * @param extension The file extension that will be selected as the default file filter 137 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 138 * If false, only the file filters that include {@code extension} will be proposed 139 * @param selectionMode The selection mode that allows the user to:<br><ul> 140 * <li>just select files ({@code FileDialog.FILES_ONLY})</li> 141 * <li>just select directories ({@code FileDialog.DIRECTORIES_ONLY})</li> 142 * <li>select both files and directories ({@code FileDialog.FILES_AND_DIRECTORIES})</li></ul> 143 * @return this 144 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 145 */ 146 public final FileDialogManager createFileChooser(boolean multiple, String title, String extension, boolean allTypes, int selectionMode) { 147 return doCreateFileChooser(multiple, title, null, null, extension, selectionMode, allTypes); 148 } 149 150 private final FileDialogManager doCreateFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, String extension, int selectionMode, boolean allTypes) { 151 fc = new FileDialog(((Frame)Main.parent)); 152 fc.setDirectory(curDir); 153 if (title != null) { 154 fc.setTitle(title); 155 } 156 157 // fc.setFileSelectionMode(selectionMode); 158 fc.setMultipleMode(multiple); 159 // fc.setAcceptAllFileFilterUsed(false); 160 161 // if (filters != null) { 162 // for (FileFilter filter : filters) { 163 // fc.addChoosableFileFilter(filter); 164 // } 165 // if (defaultFilter != null) { 166 // fc.setFileFilter(defaultFilter); 167 // } 168 // } else if (open) { 169 // ExtensionFileFilter.applyChoosableImportFileFilters(fc, extension, allTypes); 170 // } else { 171 // ExtensionFileFilter.applyChoosableExportFileFilters(fc, extension, allTypes); 172 // } 173 return this; 174 } 175 176 /** 177 * Opens the {@code FileDialog} that has been created. Nothing happens if it has not been created yet. 178 * @return the {@code FileDialog} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog. 179 */ 180 public final FileDialog openFileChooser() { 181 return openFileChooser(null); 182 } 183 184 /** 185 * Opens the {@code FileDialog} that has been created and waits for the user to choose a file/directory, or cancel the dialog.<br> 186 * Nothing happens if the dialog has not been created yet.<br> 187 * When the user choses a file or directory, the {@code lastDirProperty} is updated to the chosen directory path. 188 * 189 * @param parent The Component used as the parent of the FileDialog. If null, uses {@code Main.parent}. 190 * @return the {@code FileDialog} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog. 191 */ 192 public FileDialog openFileChooser(Component parent) { 193 if (fc != null) { 194 if (parent == null) { 195 parent = Main.parent; 196 } 197 198 // int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent); 199 // if (answer != FileDialog.APPROVE_OPTION) { 200 // return null; 201 // } 202 // 203 // if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 204 // Main.pref.put(lastDirProperty, fc.getDirectory().getAbsolutePath()); 205 // } 206 // 207 if (!open) { 208 File file = new File(fc.getFile()); 209 if (!SaveActionBase.confirmOverwrite(file)) { 210 return null; 211 } 212 } 213 } 214 return fc; 215 } 216 } -
src/org/openstreetmap/josm/gui/widgets/GenericFileChooser.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 import java.util.Locale; 10 11 import javax.swing.JFileChooser; 12 import javax.swing.filechooser.FileFilter; 13 14 import org.openstreetmap.josm.Main; 15 16 /** 17 * Generic File chooser which chooses based on "use.native.file.dialog" the native dialog via FileDialog, 18 * or the more generic JFileChooser. This class should behave like a normal JFileChooser. If something is 19 * missing form JFileChooser please implement here with both dialogs handled (if possible). 20 * 21 * Generally speaking this directs the input to the right instance of JFileChooser or FileDialog or implements 22 * the missing features. It should act like a JFileChooser as a facade and implement for a FileDialog the missing 23 * (so possible) features. 24 * 25 * Basically this is only a facade for different Open/Save Dialogs. 26 * 27 * This implements only the currently in JOSM needed methods - no more no less. 28 * 29 * It will construct/update both instances to avoid keeping out of sync dialogs. 30 * 31 * @author lesath 32 * 33 */ 34 public class GenericFileChooser { 35 36 public static final int FILES_ONLY = JFileChooser.FILES_ONLY; 37 public static final int APPROVE_OPTION = JFileChooser.APPROVE_OPTION; 38 public static final int FILES_AND_DIRECTORIES = JFileChooser.FILES_AND_DIRECTORIES; 39 public static final int DIRECTORIES_ONLY = JFileChooser.DIRECTORIES_ONLY; 40 41 /** The JFileChooser which we use for this. */ 42 private JFileChooser jFileChooser; 43 44 /** The instance of the fileDialog */ 45 private FileDialog fileDialog; 46 47 /** The locale for both Dialogs */ 48 private static Locale locale; 49 50 /** Based on this every method decides what to do with the different dialogs. */ 51 private boolean usesNativeFileDialog; 52 53 public GenericFileChooser(File file) { 54 55 // Try to fetch value from start-properties 56 usesNativeFileDialog = Main.pref.getBoolean("use.native.file.dialog"); 57 58 jFileChooser = new JFileChooser(file); 59 60 fileDialog = new FileDialog((Frame) Main.parent); 61 if(file != null) { 62 fileDialog.setDirectory(file.getAbsolutePath()); 63 fileDialog.setFile(file.toString()); 64 } 65 } 66 67 public void setDialogTitle(String title) { 68 fileDialog.setTitle(title); 69 jFileChooser.setDialogTitle(title); 70 } 71 72 /** 73 * <code>java.awt.FileDialog</code> does not support a selection mode for different types 74 * of files, so this is not used when using FileDialog. 75 */ 76 public void setFileSelectionMode(int selectionMode) { 77 jFileChooser.setFileSelectionMode(selectionMode); 78 } 79 80 public void setMultiSelectionEnabled(boolean multiple) { 81 jFileChooser.setMultiSelectionEnabled(multiple); 82 fileDialog.setMultipleMode(multiple); 83 } 84 85 public File getSelectedFile() { 86 if(usesNativeFileDialog) { 87 return new File(fileDialog.getDirectory()+fileDialog.getFile()); 88 } 89 else { 90 return jFileChooser.getSelectedFile(); 91 } 92 } 93 public File[] getSelectedFiles() { 94 if(usesNativeFileDialog) { 95 return fileDialog.getFiles(); 96 } 97 return jFileChooser.getSelectedFiles(); 98 } 99 public void setSelectedFile(File file) { 100 fileDialog.setDirectory(file.getAbsolutePath()); 101 fileDialog.setFile(file.getName()); 102 jFileChooser.setSelectedFile(file); 103 } 104 105 /** 106 * <code>java.awt.FileDialog</code> does not support a selection mode for different types 107 * of files, so this is not used when using FileDialog. 108 */ 109 public void setAcceptAllFileFilterUsed(boolean b) { 110 jFileChooser.setAcceptAllFileFilterUsed(b); 111 } 112 113 /** 114 * <code>java.awt.FileDialog</code> uses "somewhat" of a filefilter - named <code>FilenameFilter</code>. 115 * This is not supported by some platforms (namely Windows), so this is only implemented for for 116 * jFileChooser yet. 117 * 118 * @return The currently used FileFilter. 119 */ 120 public FileFilter getFileFilter() { 121 return jFileChooser.getFileFilter(); 122 } 123 /** 124 * <code>java.awt.FileDialog</code> uses "somewhat" of a filefilter - named <code>FilenameFilter</code>. 125 */ 126 public FileFilter[] getChoosableFileFilters() { 127 return jFileChooser.getChoosableFileFilters(); 128 } 129 public void addChoosableFileFilter(FileFilter filter) { 130 jFileChooser.addChoosableFileFilter(filter); 131 } 132 public void setFileFilter(final FileFilter cff) { 133 FilenameFilter filter = new FilenameFilter() { 134 public boolean accept(File Directory, String fileName) { 135 return cff.accept(new File(Directory.getAbsolutePath()+fileName)); 136 } 137 }; 138 fileDialog.setFilenameFilter(filter); 139 jFileChooser.setFileFilter(cff); 140 } 141 142 public boolean isMultiSelectionEnabled() { 143 if(usesNativeFileDialog) { 144 return fileDialog.isMultipleMode(); 145 } 146 return jFileChooser.isMultiSelectionEnabled(); 147 } 148 public int showOpenDialog(Component parent) { 149 if(usesNativeFileDialog) { 150 fileDialog.setLocale(locale); 151 fileDialog.setMode(FileDialog.LOAD); 152 fileDialog.setVisible(true); 153 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION; 154 } 155 jFileChooser.setLocale(locale); 156 return jFileChooser.showOpenDialog(parent); 157 } 158 public int showSaveDialog(Component parent) { 159 if(usesNativeFileDialog) { 160 fileDialog.setLocale(locale); 161 fileDialog.setMode(FileDialog.SAVE); 162 fileDialog.setVisible(true); 163 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION; 164 } 165 return jFileChooser.showSaveDialog(parent); 166 } 167 public File getCurrentDirectory() { 168 if(usesNativeFileDialog) { 169 return new File(fileDialog.getDirectory()); 170 } 171 return jFileChooser.getCurrentDirectory(); 172 } 173 public void setCurrentDirectory(File f) { 174 jFileChooser.setCurrentDirectory(f); 175 fileDialog.setDirectory(f.toString()); 176 } 177 public static void setDefaultLocale(Locale l) { 178 GenericFileChooser.locale = l; 179 } 180 } -
src/org/openstreetmap/josm/gui/widgets/GenericFileChooserManager.java
6 6 import java.util.Collection; 7 7 import java.util.Collections; 8 8 9 import javax.swing.JFileChooser;10 9 import javax.swing.filechooser.FileFilter; 11 10 12 11 import org.openstreetmap.josm.Main; … … 15 14 import org.openstreetmap.josm.actions.SaveActionBase; 16 15 17 16 /** 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>17 * A chained utility class used to create and open {@link GenericFileChooser} dialogs.<br> 18 * Use only this class if you need to control specifically your GenericFileChooser dialog.<br> 20 19 * <p> 21 20 * A simpler usage is to call the {@link DiskAccessAction#createAndOpenFileChooser} methods. 22 21 * 23 22 * @since 5438 24 23 */ 25 public class JFileChooserManager {24 public class GenericFileChooserManager { 26 25 private final boolean open; 27 26 private final String lastDirProperty; 28 27 private final String curDir; 29 28 30 private JFileChooser fc;29 private GenericFileChooser fc; 31 30 32 31 /** 33 * Creates a new {@code JFileChooserManager}.32 * Creates a new {@code GenericFileChooserManager}. 34 33 * @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created. 35 34 * @see #createFileChooser 36 35 */ 37 public JFileChooserManager(boolean open) {36 public GenericFileChooserManager(boolean open) { 38 37 this(open, null); 39 38 } 40 39 41 40 /** 42 * Creates a new {@code JFileChooserManager}.41 * Creates a new {@code GenericFileChooserManager}. 43 42 * @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.43 * @param lastDirProperty The name of the property used to get the last directory. This directory is used to initialize the GenericFileChooser. 45 44 * Then, if the user effectively chooses a file or a directory, this property will be updated to the directory path. 46 45 * @see #createFileChooser 47 46 */ 48 public JFileChooserManager(boolean open, String lastDirProperty) {47 public GenericFileChooserManager(boolean open, String lastDirProperty) { 49 48 this(open, lastDirProperty, null); 50 49 } 51 50 52 51 /** 53 * Creates a new {@code JFileChooserManager}.52 * Creates a new {@code GenericFileChooserManager}. 54 53 * @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.54 * @param lastDirProperty The name of the property used to get the last directory. This directory is used to initialize the GenericFileChooser. 56 55 * 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.56 * @param defaultDir The default directory used to initialize the GenericFileChooser if the {@code lastDirProperty} property value is missing. 58 57 * @see #createFileChooser 59 58 */ 60 public JFileChooserManager(boolean open, String lastDirProperty, String defaultDir) {59 public GenericFileChooserManager(boolean open, String lastDirProperty, String defaultDir) { 61 60 this.open = open; 62 61 this.lastDirProperty = lastDirProperty == null || lastDirProperty.isEmpty() ? "lastDirectory" : lastDirProperty; 63 62 this.curDir = Main.pref.get(this.lastDirProperty).isEmpty() ? … … 66 65 } 67 66 68 67 /** 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.68 * Replies the {@code GenericFileChooser} that has been previously created. 69 * @return The {@code GenericFileChooser} that has been previously created, or {@code null} if it has not been created yet. 71 70 * @see #createFileChooser 72 71 */ 73 public final JFileChooser getFileChooser() {72 public final GenericFileChooser getFileChooser() { 74 73 return fc; 75 74 } 76 75 77 76 /** 78 * Replies the initial directory used to construct the {@code JFileChooser}.79 * @return The initial directory used to construct the {@code JFileChooser}.77 * Replies the initial directory used to construct the {@code GenericFileChooser}. 78 * @return The initial directory used to construct the {@code GenericFileChooser}. 80 79 */ 81 80 public final String getInitialDirectory() { 82 81 return curDir; 83 82 } 84 83 85 84 /** 86 * Creates a new {@link JFileChooser} with default settings. All files will be accepted.85 * Creates a new {@link GenericFileChooser} with default settings. All files will be accepted. 87 86 * @return this 88 87 */ 89 public final JFileChooserManager createFileChooser() {90 return doCreateFileChooser(false, null, null, null, null, JFileChooser.FILES_ONLY, false);88 public final GenericFileChooserManager createFileChooser() { 89 return doCreateFileChooser(false, null, null, null, null, GenericFileChooser.FILES_ONLY, false); 91 90 } 92 91 93 92 /** 94 * Creates a new {@link JFileChooser} with given settings for a single {@code FileFilter}.93 * Creates a new {@link GenericFileChooser} with given settings for a single {@code FileFilter}. 95 94 * 96 95 * @param multiple If true, makes the dialog allow multiple file selections 97 96 * @param title The string that goes in the dialog window's title bar 98 97 * @param filter The only file filter that will be proposed by the dialog 99 98 * @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>99 * <li>just select files ({@code GenericFileChooser.FILES_ONLY})</li> 100 * <li>just select directories ({@code GenericFileChooser.DIRECTORIES_ONLY})</li> 101 * <li>select both files and directories ({@code GenericFileChooser.FILES_AND_DIRECTORIES})</li></ul> 103 102 * @return this 104 103 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 105 104 */ 106 public final JFileChooserManager createFileChooser(boolean multiple, String title, FileFilter filter, int selectionMode) {105 public final GenericFileChooserManager createFileChooser(boolean multiple, String title, FileFilter filter, int selectionMode) { 107 106 doCreateFileChooser(multiple, title, Collections.singleton(filter), filter, null, selectionMode, false); 108 107 getFileChooser().setAcceptAllFileFilterUsed(false); 109 108 return this; 110 109 } 111 110 112 111 /** 113 * Creates a new {@link JFileChooser} with given settings for a collection of {@code FileFilter}s.112 * Creates a new {@link GenericFileChooser} with given settings for a collection of {@code FileFilter}s. 114 113 * 115 114 * @param multiple If true, makes the dialog allow multiple file selections 116 115 * @param title The string that goes in the dialog window's title bar 117 116 * @param filters The file filters that will be proposed by the dialog 118 117 * @param defaultFilter The file filter that will be selected by default 119 118 * @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>119 * <li>just select files ({@code GenericFileChooser.FILES_ONLY})</li> 120 * <li>just select directories ({@code GenericFileChooser.DIRECTORIES_ONLY})</li> 121 * <li>select both files and directories ({@code GenericFileChooser.FILES_AND_DIRECTORIES})</li></ul> 123 122 * @return this 124 123 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, Collection, FileFilter, int, String) 125 124 */ 126 public final JFileChooserManager createFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode) {125 public final GenericFileChooserManager createFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode) { 127 126 return doCreateFileChooser(multiple, title, filters, defaultFilter, null, selectionMode, false); 128 127 } 129 128 130 129 /** 131 * Creates a new {@link JFileChooser} with given settings for a file extension.130 * Creates a new {@link GenericFileChooser} with given settings for a file extension. 132 131 * 133 132 * @param multiple If true, makes the dialog allow multiple file selections 134 133 * @param title The string that goes in the dialog window's title bar … … 136 135 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 137 136 * If false, only the file filters that include {@code extension} will be proposed 138 137 * @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>138 * <li>just select files ({@code GenericFileChooser.FILES_ONLY})</li> 139 * <li>just select directories ({@code GenericFileChooser.DIRECTORIES_ONLY})</li> 140 * <li>select both files and directories ({@code GenericFileChooser.FILES_AND_DIRECTORIES})</li></ul> 142 141 * @return this 143 142 * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String) 144 143 */ 145 public final JFileChooserManager createFileChooser(boolean multiple, String title, String extension, boolean allTypes, int selectionMode) {144 public final GenericFileChooserManager createFileChooser(boolean multiple, String title, String extension, boolean allTypes, int selectionMode) { 146 145 return doCreateFileChooser(multiple, title, null, null, extension, selectionMode, allTypes); 147 146 } 148 147 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));148 private final GenericFileChooserManager doCreateFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, FileFilter defaultFilter, String extension, int selectionMode, boolean allTypes) { 149 fc = new GenericFileChooser(new File(curDir)); 151 150 if (title != null) { 152 151 fc.setDialogTitle(title); 153 152 } … … 172 171 } 173 172 174 173 /** 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.174 * Opens the {@code GenericFileChooser} that has been created. Nothing happens if it has not been created yet. 175 * @return the {@code GenericFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog. 177 176 */ 178 public final JFileChooser openFileChooser() {177 public final GenericFileChooser openFileChooser() { 179 178 return openFileChooser(null); 180 179 } 181 180 182 181 /** 183 * Opens the {@code JFileChooser} that has been created and waits for the user to choose a file/directory, or cancel the dialog.<br>182 * Opens the {@code GenericFileChooser} that has been created and waits for the user to choose a file/directory, or cancel the dialog.<br> 184 183 * Nothing happens if the dialog has not been created yet.<br> 185 184 * When the user choses a file or directory, the {@code lastDirProperty} is updated to the chosen directory path. 186 185 * 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.186 * @param parent The Component used as the parent of the GenericFileChooser. If null, uses {@code Main.parent}. 187 * @return the {@code GenericFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog. 189 188 */ 190 public JFileChooser openFileChooser(Component parent) {189 public GenericFileChooser openFileChooser(Component parent) { 191 190 if (fc != null) { 192 191 if (parent == null) { 193 192 parent = Main.parent; 194 193 } 195 194 196 195 int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent); 197 if (answer != JFileChooser.APPROVE_OPTION) {196 if (answer != GenericFileChooser.APPROVE_OPTION) { 198 197 return null; 199 198 } 200 199 -
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/tools/I18n.java
20 20 import java.util.zip.ZipEntry; 21 21 22 22 import javax.swing.JColorChooser; 23 import javax.swing.JFileChooser;24 23 import javax.swing.UIManager; 25 24 26 25 import org.openstreetmap.gui.jmapviewer.FeatureAdapter.TranslationAdapter; 27 26 import org.openstreetmap.josm.Main; 27 import org.openstreetmap.josm.gui.widgets.GenericFileChooser; 28 28 29 29 /** 30 30 * Internationalisation support. … … 640 640 public static void translateJavaInternalMessages() { 641 641 Locale l = Locale.getDefault(); 642 642 643 JFileChooser.setDefaultLocale(l);643 GenericFileChooser.setDefaultLocale(l); 644 644 JColorChooser.setDefaultLocale(l); 645 645 for (String key : javaInternalMessageKeys) { 646 646 String us = UIManager.getString(key, Locale.US);