Skip to content

Commit

Permalink
Use native file dialogs in macOS
Browse files Browse the repository at this point in the history
This is a Java port of Jianhua Feng's earlier Groovy-PR (java-decompiler#180)
  • Loading branch information
lhaeger committed Dec 30, 2019
1 parent b3c1ced commit 7d37bb8
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 7 deletions.
15 changes: 8 additions & 7 deletions app/src/main/java/org/jd/gui/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import org.jd.gui.view.component.FileChooser;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
Expand Down Expand Up @@ -155,7 +156,7 @@ public void show(List<File> files) {
// Set drop files transfer handler
mainFrame.setTransferHandler(new FilesTransferHandler());
// Background class loading
new JFileChooser().addChoosableFileFilter(new FileNameExtensionFilter("", "dummy"));
new FileChooser().addChoosableFileFilter(new FileNameExtensionFilter("", "dummy"));
FileSystemView.getFileSystemView().isFileSystemRoot(new File("dummy"));
new JLayer();
});
Expand Down Expand Up @@ -183,7 +184,7 @@ protected void onOpen() {

String description = sb.toString();
String[] array = extensions.toArray(new String[0]);
JFileChooser chooser = new JFileChooser();
FileChooser chooser = new FileChooser();

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

chooser.setCurrentDirectory(configuration.getRecentLoadDirectory());

if (chooser.showOpenDialog(mainView.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
if (chooser.showOpenDialog(mainView.getMainFrame()) == FileChooser.APPROVE_OPTION) {
configuration.setRecentLoadDirectory(chooser.getCurrentDirectory());
openFile(chooser.getSelectedFile());
}
Expand All @@ -207,12 +208,12 @@ protected void onClose() {

protected void onSaveSource() {
if (currentPage instanceof ContentSavable) {
JFileChooser chooser = new JFileChooser();
FileChooser chooser = new FileChooser();
JFrame mainFrame = mainView.getMainFrame();

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

if (chooser.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
if (chooser.showSaveDialog(mainFrame) == FileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();

configuration.setRecentSaveDirectory(chooser.getCurrentDirectory());
Expand Down Expand Up @@ -245,12 +246,12 @@ protected void onSaveAllSources() {

if (currentPanel instanceof SourcesSavable) {
SourcesSavable sourcesSavable = (SourcesSavable)currentPanel;
JFileChooser chooser = new JFileChooser();
FileChooser chooser = new FileChooser();
JFrame mainFrame = mainView.getMainFrame();

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

if (chooser.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
if (chooser.showSaveDialog(mainFrame) == FileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();

configuration.setRecentSaveDirectory(chooser.getCurrentDirectory());
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/org/jd/gui/util/io/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jd.gui.util.io;

import java.io.File;


/**
* Created by jianhua.fengjh on 27/11/2015.
*/
public class FileUtils {

public static String ensureTrailingSlash(final String path) {
if ((path == null) || "".equals(path)) {
return "";
}

StringBuilder buf = new StringBuilder(path);
while (buf.charAt(buf.length() - 1) == File.separatorChar) {
buf.deleteCharAt(buf.length() - 1);
}

return buf.append(File.separatorChar).toString();
}
}
20 changes: 20 additions & 0 deletions app/src/main/java/org/jd/gui/util/sys/SystemUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jd.gui.util.sys;

/**
* Created by jianhua.fengjh on 27/11/2015.
*/
public final class SystemUtils {

static boolean isLinux() {
return System.getProperty("os.name").startsWith("Linux");
}

public static boolean isMacOS() {
return System.getProperty("os.name").startsWith("Mac");
}

public static boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}

}
94 changes: 94 additions & 0 deletions app/src/main/java/org/jd/gui/view/component/FileChooser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.jd.gui.view.component;

import org.jd.gui.util.io.FileUtils;
import org.jd.gui.util.sys.SystemUtils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FilenameFilter;

/**
* Created by jianhua.fengjh on 27/11/2015.
*/
public class FileChooser extends JFileChooser {

/**
*
*/
private static final long serialVersionUID = 1L;

public int showOpenDialog(Component parent) {
if (!SystemUtils.isMacOS()) {
return super.showOpenDialog(parent);
} else {
setDialogType(JFileChooser.OPEN_DIALOG);
return showNativeFileDialog(this);
}
}

public int showSaveDialog(Component parent) {

if (!SystemUtils.isMacOS()) {
return super.showSaveDialog(parent);
} else {
setDialogType(JFileChooser.SAVE_DIALOG);
return showNativeFileDialog(this);
}
}

private static int showNativeFileDialog(final JFileChooser chooser) {
if (chooser != null) {

FileDialog fileDialog = new FileDialog((Frame) chooser.getParent());
fileDialog.setDirectory(chooser.getCurrentDirectory().getPath());
File file = chooser.getSelectedFile();

if (chooser.getDialogType() == JFileChooser.SAVE_DIALOG) {
fileDialog.setFile(file != null ? file.getName() : ""); //save only need name
} else {
fileDialog.setFile(file != null ? file.getPath() : "");
}

fileDialog.setFilenameFilter(new FilenameFilter() {

public boolean accept(File dir, String name) {
String path = dir.getPath();
String pathSeparator = File.pathSeparator;
return chooser.getFileFilter().accept(new File(0 + path.length() + pathSeparator.length() + name.length() + path + pathSeparator + name));
}

});

if (chooser.getDialogType() == JFileChooser.SAVE_DIALOG) {
fileDialog.setMode(FileDialog.SAVE);
} else {
fileDialog.setMode(FileDialog.LOAD);
}

if (chooser.getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
} else {
System.setProperty("apple.awt.fileDialogForDirectories", "false");
}

fileDialog.setVisible(true);

//reset fileDialogForDirectories property
System.setProperty("apple.awt.fileDialogForDirectories", "false");
if (fileDialog.getFile() == null) {
return JFileChooser.CANCEL_OPTION;
}

String dir = fileDialog.getDirectory();
String trailingSlash = FileUtils.ensureTrailingSlash(dir);
String strFile = fileDialog.getFile();
chooser.setSelectedFile(new File(strFile.length() != 0 ? trailingSlash.concat(strFile) : trailingSlash));

return JFileChooser.APPROVE_OPTION;
}

return JFileChooser.ERROR_OPTION;
}

}

0 comments on commit 7d37bb8

Please sign in to comment.