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

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

enable PMD rule OptimizableToArrayCall

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