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