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

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

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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