source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/NativeFileChooser.java@ 14138

Last change on this file since 14138 was 14138, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.platform and related methods - new class PlatformManager

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Component;
5import java.awt.FileDialog;
6import java.awt.Frame;
7import java.io.File;
8import java.io.FilenameFilter;
9import java.util.ArrayList;
10import java.util.List;
11
12import javax.swing.JFileChooser;
13import javax.swing.filechooser.FileFilter;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.tools.PlatformManager;
17import org.openstreetmap.josm.tools.Utils;
18
19/**
20 * File chooser based on the AWT's {@link FileDialog} implementation,
21 * which looks like more a native file chooser than the Swing implementation.
22 * @since 7578
23 */
24public class NativeFileChooser extends AbstractFileChooser {
25
26 /** The instance of the fileDialog */
27 private final FileDialog fileDialog;
28 private FileFilter fileFilter;
29 private final List<FileFilter> fileFilters = new ArrayList<>();
30 private int selectionMode;
31
32 /**
33 * Constructs a new {@code NativeFileChooser}.
34 * @param file the current file/directory to point to
35 */
36 public NativeFileChooser(File file) {
37 fileDialog = new FileDialog((Frame) Main.parent);
38 if (file != null) {
39 fileDialog.setDirectory(file.getAbsolutePath());
40 if (file.isFile()) {
41 fileDialog.setFile(file.toString());
42 }
43 }
44 }
45
46 @Override
47 public void addChoosableFileFilter(FileFilter filter) {
48 // TODO implement this after Oracle fixes JDK-4811090 / JDK-6192906
49 // https://bugs.openjdk.java.net/browse/JDK-4811090 : Extend awt filedialog
50 // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
51 fileFilters.add(filter);
52 }
53
54 @Override
55 public FileFilter[] getChoosableFileFilters() {
56 // TODO implement this after Oracle fixes JDK-4811090 / JDK-6192906
57 // https://bugs.openjdk.java.net/browse/JDK-4811090 : Extend awt filedialog
58 // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
59 return fileFilters.toArray(new FileFilter[0]);
60 }
61
62 @Override
63 public File getCurrentDirectory() {
64 return new File(fileDialog.getDirectory());
65 }
66
67 @Override
68 public FileFilter getFileFilter() {
69 return fileFilter;
70 }
71
72 @Override
73 public File getSelectedFile() {
74 return new File(fileDialog.getDirectory() + fileDialog.getFile());
75 }
76
77 @Override
78 public File[] getSelectedFiles() {
79 return fileDialog.getFiles();
80 }
81
82 @Override
83 public boolean isMultiSelectionEnabled() {
84 return fileDialog.isMultipleMode();
85 }
86
87 @Override
88 public void setAcceptAllFileFilterUsed(boolean b) {
89 // TODO implement this after Oracle fixes JDK-4811090 / JDK-6192906
90 // https://bugs.openjdk.java.net/browse/JDK-4811090 : Extend awt filedialog
91 // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
92 }
93
94 @Override
95 public void setCurrentDirectory(File f) {
96 fileDialog.setDirectory(f.toString());
97 }
98
99 @Override
100 public void setDialogTitle(String title) {
101 fileDialog.setTitle(title);
102 }
103
104 @Override
105 public void setFileFilter(final FileFilter cff) {
106 FilenameFilter filter = (directory, fileName) -> cff.accept(new File(directory.getAbsolutePath() + fileName));
107 fileDialog.setFilenameFilter(filter);
108 fileFilter = cff;
109 }
110
111 @Override
112 public void setFileSelectionMode(int selectionMode) {
113 // CHECKSTYLE.OFF: LineLength
114 // TODO implement this after Oracle fixes JDK-6192906 / JDK-6699863 / JDK-6927978 / JDK-7125172:
115 // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
116 // https://bugs.openjdk.java.net/browse/JDK-6699863 : awt filedialog cannot select directories
117 // https://bugs.openjdk.java.net/browse/JDK-6927978 : Directory Selection standard dialog support
118 // https://bugs.openjdk.java.net/browse/JDK-7125172 : FileDialog objects don't allow directory AND files selection simultaneously
119
120 // There is however a basic support for directory selection on OS X, with Java >= 7u40:
121 // http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x/1224744#1224744
122 // https://bugs.openjdk.java.net/browse/JDK-7161437 : [macosx] awt.FileDialog doesn't respond appropriately for mac when selecting folders
123 // CHECKSTYLE.ON: LineLength
124 this.selectionMode = selectionMode;
125 }
126
127 @Override
128 public void setMultiSelectionEnabled(boolean multiple) {
129 fileDialog.setMultipleMode(multiple);
130 }
131
132 @Override
133 public void setSelectedFile(File file) {
134 if (file == null) return;
135 fileDialog.setDirectory(file.getParent());
136 fileDialog.setFile(file.getName());
137 }
138
139 @Override
140 public int showOpenDialog(Component parent) {
141 boolean appleProperty = PlatformManager.isPlatformOsx() && selectionMode == JFileChooser.DIRECTORIES_ONLY;
142 if (appleProperty) {
143 Utils.updateSystemProperty("apple.awt.fileDialogForDirectories", "true");
144 }
145 try {
146 fileDialog.setLocale(locale);
147 fileDialog.setMode(FileDialog.LOAD);
148 fileDialog.setVisible(true);
149 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
150 } finally {
151 if (appleProperty) {
152 Utils.updateSystemProperty("apple.awt.fileDialogForDirectories", "false");
153 }
154 }
155 }
156
157 @Override
158 public int showSaveDialog(Component parent) {
159 fileDialog.setLocale(locale);
160 fileDialog.setMode(FileDialog.SAVE);
161 fileDialog.setVisible(true);
162 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
163 }
164
165 /**
166 * Determines if the selection mode is suuported by the native file chooser.
167 * @param selectionMode the selection mode
168 * @return {@code true} if the selection mode is supported, {@code false} otherwise
169 */
170 public static boolean supportsSelectionMode(int selectionMode) {
171 switch (selectionMode) {
172 case JFileChooser.FILES_AND_DIRECTORIES:
173 // CHECKSTYLE.OFF: LineLength
174 // https://bugs.openjdk.java.net/browse/JDK-7125172 : FileDialog objects don't allow directory AND files selection simultaneously
175 return false;
176 case JFileChooser.DIRECTORIES_ONLY:
177 // http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x/1224744#1224744
178 // CHECKSTYLE.ON: LineLength
179 return PlatformManager.isPlatformOsx();
180 case JFileChooser.FILES_ONLY:
181 default:
182 return true;
183 }
184 }
185}
Note: See TracBrowser for help on using the repository browser.