001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.actions;
003    
004    import java.util.Collection;
005    
006    import javax.swing.JFileChooser;
007    import javax.swing.filechooser.FileFilter;
008    
009    import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
010    import org.openstreetmap.josm.tools.Shortcut;
011    
012    /**
013     * Helper class for all actions that access the disk.
014     * @since 78
015     */
016    abstract public class DiskAccessAction extends JosmAction {
017    
018        /**
019         * Constructs a new {@code DiskAccessAction}.
020         * 
021         * @param name The action's text as displayed on the menu (if it is added to a menu)
022         * @param iconName The filename of the icon to use
023         * @param tooltip A longer description of the action that will be displayed in the tooltip
024         * @param shortcut A ready-created shortcut object or {@code null} if you don't want a shortcut
025         * @since 1084
026         */
027        public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut) {
028            super(name, iconName, tooltip, shortcut, true);
029        }
030    
031        /**
032         * Constructs a new {@code DiskAccessAction}.
033         *
034         * @param name The action's text as displayed on the menu (if it is added to a menu)
035         * @param iconName The filename of the icon to use
036         * @param tooltip  A longer description of the action that will be displayed in the tooltip
037         * @param shortcut A ready-created shortcut object or null if you don't want a shortcut
038         * @param register Register this action for the toolbar preferences?
039         * @param toolbarId Identifier for the toolbar preferences. The iconName is used, if this parameter is null
040         * @param installAdapters False, if you don't want to install layer changed and selection changed adapters
041         * @since 5438
042         */
043        public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
044            super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
045        }
046    
047        /**
048         * Creates a new {@link JFileChooser} and makes it visible.
049         * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 
050         * @param multiple If true, makes the dialog allow multiple file selections
051         * @param title The string that goes in the dialog window's title bar
052         * @return The {@code JFileChooser}.
053         * @since 1646
054         */
055        public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {
056            return createAndOpenFileChooser(open, multiple, title, null);
057        }
058    
059        /**
060         * Creates a new {@link JFileChooser} and makes it visible.
061         * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 
062         * @param multiple If true, makes the dialog allow multiple file selections
063         * @param title The string that goes in the dialog window's title bar
064         * @param extension The file extension that will be selected as the default file filter
065         * @return The {@code JFileChooser}.
066         * @since 2020
067         */
068        public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) {
069            return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null);
070        }
071    
072        /**
073         * Creates a new {@link JFileChooser} and makes it visible.
074         * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 
075         * @param multiple If true, makes the dialog allow multiple file selections
076         * @param title The string that goes in the dialog window's title bar
077         * @param extension The file extension that will be selected as the default file filter
078         * @param selectionMode The selection mode that allows the user to:<br/>
079         *                      <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 
080         *                      <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 
081         *                      <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li>
082         * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 
083         *                 If false, only the file filters that include {@code extension} will be proposed
084         * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory. 
085         *        This property will then be updated to the new "last directory" chosen by the user. If null, the default property "lastDirectory" will be used.
086         * @return The {@code JFileChooser}.
087         * @since 5438
088         */
089        public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension, int selectionMode, boolean allTypes, String lastDirProperty) {
090            return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, extension, allTypes, selectionMode).openFileChooser();
091        }
092    
093        /**
094         * Creates a new {@link JFileChooser} for a single {@link FileFilter} and makes it visible.
095         * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 
096         * @param multiple If true, makes the dialog allow multiple file selections
097         * @param title The string that goes in the dialog window's title bar
098         * @param filter The only file filter that will be proposed by the dialog
099         * @param selectionMode The selection mode that allows the user to:<br/>
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>
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}.
105         * @since 5438
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();
109        }
110    
111        /**
112         * Creates a new {@link JFileChooser} for several {@link FileFilter}s and makes it visible.
113         * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 
114         * @param multiple If true, makes the dialog allow multiple file selections
115         * @param title The string that goes in the dialog window's title bar
116         * @param filters The file filters that will be proposed by the dialog
117         * @param defaultFilter The file filter that will be selected by default
118         * @param selectionMode The selection mode that allows the user to:<br/>
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>
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 user
123         * @return The {@code JFileChooser}.
124         * @since 5438
125         */
126        public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, 
127                Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode, String lastDirProperty) {
128            return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filters, defaultFilter, selectionMode).openFileChooser();
129        }
130    }