1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 |
|
---|
6 | import javax.swing.JFileChooser;
|
---|
7 | import javax.swing.filechooser.FileFilter;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
|
---|
10 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Helper class for all actions that access the disk.
|
---|
14 | * @since 78
|
---|
15 | */
|
---|
16 | public abstract class DiskAccessAction extends JosmAction {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Constructs a new {@code DiskAccessAction}.
|
---|
20 | *
|
---|
21 | * @param name The action's text as displayed on the menu (if it is added to a menu)
|
---|
22 | * @param iconName The filename of the icon to use
|
---|
23 | * @param tooltip A longer description of the action that will be displayed in the tooltip
|
---|
24 | * @param shortcut A ready-created shortcut object or {@code null} if you don't want a shortcut
|
---|
25 | * @since 1084
|
---|
26 | */
|
---|
27 | public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut) {
|
---|
28 | super(name, iconName, tooltip, shortcut, true);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructs a new {@code DiskAccessAction}.
|
---|
33 | *
|
---|
34 | * @param name The action's text as displayed on the menu (if it is added to a menu)
|
---|
35 | * @param iconName The filename of the icon to use
|
---|
36 | * @param tooltip A longer description of the action that will be displayed in the tooltip
|
---|
37 | * @param shortcut A ready-created shortcut object or null if you don't want a shortcut
|
---|
38 | * @param register Register this action for the toolbar preferences?
|
---|
39 | * @param toolbarId Identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
40 | * @param installAdapters False, if you don't want to install layer changed and selection changed adapters
|
---|
41 | * @since 5438
|
---|
42 | */
|
---|
43 | public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
|
---|
44 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Creates a new {@link JFileChooser} and makes it visible.
|
---|
49 | * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
|
---|
50 | * @param multiple If true, makes the dialog allow multiple file selections
|
---|
51 | * @param title The string that goes in the dialog window's title bar
|
---|
52 | * @return The {@code JFileChooser}.
|
---|
53 | * @since 1646
|
---|
54 | */
|
---|
55 | public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {
|
---|
56 | return createAndOpenFileChooser(open, multiple, title, null);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Creates a new {@link JFileChooser} and makes it visible.
|
---|
61 | * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
|
---|
62 | * @param multiple If true, makes the dialog allow multiple file selections
|
---|
63 | * @param title The string that goes in the dialog window's title bar
|
---|
64 | * @param extension The file extension that will be selected as the default file filter
|
---|
65 | * @return The {@code JFileChooser}.
|
---|
66 | * @since 2020
|
---|
67 | */
|
---|
68 | public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) {
|
---|
69 | return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Creates a new {@link JFileChooser} and makes it visible.
|
---|
74 | * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
|
---|
75 | * @param multiple If true, makes the dialog allow multiple file selections
|
---|
76 | * @param title The string that goes in the dialog window's title bar
|
---|
77 | * @param extension The file extension that will be selected as the default file filter
|
---|
78 | * @param selectionMode The selection mode that allows the user to:<br><ul>
|
---|
79 | * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
|
---|
80 | * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
|
---|
81 | * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
|
---|
82 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
---|
83 | * If false, only the file filters that include {@code extension} will be proposed
|
---|
84 | * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory.
|
---|
85 | * This property will then be updated to the new "last directory" chosen by the user. If null, the default property "lastDirectory" will be used.
|
---|
86 | * @return The {@code JFileChooser}.
|
---|
87 | * @since 5438
|
---|
88 | */
|
---|
89 | public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension, int selectionMode, boolean allTypes, String lastDirProperty) {
|
---|
90 | return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, extension, allTypes, selectionMode).openFileChooser();
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Creates a new {@link JFileChooser} for a single {@link FileFilter} and makes it visible.
|
---|
95 | * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
|
---|
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 | * @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><ul>
|
---|
119 | * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
|
---|
120 | * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
|
---|
121 | * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
|
---|
122 | * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the 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 | }
|
---|