1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.FileDialog;
|
---|
6 | import java.awt.Frame;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FilenameFilter;
|
---|
9 |
|
---|
10 | import javax.swing.JFileChooser;
|
---|
11 | import javax.swing.filechooser.FileFilter;
|
---|
12 |
|
---|
13 | import 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 | */
|
---|
20 | public 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 | if (file == null) return;
|
---|
133 | fileDialog.setDirectory(file.getParent());
|
---|
134 | fileDialog.setFile(file.getName());
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public int showOpenDialog(Component parent) {
|
---|
139 | boolean appleProperty = Main.isPlatformOsx() && selectionMode == JFileChooser.DIRECTORIES_ONLY;
|
---|
140 | if (appleProperty) {
|
---|
141 | System.setProperty("apple.awt.fileDialogForDirectories", "true");
|
---|
142 | }
|
---|
143 | try {
|
---|
144 | fileDialog.setLocale(locale);
|
---|
145 | fileDialog.setMode(FileDialog.LOAD);
|
---|
146 | fileDialog.setVisible(true);
|
---|
147 | return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
|
---|
148 | } finally {
|
---|
149 | if (appleProperty) {
|
---|
150 | System.setProperty("apple.awt.fileDialogForDirectories", "false");
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public int showSaveDialog(Component parent) {
|
---|
157 | fileDialog.setLocale(locale);
|
---|
158 | fileDialog.setMode(FileDialog.SAVE);
|
---|
159 | fileDialog.setVisible(true);
|
---|
160 | return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Determines if the selection mode is suuported by the native file chooser.
|
---|
165 | * @param selectionMode the selection mode
|
---|
166 | * @return {@code true} if the selection mode is supported, {@code false} otherwise
|
---|
167 | */
|
---|
168 | public static boolean supportsSelectionMode(int selectionMode) {
|
---|
169 | switch (selectionMode) {
|
---|
170 | case JFileChooser.FILES_AND_DIRECTORIES:
|
---|
171 | // CHECKSTYLE.OFF: LineLength
|
---|
172 | // https://bugs.openjdk.java.net/browse/JDK-7125172 : FileDialog objects don't allow directory AND files selection simultaneously
|
---|
173 | return false;
|
---|
174 | case JFileChooser.DIRECTORIES_ONLY:
|
---|
175 | // http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x/1224744#1224744
|
---|
176 | // CHECKSTYLE.ON: LineLength
|
---|
177 | return Main.isPlatformOsx();
|
---|
178 | case JFileChooser.FILES_ONLY:
|
---|
179 | default:
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|