Commits
Lothar Haeger authored and Frank Schima committed 80fdc4673ee
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 + | } |