source: josm/trunk/src/org/openstreetmap/josm/actions/DiskAccessAction.java@ 8345

Last change on this file since 8345 was 7578, checked in by Don-vip, 10 years ago

fix #10024 - Add an option in Preferences/Look-and-Feel to use native file-choosing dialogs.
They look nicer but they do not support file filters, so we cannot use them (yet) as default.
Based on patch by Lesath and code review by simon04.
The native dialogs are not used if selection mode is not supported ("files and directories" on all platforms, "directories" on systems other than OS X)

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