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

Last change on this file since 9257 was 9257, checked in by simon04, 9 years ago

NativeFileChooser: fix setting previous directory

With file=/tmp on Linux, the file chooser showed the / directory and had tmp entered in the filename.

  • Property svn:eol-style set to native
File size: 6.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.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 = new FilenameFilter() {
101 public boolean accept(File Directory, String fileName) {
102 return cff.accept(new File(Directory.getAbsolutePath() + fileName));
103 }
104 };
105 fileDialog.setFilenameFilter(filter);
106 fileFilter = cff;
107 }
108
109 @Override
110 public void setFileSelectionMode(int selectionMode) {
111 // CHECKSTYLE.OFF: LineLength
112 // TODO implement this after Oracle fixes JDK-6192906 / JDK-6699863 / JDK-6927978 / JDK-7125172:
113 // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
114 // https://bugs.openjdk.java.net/browse/JDK-6699863 : awt filedialog cannot select directories
115 // https://bugs.openjdk.java.net/browse/JDK-6927978 : Directory Selection standard dialog support
116 // https://bugs.openjdk.java.net/browse/JDK-7125172 : FileDialog objects don't allow directory AND files selection simultaneously
117
118 // There is however a basic support for directory selection on OS X, with Java >= 7u40:
119 // http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x/1224744#1224744
120 // https://bugs.openjdk.java.net/browse/JDK-7161437 : [macosx] awt.FileDialog doesn't respond appropriately for mac when selecting folders
121 // CHECKSTYLE.ON: LineLength
122 this.selectionMode = selectionMode;
123 }
124
125 @Override
126 public void setMultiSelectionEnabled(boolean multiple) {
127 fileDialog.setMultipleMode(multiple);
128 }
129
130 @Override
131 public void setSelectedFile(File file) {
132 fileDialog.setDirectory(file.getAbsolutePath());
133 fileDialog.setFile(file.getName());
134 }
135
136 @Override
137 public int showOpenDialog(Component parent) {
138 boolean appleProperty = Main.isPlatformOsx() && selectionMode == JFileChooser.DIRECTORIES_ONLY;
139 if (appleProperty) {
140 System.setProperty("apple.awt.fileDialogForDirectories", "true");
141 }
142 try {
143 fileDialog.setLocale(locale);
144 fileDialog.setMode(FileDialog.LOAD);
145 fileDialog.setVisible(true);
146 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
147 } finally {
148 if (appleProperty) {
149 System.setProperty("apple.awt.fileDialogForDirectories", "false");
150 }
151 }
152 }
153
154 @Override
155 public int showSaveDialog(Component parent) {
156 fileDialog.setLocale(locale);
157 fileDialog.setMode(FileDialog.SAVE);
158 fileDialog.setVisible(true);
159 return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
160 }
161
162 /**
163 * Determines if the selection mode is suuported by the native file chooser.
164 * @param selectionMode the selection mode
165 * @return {@code true} if the selection mode is supported, {@code false} otherwise
166 */
167 public static boolean supportsSelectionMode(int selectionMode) {
168 switch (selectionMode) {
169 case JFileChooser.FILES_AND_DIRECTORIES:
170 // CHECKSTYLE.OFF: LineLength
171 // https://bugs.openjdk.java.net/browse/JDK-7125172 : FileDialog objects don't allow directory AND files selection simultaneously
172 return false;
173 case JFileChooser.DIRECTORIES_ONLY:
174 // http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x/1224744#1224744
175 // CHECKSTYLE.ON: LineLength
176 return Main.isPlatformOsx();
177 case JFileChooser.FILES_ONLY:
178 default:
179 return true;
180 }
181 }
182}
Note: See TracBrowser for help on using the repository browser.