Changeset 9670 in josm


Ignore:
Timestamp:
2016-01-29T17:39:58+01:00 (4 years ago)
Author:
bastiK
Message:

applied #12392 - default filename when saving layer (patch by kolesar)

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java

    r9510 r9670  
    156156    }
    157157
    158     private static File checkFileAndConfirmOverWrite(AbstractFileChooser fc, String extension) {
     158    /**
     159     * Checks if selected filename has the given extension. If not, adds the extension and asks for overwrite if filename exists.
     160     *
     161     * @param fc FileChooser where file was already selected
     162     * @return the {@code File} or {@code null} if the user cancelled the dialog.
     163     */
     164    public static File checkFileAndConfirmOverWrite(AbstractFileChooser fc, String extension) {
    159165        if (fc == null) return null;
    160166        File file = fc.getSelectedFile();
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r9668 r9670  
    4545import org.openstreetmap.josm.actions.ExpertToggleAction;
    4646import org.openstreetmap.josm.actions.RenameLayerAction;
    47 import org.openstreetmap.josm.actions.SaveActionBase;
    4847import org.openstreetmap.josm.actions.ToggleUploadDiscouragedLayerAction;
    4948import org.openstreetmap.josm.data.APIDataSet;
     
    9291import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    9392import org.openstreetmap.josm.gui.util.GuiHelper;
     93import org.openstreetmap.josm.gui.widgets.FileChooserManager;
    9494import org.openstreetmap.josm.gui.widgets.JosmTextArea;
     95import org.openstreetmap.josm.io.OsmImporter;
    9596import org.openstreetmap.josm.tools.CheckParameterUtil;
    9697import org.openstreetmap.josm.tools.FilteredCollection;
     
    10021003    public File createAndOpenSaveFileChooser() {
    10031004        String extension = Main.pref.get("save.extension.osm", "osm");
    1004         return SaveActionBase.createAndOpenSaveFileChooser(tr("Save OSM file"), extension);
     1005        File file = getAssociatedFile();
     1006        if (file == null && isRenamed()) {
     1007            String filename = Main.pref.get("lastDirectory") + '/' + getName();
     1008            if (!OsmImporter.FILE_FILTER.acceptName(filename)) filename = filename + '.' + extension;
     1009            file = new File(filename);
     1010        }
     1011        return new FileChooserManager()
     1012            .title(tr("Save OSM file"))
     1013            .extension(extension)
     1014            .file(file)
     1015            .getFileForSave();
    10051016    }
    10061017
  • trunk/src/org/openstreetmap/josm/gui/widgets/FileChooserManager.java

    r8540 r9670  
    3838    private final String curDir;
    3939
     40    private boolean multiple = false;
     41    private String title = null;
     42    private Collection<? extends FileFilter> filters;
     43    private FileFilter defaultFilter;
     44    private int selectionMode = JFileChooser.FILES_ONLY;
     45    private String extension = null;
     46    private boolean allTypes = false;
     47    private File file = null;
     48
    4049    private AbstractFileChooser fc;
     50
     51    /**
     52     * Creates a new {@code FileChooserManager} with default values.
     53     * @see #createFileChooser
     54     */
     55    public FileChooserManager() {
     56        this(false, null, null);
     57    }
    4158
    4259    /**
     
    102119     */
    103120    public final FileChooserManager createFileChooser() {
    104         return doCreateFileChooser(false, null, null, null, null, JFileChooser.FILES_ONLY, false);
     121        return doCreateFileChooser();
    105122    }
    106123
     
    119136     */
    120137    public final FileChooserManager createFileChooser(boolean multiple, String title, FileFilter filter, int selectionMode) {
    121         doCreateFileChooser(multiple, title, Collections.singleton(filter), filter, null, selectionMode, false);
    122         getFileChooser().setAcceptAllFileFilterUsed(false);
     138        multiple(multiple);
     139        title(title);
     140        filters(Collections.singleton(filter));
     141        defaultFilter(filter);
     142        selectionMode(selectionMode);
     143
     144        doCreateFileChooser();
     145        fc.setAcceptAllFileFilterUsed(false);
    123146        return this;
    124147    }
     
    140163    public final FileChooserManager createFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters,
    141164            FileFilter defaultFilter, int selectionMode) {
    142         return doCreateFileChooser(multiple, title, filters, defaultFilter, null, selectionMode, false);
     165        multiple(multiple);
     166        title(title);
     167        filters(filters);
     168        defaultFilter(defaultFilter);
     169        selectionMode(selectionMode);
     170        return doCreateFileChooser();
    143171    }
    144172
     
    159187     */
    160188    public final FileChooserManager createFileChooser(boolean multiple, String title, String extension, boolean allTypes, int selectionMode) {
    161         return doCreateFileChooser(multiple, title, null, null, extension, selectionMode, allTypes);
    162     }
    163 
    164     private FileChooserManager doCreateFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters,
    165             FileFilter defaultFilter, String extension, int selectionMode, boolean allTypes) {
     189        multiple(multiple);
     190        title(title);
     191        extension(extension);
     192        allTypes(allTypes);
     193        selectionMode(selectionMode);
     194        return doCreateFileChooser();
     195    }
     196
     197    /**
     198     * Builder method to set {@code multiple} property.
     199     * @param value If true, makes the dialog allow multiple file selections
     200     * @return this
     201     */
     202    public FileChooserManager multiple(boolean value) {
     203        multiple = value;
     204        return this;
     205    }
     206
     207    /**
     208     * Builder method to set {@code title} property.
     209     * @param value The string that goes in the dialog window's title bar
     210     * @return this
     211     */
     212     public FileChooserManager title(String value) {
     213        title = value;
     214        return this;
     215    }
     216
     217    /**
     218     * Builder method to set {@code filters} property.
     219     * @param value The file filters that will be proposed by the dialog
     220     * @return this
     221     */
     222    public FileChooserManager filters(Collection<? extends FileFilter> value) {
     223        filters = value;
     224        return this;
     225    }
     226
     227    /**
     228     * Builder method to set {@code defaultFilter} property.
     229     * @param value The file filter that will be selected by default
     230     * @return this
     231     */
     232    public FileChooserManager defaultFilter(FileFilter value) {
     233        defaultFilter = value;
     234        return this;
     235    }
     236
     237    /**
     238     * Builder method to set {@code selectionMode} property.
     239     * @param value The selection mode that allows the user to:<br><ul>
     240     *                      <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
     241     *                      <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
     242     *                      <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
     243     * @return this
     244     */
     245    public FileChooserManager selectionMode(int value) {
     246        selectionMode = value;
     247        return this;
     248    }
     249
     250    /**
     251     * Builder method to set {@code extension} property.
     252     * @param value The file extension that will be selected as the default file filter
     253     * @return this
     254     */
     255    public FileChooserManager extension(String value) {
     256        extension = value;
     257        return this;
     258    }
     259
     260    /**
     261     * Builder method to set {@code allTypes} property.
     262     * @param value If true, all the files types known by JOSM will be proposed in the "file type" combobox.
     263     *              If false, only the file filters that include {@code extension} will be proposed
     264     * @return this
     265     */
     266    public FileChooserManager allTypes(boolean value) {
     267        allTypes = value;
     268        return this;
     269    }
     270
     271    /**
     272     * Builder method to set {@code file} property.
     273     * @param value {@link File} object with default filename
     274     * @return this
     275     */
     276    public FileChooserManager file(File value) {
     277        file = value;
     278        return this;
     279    }
     280
     281    /**
     282     * Builds {@code FileChooserManager} object using properties set by builder methods or default values.
     283     * @return this
     284     */
     285    public FileChooserManager doCreateFileChooser() {
    166286        File file = new File(curDir);
    167287        // Use native dialog is preference is set, unless an unsupported selection mode is specifically wanted
     
    179299        fc.setMultiSelectionEnabled(multiple);
    180300        fc.setAcceptAllFileFilterUsed(false);
     301        fc.setSelectedFile(this.file);
    181302
    182303        if (filters != null) {
     
    196317
    197318    /**
    198      * Opens the {@code AbstractFileChooser} that has been created. Nothing happens if it has not been created yet.
     319     * Opens the {@code AbstractFileChooser} that has been created.
    199320     * @return the {@code AbstractFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog.
    200321     */
     
    205326    /**
    206327     * Opens the {@code AbstractFileChooser} that has been created and waits for the user to choose a file/directory, or cancel the dialog.<br>
    207      * Nothing happens if the dialog has not been created yet.<br>
    208328     * When the user choses a file or directory, the {@code lastDirProperty} is updated to the chosen directory path.
    209329     *
     
    212332     */
    213333    public AbstractFileChooser openFileChooser(Component parent) {
    214         if (fc != null) {
    215             if (parent == null) {
    216                 parent = Main.parent;
    217             }
    218 
    219             int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent);
    220             if (answer != JFileChooser.APPROVE_OPTION) {
     334        if (fc == null) doCreateFileChooser();
     335
     336        if (parent == null) {
     337            parent = Main.parent;
     338        }
     339
     340        int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent);
     341        if (answer != JFileChooser.APPROVE_OPTION) {
     342            return null;
     343        }
     344
     345        if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
     346            Main.pref.put(lastDirProperty, fc.getCurrentDirectory().getAbsolutePath());
     347        }
     348
     349        if (!open) {
     350            File file = fc.getSelectedFile();
     351            if (!SaveActionBase.confirmOverwrite(file)) {
    221352                return null;
    222353            }
    223 
    224             if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
    225                 Main.pref.put(lastDirProperty, fc.getCurrentDirectory().getAbsolutePath());
    226             }
    227 
    228             if (!open) {
    229                 File file = fc.getSelectedFile();
    230                 if (!SaveActionBase.confirmOverwrite(file)) {
    231                     return null;
    232                 }
    233             }
    234354        }
    235355        return fc;
    236356    }
     357
     358    /**
     359     * Opens the file chooser dialog, then checks if filename has the given extension. If not, adds the extension and asks for overwrite if filename exists.
     360     *
     361     * @return the {@code File} or {@code null} if the user cancelled the dialog.
     362     */
     363    public File getFileForSave() {
     364        return SaveActionBase.checkFileAndConfirmOverWrite(openFileChooser(), extension);
     365    }
    237366}
Note: See TracChangeset for help on using the changeset viewer.