Ticket #10024: 10024_beta1.patch

File 10024_beta1.patch, 65.6 KB (added by Don-vip, 12 years ago)
  • src/org/openstreetmap/josm/actions/DiskAccessAction.java

     
    66import javax.swing.JFileChooser;
    77import javax.swing.filechooser.FileFilter;
    88
    9 import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
     9import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
     10import org.openstreetmap.josm.gui.widgets.FileChooserManager;
    1011import org.openstreetmap.josm.tools.Shortcut;
    1112
    1213/**
     
    4546    }
    4647
    4748    /**
    48      * Creates a new {@link JFileChooser} and makes it visible.
     49     * Creates a new {@link AbstractFileChooser} and makes it visible.
    4950     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
    5051     * @param multiple If true, makes the dialog allow multiple file selections
    5152     * @param title The string that goes in the dialog window's title bar
    52      * @return The {@code JFileChooser}.
     53     * @return The {@code AbstractFileChooser}.
    5354     * @since 1646
    5455     */
    55     public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {
     56    public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {
    5657        return createAndOpenFileChooser(open, multiple, title, null);
    5758    }
    5859
    5960    /**
    60      * Creates a new {@link JFileChooser} and makes it visible.
     61     * Creates a new {@link AbstractFileChooser} and makes it visible.
    6162     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
    6263     * @param multiple If true, makes the dialog allow multiple file selections
    6364     * @param title The string that goes in the dialog window's title bar
    6465     * @param extension The file extension that will be selected as the default file filter
    65      * @return The {@code JFileChooser}.
     66     * @return The {@code AbstractFileChooser}.
    6667     * @since 2020
    6768     */
    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) {
    6970        return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null);
    7071    }
    7172
    7273    /**
    73      * Creates a new {@link JFileChooser} and makes it visible.
     74     * Creates a new {@link AbstractFileChooser} and makes it visible.
    7475     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
    7576     * @param multiple If true, makes the dialog allow multiple file selections
    7677     * @param title The string that goes in the dialog window's title bar
     
    8182     *                      <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
    8283     * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
    8384     *                 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.
    8586     *        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}.
    8788     * @since 5438
    8889     */
    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();
    9192    }
    9293
    9394    /**
    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.
    9596     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
    9697     * @param multiple If true, makes the dialog allow multiple file selections
    9798     * @param title The string that goes in the dialog window's title bar
     
    100101     *                      <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
    101102     *                      <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
    102103     *                      <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 user
    104      * @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}.
    105106     * @since 5438
    106107     */
    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();
    109110    }
    110111
    111112    /**
    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.
    113114     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
    114115     * @param multiple If true, makes the dialog allow multiple file selections
    115116     * @param title The string that goes in the dialog window's title bar
     
    120121     *                      <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
    121122     *                      <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
    122123     * @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}.
    124125     * @since 5438
    125126     */
    126     public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title,
     127    public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title,
    127128            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();
    129130    }
    130131}
  • src/org/openstreetmap/josm/actions/ExtensionFileFilter.java

     
    99import java.util.List;
    1010import java.util.ServiceConfigurationError;
    1111
    12 import javax.swing.JFileChooser;
    1312import javax.swing.filechooser.FileFilter;
    1413
    1514import org.openstreetmap.josm.Main;
    1615import org.openstreetmap.josm.gui.MapView;
     16import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    1717import org.openstreetmap.josm.io.AllFormatsImporter;
    1818import org.openstreetmap.josm.io.FileExporter;
    1919import org.openstreetmap.josm.io.FileImporter;
     
    223223    }
    224224
    225225    /**
    226      * Applies the choosable {@link FileFilter} to a {@link JFileChooser} before using the
     226     * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
    227227     * file chooser for selecting a file for reading.
    228228     *
    229229     * @param fileChooser the file chooser
     
    232232     *                 If false, only the file filters that include {@code extension} will be proposed
    233233     * @since 5438
    234234     */
    235     public static void applyChoosableImportFileFilters(JFileChooser fileChooser, String extension, boolean allTypes) {
     235    public static void applyChoosableImportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
    236236        for (ExtensionFileFilter filter: getImportExtensionFileFilters()) {
    237237            if (allTypes || filter.acceptName("file."+extension)) {
    238238                fileChooser.addChoosableFileFilter(filter);
     
    242242    }
    243243
    244244    /**
    245      * Applies the choosable {@link FileFilter} to a {@link JFileChooser} before using the
     245     * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
    246246     * file chooser for selecting a file for writing.
    247247     *
    248248     * @param fileChooser the file chooser
     
    251251     *                 If false, only the file filters that include {@code extension} will be proposed
    252252     * @since 5438
    253253     */
    254     public static void applyChoosableExportFileFilters(JFileChooser fileChooser, String extension, boolean allTypes) {
     254    public static void applyChoosableExportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
    255255        for (ExtensionFileFilter filter: getExportExtensionFileFilters()) {
    256256            if (allTypes || filter.acceptName("file."+extension)) {
    257257                fileChooser.addChoosableFileFilter(filter);
  • src/org/openstreetmap/josm/actions/OpenFileAction.java

     
    2424import java.util.regex.Matcher;
    2525import java.util.regex.Pattern;
    2626
    27 import javax.swing.JFileChooser;
    2827import javax.swing.JOptionPane;
    2928import javax.swing.SwingUtilities;
    3029import javax.swing.filechooser.FileFilter;
     
    3332import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    3433import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    3534import org.openstreetmap.josm.gui.help.HelpUtil;
     35import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    3636import org.openstreetmap.josm.io.AllFormatsImporter;
    3737import org.openstreetmap.josm.io.FileImporter;
    3838import org.openstreetmap.josm.io.OsmTransferException;
     
    6464
    6565    @Override
    6666    public void actionPerformed(ActionEvent e) {
    67         JFileChooser fc = createAndOpenFileChooser(true, true, null);
     67        AbstractFileChooser fc = createAndOpenFileChooser(true, true, null);
    6868        if (fc == null)
    6969            return;
    7070        File[] files = fc.getSelectedFiles();
  • src/org/openstreetmap/josm/actions/SaveActionBase.java

     
    1818import org.openstreetmap.josm.gui.ExtendedDialog;
    1919import org.openstreetmap.josm.gui.layer.Layer;
    2020import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     21import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    2122import org.openstreetmap.josm.io.FileExporter;
    2223import org.openstreetmap.josm.tools.Shortcut;
    2324
     
    135136     * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String)
    136137     */
    137138    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);
    139140        return checkFileAndConfirmOverWrite(fc, filter.getDefaultExtension());
    140141    }
    141142
     
    149150     * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, String)
    150151     */
    151152    public static File createAndOpenSaveFileChooser(String title, String extension) {
    152         JFileChooser fc = createAndOpenFileChooser(false, false, title, extension);
     153        AbstractFileChooser fc = createAndOpenFileChooser(false, false, title, extension);
    153154        return checkFileAndConfirmOverWrite(fc, extension);
    154155    }
    155156
    156     private static File checkFileAndConfirmOverWrite(JFileChooser fc, String extension) {
     157    private static File checkFileAndConfirmOverWrite(AbstractFileChooser fc, String extension) {
    157158        if (fc == null) return null;
    158159        File file = fc.getSelectedFile();
    159160
  • src/org/openstreetmap/josm/actions/SessionLoadAction.java

     
    2424import org.openstreetmap.josm.gui.layer.Layer;
    2525import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2626import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
     27import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    2728import org.openstreetmap.josm.io.IllegalDataException;
    2829import org.openstreetmap.josm.io.session.SessionImporter;
    2930import org.openstreetmap.josm.io.session.SessionReader;
     
    4647
    4748    @Override
    4849    public void actionPerformed(ActionEvent e) {
    49         JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"),
     50        AbstractFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"),
    5051                Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()),
    5152                SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory");
    5253        if (fc == null) return;
  • src/org/openstreetmap/josm/actions/SessionSaveAsAction.java

     
    3535import org.openstreetmap.josm.gui.ExtendedDialog;
    3636import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    3737import org.openstreetmap.josm.gui.layer.Layer;
     38import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    3839import org.openstreetmap.josm.io.session.SessionLayerExporter;
    3940import org.openstreetmap.josm.io.session.SessionWriter;
    4041import org.openstreetmap.josm.tools.GBC;
     
    8182        FileFilter joz = new ExtensionFileFilter("joz", "joz", tr("Session file (archive) (*.joz)"));
    8283        FileFilter jos = new ExtensionFileFilter("jos", "jos", tr("Session file (*.jos)"));
    8384
    84         JFileChooser fc;
     85        AbstractFileChooser fc;
    8586
    8687        if (zipRequired) {
    8788            fc = createAndOpenFileChooser(false, false, tr("Save session"), joz, JFileChooser.FILES_ONLY, "lastDirectory");
  • src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

     
    7272import org.openstreetmap.josm.gui.preferences.SourceEntry;
    7373import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
    7474import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
     75import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
     76import org.openstreetmap.josm.gui.widgets.FileChooserManager;
    7577import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    76 import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
    7778import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    7879import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    7980import org.openstreetmap.josm.tools.GBC;
     
    442443                return;
    443444            final StyleSource s = model.getRow(sel);
    444445
    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"));
    446447            String suggestion = fcm.getInitialDirectory() + File.separator + s.getFileNamePart();
    447448
    448449            FileFilter ff;
     
    453454            }
    454455            fcm.createFileChooser(false, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY)
    455456                    .getFileChooser().setSelectedFile(new File(suggestion));
    456             JFileChooser fc = fcm.openFileChooser();
     457            AbstractFileChooser fc = fcm.openFileChooser();
    457458            if (fc == null)
    458459                return;
    459460            Main.worker.submit(new SaveToFileTask(s, fc.getSelectedFile()));
  • src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java

     
    7171import org.openstreetmap.josm.gui.ExtendedDialog;
    7272import org.openstreetmap.josm.gui.layer.GpxLayer;
    7373import org.openstreetmap.josm.gui.layer.Layer;
     74import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    7475import org.openstreetmap.josm.gui.widgets.JosmComboBox;
    7576import org.openstreetmap.josm.gui.widgets.JosmTextField;
    7677import org.openstreetmap.josm.io.GpxReader;
     
    150151                    return tr("GPX Files (*.gpx *.gpx.gz)");
    151152                }
    152153            };
    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);
    154155            if (fc == null)
    155156                return;
    156157            File sel = fc.getSelectedFile();
     
    382383
    383384                @Override
    384385                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(),
    386387                            JFileChooser.FILES_ONLY, "geoimage.lastdirectory");
    387388                    if (fc == null)
    388389                        return;
  • src/org/openstreetmap/josm/gui/layer/gpx/ImportAudioAction.java

     
    2929import org.openstreetmap.josm.gui.layer.GpxLayer;
    3030import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker;
    3131import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
     32import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    3233import org.openstreetmap.josm.tools.AudioUtil;
    3334import org.openstreetmap.josm.tools.ImageProvider;
    3435import org.openstreetmap.josm.tools.Utils;
     
    7778                return tr("Wave Audio files (*.wav)");
    7879            }
    7980        };
    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");
    8182        if (fc != null) {
    8283            File[] sel = fc.getSelectedFiles();
    8384            // sort files in increasing order of timestamp (this is the end time, but so
  • src/org/openstreetmap/josm/gui/layer/gpx/ImportImagesAction.java

     
    1717import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    1818import org.openstreetmap.josm.gui.layer.GpxLayer;
    1919import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    20 import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
     20import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
     21import org.openstreetmap.josm.gui.widgets.FileChooserManager;
    2122import org.openstreetmap.josm.io.JpgImporter;
    2223import org.openstreetmap.josm.tools.ImageProvider;
    2324
     
    5253            return;
    5354        }
    5455        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();
    5658        if (fc != null) {
    5759            File[] sel = fc.getSelectedFiles();
    5860            if (sel != null && sel.length > 0) {
  • src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

     
    8888import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
    8989import org.openstreetmap.josm.gui.util.GuiHelper;
    9090import org.openstreetmap.josm.gui.util.TableHelper;
    91 import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
     91import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
     92import org.openstreetmap.josm.gui.widgets.FileChooserManager;
    9293import org.openstreetmap.josm.gui.widgets.JosmTextField;
    9394import org.openstreetmap.josm.io.CachedFile;
    9495import org.openstreetmap.josm.io.OnlineResource;
     
    700701        }
    701702    }
    702703
    703     private static void prepareFileChooser(String url, JFileChooser fc) {
     704    private static void prepareFileChooser(String url, AbstractFileChooser fc) {
    704705        if (url == null || url.trim().length() == 0) return;
    705706        URL sourceUrl = null;
    706707        try {
     
    812813                    Main.error("Unsupported source type: "+sourceType);
    813814                    return;
    814815                }
    815                 JFileChooserManager fcm = new JFileChooserManager(true)
     816                FileChooserManager fcm = new FileChooserManager(true)
    816817                        .createFileChooser(true, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY);
    817818                prepareFileChooser(tfURL.getText(), fcm.getFileChooser());
    818                 JFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));
     819                AbstractFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));
    819820                if (fc != null) {
    820821                    tfURL.setText(fc.getSelectedFile().toString());
    821822                }
     
    15111512
    15121513            @Override
    15131514            public void actionPerformed(ActionEvent e) {
    1514                 JFileChooserManager fcm = new JFileChooserManager(true).createFileChooser();
     1515                FileChooserManager fcm = new FileChooserManager(true).createFileChooser();
    15151516                if (!isFile) {
    15161517                    fcm.getFileChooser().setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    15171518                }
    15181519                prepareFileChooser(tfFileName.getText(), fcm.getFileChooser());
    1519                 JFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));
     1520                AbstractFileChooser fc = fcm.openFileChooser(JOptionPane.getFrameForComponent(SourceEditor.this));
    15201521                if (fc != null) {
    15211522                    tfFileName.setText(fc.getSelectedFile().toString());
    15221523                }
  • src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java

     
    4545import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
    4646import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
    4747import org.openstreetmap.josm.gui.util.GuiHelper;
     48import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    4849import org.openstreetmap.josm.gui.widgets.JosmTextField;
    4950import org.openstreetmap.josm.tools.GBC;
    5051
     
    196197                return tr("JOSM custom settings files (*.xml)");
    197198            }
    198199        };
    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");
    200202        if (fc != null) {
    201203            File[] sel = fc.isMultiSelectionEnabled() ? fc.getSelectedFiles() : (new File[]{fc.getSelectedFile()});
    202204            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

     
    1919import org.openstreetmap.josm.data.CustomConfigurator;
    2020import org.openstreetmap.josm.data.Preferences;
    2121import org.openstreetmap.josm.data.Preferences.Setting;
     22import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    2223
    2324/**
    2425 * Action that exports some fragment of settings to custom configuration file
     
    7071                return tr("JOSM custom settings files (*.xml)");
    7172            }
    7273        };
    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");
    7475        if (fc != null) {
    7576            File sel = fc.getSelectedFile();
    7677            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.
     2package org.openstreetmap.josm.gui.widgets;
     3
     4import java.awt.Component;
     5import java.awt.HeadlessException;
     6import java.io.File;
     7import java.util.Locale;
     8
     9import javax.swing.filechooser.FileFilter;
     10
     11/**
     12 * Abstract class to allow different file chooser implementations.
     13 */
     14public 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.
     2package org.openstreetmap.josm.gui.widgets;
     3
     4import java.awt.Component;
     5import java.io.File;
     6import java.util.Collection;
     7import java.util.Collections;
     8
     9import javax.swing.JFileChooser;
     10import javax.swing.filechooser.FileFilter;
     11
     12import org.openstreetmap.josm.Main;
     13import org.openstreetmap.josm.actions.DiskAccessAction;
     14import org.openstreetmap.josm.actions.ExtensionFileFilter;
     15import 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 */
     25public 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 5438
    24  */
    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 #createFileChooser
    36      */
    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 #createFileChooser
    47      */
    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 #createFileChooser
    59      */
    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 #createFileChooser
    72      */
    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 this
    88      */
    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 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 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 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 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 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 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.
     2package org.openstreetmap.josm.gui.widgets;
     3
     4import java.awt.Component;
     5import java.awt.FileDialog;
     6import java.awt.Frame;
     7import java.io.File;
     8import java.io.FilenameFilter;
     9
     10import javax.swing.JFileChooser;
     11import javax.swing.filechooser.FileFilter;
     12
     13import 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 */
     19public 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.
     2package org.openstreetmap.josm.gui.widgets;
     3
     4import java.awt.Component;
     5import java.io.File;
     6
     7import javax.swing.JFileChooser;
     8import javax.swing.filechooser.FileFilter;
     9
     10/**
     11 * File chooser based on the Swing's {@link JFileChooser} implementation.
     12 */
     13public 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

     
    2525
    2626import org.openstreetmap.gui.jmapviewer.FeatureAdapter.TranslationAdapter;
    2727import org.openstreetmap.josm.Main;
     28import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    2829
    2930/**
    3031 * Internationalisation support.
     
    640641    public static void translateJavaInternalMessages() {
    641642        Locale l = Locale.getDefault();
    642643
     644        AbstractFileChooser.setDefaultLocale(l);
    643645        JFileChooser.setDefaultLocale(l);
    644646        JColorChooser.setDefaultLocale(l);
    645647        for (String key : javaInternalMessageKeys) {