Skip to content

Commit 7d37bb8

Browse files
committed
Use native file dialogs in macOS
This is a Java port of Jianhua Feng's earlier Groovy-PR (java-decompiler#180)
1 parent b3c1ced commit 7d37bb8

File tree

4 files changed

+145
-7
lines changed

4 files changed

+145
-7
lines changed

app/src/main/java/org/jd/gui/controller/MainController.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.swing.*;
3636
import javax.swing.filechooser.FileNameExtensionFilter;
3737
import javax.swing.filechooser.FileSystemView;
38+
import org.jd.gui.view.component.FileChooser;
3839
import java.awt.*;
3940
import java.awt.datatransfer.DataFlavor;
4041
import java.awt.datatransfer.Transferable;
@@ -155,7 +156,7 @@ public void show(List<File> files) {
155156
// Set drop files transfer handler
156157
mainFrame.setTransferHandler(new FilesTransferHandler());
157158
// Background class loading
158-
new JFileChooser().addChoosableFileFilter(new FileNameExtensionFilter("", "dummy"));
159+
new FileChooser().addChoosableFileFilter(new FileNameExtensionFilter("", "dummy"));
159160
FileSystemView.getFileSystemView().isFileSystemRoot(new File("dummy"));
160161
new JLayer();
161162
});
@@ -183,7 +184,7 @@ protected void onOpen() {
183184

184185
String description = sb.toString();
185186
String[] array = extensions.toArray(new String[0]);
186-
JFileChooser chooser = new JFileChooser();
187+
FileChooser chooser = new FileChooser();
187188

188189
chooser.removeChoosableFileFilter(chooser.getFileFilter());
189190
chooser.addChoosableFileFilter(new FileNameExtensionFilter("All files (" + description + ")", array));
@@ -195,7 +196,7 @@ protected void onOpen() {
195196

196197
chooser.setCurrentDirectory(configuration.getRecentLoadDirectory());
197198

198-
if (chooser.showOpenDialog(mainView.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
199+
if (chooser.showOpenDialog(mainView.getMainFrame()) == FileChooser.APPROVE_OPTION) {
199200
configuration.setRecentLoadDirectory(chooser.getCurrentDirectory());
200201
openFile(chooser.getSelectedFile());
201202
}
@@ -207,12 +208,12 @@ protected void onClose() {
207208

208209
protected void onSaveSource() {
209210
if (currentPage instanceof ContentSavable) {
210-
JFileChooser chooser = new JFileChooser();
211+
FileChooser chooser = new FileChooser();
211212
JFrame mainFrame = mainView.getMainFrame();
212213

213214
chooser.setSelectedFile(new File(configuration.getRecentSaveDirectory(), ((ContentSavable)currentPage).getFileName()));
214215

215-
if (chooser.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
216+
if (chooser.showSaveDialog(mainFrame) == FileChooser.APPROVE_OPTION) {
216217
File selectedFile = chooser.getSelectedFile();
217218

218219
configuration.setRecentSaveDirectory(chooser.getCurrentDirectory());
@@ -245,12 +246,12 @@ protected void onSaveAllSources() {
245246

246247
if (currentPanel instanceof SourcesSavable) {
247248
SourcesSavable sourcesSavable = (SourcesSavable)currentPanel;
248-
JFileChooser chooser = new JFileChooser();
249+
FileChooser chooser = new FileChooser();
249250
JFrame mainFrame = mainView.getMainFrame();
250251

251252
chooser.setSelectedFile(new File(configuration.getRecentSaveDirectory(), sourcesSavable.getSourceFileName()));
252253

253-
if (chooser.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
254+
if (chooser.showSaveDialog(mainFrame) == FileChooser.APPROVE_OPTION) {
254255
File selectedFile = chooser.getSelectedFile();
255256

256257
configuration.setRecentSaveDirectory(chooser.getCurrentDirectory());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.jd.gui.util.io;
2+
3+
import java.io.File;
4+
5+
6+
/**
7+
* Created by jianhua.fengjh on 27/11/2015.
8+
*/
9+
public class FileUtils {
10+
11+
public static String ensureTrailingSlash(final String path) {
12+
if ((path == null) || "".equals(path)) {
13+
return "";
14+
}
15+
16+
StringBuilder buf = new StringBuilder(path);
17+
while (buf.charAt(buf.length() - 1) == File.separatorChar) {
18+
buf.deleteCharAt(buf.length() - 1);
19+
}
20+
21+
return buf.append(File.separatorChar).toString();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.jd.gui.util.sys;
2+
3+
/**
4+
* Created by jianhua.fengjh on 27/11/2015.
5+
*/
6+
public final class SystemUtils {
7+
8+
static boolean isLinux() {
9+
return System.getProperty("os.name").startsWith("Linux");
10+
}
11+
12+
public static boolean isMacOS() {
13+
return System.getProperty("os.name").startsWith("Mac");
14+
}
15+
16+
public static boolean isWindows() {
17+
return System.getProperty("os.name").startsWith("Windows");
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.jd.gui.view.component;
2+
3+
import org.jd.gui.util.io.FileUtils;
4+
import org.jd.gui.util.sys.SystemUtils;
5+
6+
import javax.swing.*;
7+
import java.awt.*;
8+
import java.io.File;
9+
import java.io.FilenameFilter;
10+
11+
/**
12+
* Created by jianhua.fengjh on 27/11/2015.
13+
*/
14+
public class FileChooser extends JFileChooser {
15+
16+
/**
17+
*
18+
*/
19+
private static final long serialVersionUID = 1L;
20+
21+
public int showOpenDialog(Component parent) {
22+
if (!SystemUtils.isMacOS()) {
23+
return super.showOpenDialog(parent);
24+
} else {
25+
setDialogType(JFileChooser.OPEN_DIALOG);
26+
return showNativeFileDialog(this);
27+
}
28+
}
29+
30+
public int showSaveDialog(Component parent) {
31+
32+
if (!SystemUtils.isMacOS()) {
33+
return super.showSaveDialog(parent);
34+
} else {
35+
setDialogType(JFileChooser.SAVE_DIALOG);
36+
return showNativeFileDialog(this);
37+
}
38+
}
39+
40+
private static int showNativeFileDialog(final JFileChooser chooser) {
41+
if (chooser != null) {
42+
43+
FileDialog fileDialog = new FileDialog((Frame) chooser.getParent());
44+
fileDialog.setDirectory(chooser.getCurrentDirectory().getPath());
45+
File file = chooser.getSelectedFile();
46+
47+
if (chooser.getDialogType() == JFileChooser.SAVE_DIALOG) {
48+
fileDialog.setFile(file != null ? file.getName() : ""); //save only need name
49+
} else {
50+
fileDialog.setFile(file != null ? file.getPath() : "");
51+
}
52+
53+
fileDialog.setFilenameFilter(new FilenameFilter() {
54+
55+
public boolean accept(File dir, String name) {
56+
String path = dir.getPath();
57+
String pathSeparator = File.pathSeparator;
58+
return chooser.getFileFilter().accept(new File(0 + path.length() + pathSeparator.length() + name.length() + path + pathSeparator + name));
59+
}
60+
61+
});
62+
63+
if (chooser.getDialogType() == JFileChooser.SAVE_DIALOG) {
64+
fileDialog.setMode(FileDialog.SAVE);
65+
} else {
66+
fileDialog.setMode(FileDialog.LOAD);
67+
}
68+
69+
if (chooser.getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) {
70+
System.setProperty("apple.awt.fileDialogForDirectories", "true");
71+
} else {
72+
System.setProperty("apple.awt.fileDialogForDirectories", "false");
73+
}
74+
75+
fileDialog.setVisible(true);
76+
77+
//reset fileDialogForDirectories property
78+
System.setProperty("apple.awt.fileDialogForDirectories", "false");
79+
if (fileDialog.getFile() == null) {
80+
return JFileChooser.CANCEL_OPTION;
81+
}
82+
83+
String dir = fileDialog.getDirectory();
84+
String trailingSlash = FileUtils.ensureTrailingSlash(dir);
85+
String strFile = fileDialog.getFile();
86+
chooser.setSelectedFile(new File(strFile.length() != 0 ? trailingSlash.concat(strFile) : trailingSlash));
87+
88+
return JFileChooser.APPROVE_OPTION;
89+
}
90+
91+
return JFileChooser.ERROR_OPTION;
92+
}
93+
94+
}

0 commit comments

Comments
 (0)