source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/AbstractFileChooser.java@ 7578

Last change on this file since 7578 was 7578, checked in by Don-vip, 11 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)

File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Component;
5import java.awt.HeadlessException;
6import java.io.File;
7import java.util.Locale;
8
9import javax.swing.filechooser.FileFilter;
10
11/**
12 * Abstract class to allow different file chooser implementations.
13 * @since 7578
14 */
15public abstract class AbstractFileChooser {
16
17 /** The locale for both implementations */
18 protected static Locale locale;
19
20 /**
21 * Sets the default locale for all implementations.
22 * @param l locale
23 */
24 public static void setDefaultLocale(Locale l) {
25 locale = l;
26 }
27
28 /**
29 * Adds a filter to the list of user choosable file filters.
30 * For information on setting the file selection mode, see
31 * {@link #setFileSelectionMode setFileSelectionMode}.
32 *
33 * @param filter the <code>FileFilter</code> to add to the choosable file
34 * filter list
35 *
36 * @see #getChoosableFileFilters
37 * @see #setFileSelectionMode
38 */
39 public abstract void addChoosableFileFilter(FileFilter filter);
40
41 /**
42 * Gets the list of user choosable file filters.
43 *
44 * @return a <code>FileFilter</code> array containing all the choosable
45 * file filters
46 *
47 * @see #addChoosableFileFilter
48 */
49 public abstract FileFilter[] getChoosableFileFilters();
50
51 /**
52 * Returns the current directory.
53 *
54 * @return the current directory
55 * @see #setCurrentDirectory
56 */
57 public abstract File getCurrentDirectory();
58
59 /**
60 * Returns the currently selected file filter.
61 *
62 * @return the current file filter
63 * @see #setFileFilter
64 * @see #addChoosableFileFilter
65 */
66 public abstract FileFilter getFileFilter();
67
68 /**
69 * Returns the selected file. This can be set either by the
70 * programmer via <code>setSelectedFile</code> or by a user action, such as
71 * either typing the filename into the UI or selecting the
72 * file from a list in the UI.
73 *
74 * @see #setSelectedFile
75 * @return the selected file
76 */
77 public abstract File getSelectedFile();
78
79 /**
80 * Returns a list of selected files if the file chooser is
81 * set to allow multiple selection.
82 * @return a list of selected files if the file chooser is
83 * set to allow multiple selection, or an empty array otherwise.
84 */
85 public abstract File[] getSelectedFiles();
86
87 /**
88 * Returns true if multiple files can be selected.
89 * @return true if multiple files can be selected
90 * @see #setMultiSelectionEnabled
91 */
92 public abstract boolean isMultiSelectionEnabled();
93
94 /**
95 * Determines whether the <code>AcceptAll FileFilter</code> is used
96 * as an available choice in the choosable filter list.
97 * If false, the <code>AcceptAll</code> file filter is removed from
98 * the list of available file filters.
99 * If true, the <code>AcceptAll</code> file filter will become the
100 * the actively used file filter.
101 * @param b whether the <code>AcceptAll FileFilter</code> is used
102 * as an available choice in the choosable filter list
103 *
104 * @see #setFileFilter
105 */
106 public abstract void setAcceptAllFileFilterUsed(boolean b);
107
108 /**
109 * Sets the current directory. Passing in <code>null</code> sets the
110 * file chooser to point to the user's default directory.
111 * This default depends on the operating system. It is
112 * typically the "My Documents" folder on Windows, and the user's
113 * home directory on Unix.
114 *
115 * If the file passed in as <code>currentDirectory</code> is not a
116 * directory, the parent of the file will be used as the currentDirectory.
117 * If the parent is not traversable, then it will walk up the parent tree
118 * until it finds a traversable directory, or hits the root of the
119 * file system.
120 *
121 * @param dir the current directory to point to
122 * @see #getCurrentDirectory
123 */
124 public abstract void setCurrentDirectory(File dir);
125
126 /**
127 * Sets the string that goes in the <code>JFileChooser</code> window's
128 * title bar.
129 *
130 * @param title the new <code>String</code> for the title bar
131 */
132 public abstract void setDialogTitle(String title);
133
134 /**
135 * Sets the current file filter. The file filter is used by the
136 * file chooser to filter out files from the user's view.
137 *
138 * @param filter the new current file filter to use
139 * @see #getFileFilter
140 */
141 public abstract void setFileFilter(final FileFilter filter);
142
143 /**
144 * Sets the <code>JFileChooser</code> to allow the user to just
145 * select files, just select
146 * directories, or select both files and directories. The default is
147 * <code>JFilesChooser.FILES_ONLY</code>.
148 *
149 * @param selectionMode the type of files to be displayed:
150 * <ul>
151 * <li>JFileChooser.FILES_ONLY
152 * <li>JFileChooser.DIRECTORIES_ONLY
153 * <li>JFileChooser.FILES_AND_DIRECTORIES
154 * </ul>
155 *
156 * @exception IllegalArgumentException if <code>mode</code> is an
157 * illegal file selection mode
158 */
159 public abstract void setFileSelectionMode(int selectionMode);
160
161 /**
162 * Sets the file chooser to allow multiple file selections.
163 *
164 * @param multiple true if multiple files may be selected
165 * @beaninfo
166 * bound: true
167 * description: Sets multiple file selection mode.
168 *
169 * @see #isMultiSelectionEnabled
170 */
171 public abstract void setMultiSelectionEnabled(boolean multiple);
172
173 /**
174 * Sets the selected file. If the file's parent directory is
175 * not the current directory, changes the current directory
176 * to be the file's parent directory.
177 *
178 * @see #getSelectedFile
179 *
180 * @param file the selected file
181 */
182 public abstract void setSelectedFile(File file);
183
184 /**
185 * Pops up an "Open File" file chooser dialog. Note that the
186 * text that appears in the approve button is determined by
187 * the L&F.
188 *
189 * @param parent the parent component of the dialog,
190 * can be <code>null</code>;
191 * see <code>showDialog</code> for details
192 * @return the return state of the file chooser on popdown:
193 * <ul>
194 * <li>JFileChooser.CANCEL_OPTION
195 * <li>JFileChooser.APPROVE_OPTION
196 * <li>JFileChooser.ERROR_OPTION if an error occurs or the
197 * dialog is dismissed
198 * </ul>
199 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
200 * returns true.
201 * @see java.awt.GraphicsEnvironment#isHeadless
202 */
203 public abstract int showOpenDialog(Component parent);
204
205 /**
206 * Pops up a "Save File" file chooser dialog. Note that the
207 * text that appears in the approve button is determined by
208 * the L&F.
209 *
210 * @param parent the parent component of the dialog,
211 * can be <code>null</code>;
212 * see <code>showDialog</code> for details
213 * @return the return state of the file chooser on popdown:
214 * <ul>
215 * <li>JFileChooser.CANCEL_OPTION
216 * <li>JFileChooser.APPROVE_OPTION
217 * <li>JFileChooser.ERROR_OPTION if an error occurs or the
218 * dialog is dismissed
219 * </ul>
220 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
221 * returns true.
222 * @see java.awt.GraphicsEnvironment#isHeadless
223 */
224 public abstract int showSaveDialog(Component parent);
225}
Note: See TracBrowser for help on using the repository browser.