Skip to content

Commit

Permalink
add native open, save dlg in macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jianhua.fengjh authored and fjh658 committed Nov 27, 2016
1 parent acd511f commit f99010a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
20 changes: 20 additions & 0 deletions app/src/main/groovy/org/jd/gui/util/io/FileUtils.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jd.gui.util.io

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

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/groovy/org/jd/gui/util/sys/SystemUtils.groovy
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.
*/
final class SystemUtils {

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

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

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

}
5 changes: 3 additions & 2 deletions app/src/main/groovy/org/jd/gui/view/MainView.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.jd.gui.api.feature.UriOpenable
import org.jd.gui.model.configuration.Configuration
import org.jd.gui.model.history.History
import org.jd.gui.spi.FileLoader
import org.jd.gui.view.component.FileChooser

import javax.swing.Icon
import javax.swing.JComponent
Expand Down Expand Up @@ -199,7 +200,7 @@ class MainView implements UriOpenable, PreferencesChangeListener {
}

JFileChooser createOpenFileChooser() {
JFileChooser chooser = new JFileChooser() {
FileChooser chooser = new FileChooser() {
void addFileFilters(Map<String, FileLoader> loaders) {
removeChoosableFileFilter(getFileFilter())

Expand All @@ -223,7 +224,7 @@ class MainView implements UriOpenable, PreferencesChangeListener {
}

JFileChooser createSaveFileChooser() {
JFileChooser chooser = new JFileChooser() {
FileChooser chooser = new FileChooser() {
void show(Closure okClosure) {
if (showSaveDialog(swing.mainFrame) == JFileChooser.APPROVE_OPTION) {
if (selectedFile.exists()) {
Expand Down
87 changes: 87 additions & 0 deletions app/src/main/groovy/org/jd/gui/view/component/FileChooser.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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.*

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

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

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() {

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 f99010a

Please sign in to comment.