[7937] | 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.io.File;
|
---|
| 7 | import java.io.FilenameFilter;
|
---|
[11004] | 8 | import java.util.ArrayList;
|
---|
| 9 | import java.util.List;
|
---|
[7937] | 10 |
|
---|
[18113] | 11 | import javax.swing.ActionMap;
|
---|
[7937] | 12 | import javax.swing.JFileChooser;
|
---|
| 13 | import javax.swing.filechooser.FileFilter;
|
---|
| 14 |
|
---|
[14153] | 15 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[14138] | 16 | import org.openstreetmap.josm.tools.PlatformManager;
|
---|
[10931] | 17 | import org.openstreetmap.josm.tools.Utils;
|
---|
[7937] | 18 |
|
---|
| 19 | /**
|
---|
| 20 | * File chooser based on the AWT's {@link FileDialog} implementation,
|
---|
| 21 | * which looks like more a native file chooser than the Swing implementation.
|
---|
| 22 | * @since 7578
|
---|
| 23 | */
|
---|
| 24 | public class NativeFileChooser extends AbstractFileChooser {
|
---|
| 25 |
|
---|
| 26 | /** The instance of the fileDialog */
|
---|
| 27 | private final FileDialog fileDialog;
|
---|
| 28 | private FileFilter fileFilter;
|
---|
[11015] | 29 | private final List<FileFilter> fileFilters = new ArrayList<>();
|
---|
[7937] | 30 | private int selectionMode;
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Constructs a new {@code NativeFileChooser}.
|
---|
| 34 | * @param file the current file/directory to point to
|
---|
| 35 | */
|
---|
| 36 | public NativeFileChooser(File file) {
|
---|
[15287] | 37 | fileDialog = new FileDialog(MainApplication.getMainFrame());
|
---|
[7937] | 38 | if (file != null) {
|
---|
| 39 | fileDialog.setDirectory(file.getAbsolutePath());
|
---|
[9257] | 40 | if (file.isFile()) {
|
---|
| 41 | fileDialog.setFile(file.toString());
|
---|
| 42 | }
|
---|
[7937] | 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | @Override
|
---|
| 47 | public void addChoosableFileFilter(FileFilter filter) {
|
---|
| 48 | // TODO implement this after Oracle fixes JDK-4811090 / JDK-6192906
|
---|
| 49 | // https://bugs.openjdk.java.net/browse/JDK-4811090 : Extend awt filedialog
|
---|
| 50 | // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
|
---|
[11004] | 51 | fileFilters.add(filter);
|
---|
[7937] | 52 | }
|
---|
| 53 |
|
---|
| 54 | @Override
|
---|
| 55 | public FileFilter[] getChoosableFileFilters() {
|
---|
| 56 | // TODO implement this after Oracle fixes JDK-4811090 / JDK-6192906
|
---|
| 57 | // https://bugs.openjdk.java.net/browse/JDK-4811090 : Extend awt filedialog
|
---|
| 58 | // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
|
---|
[13206] | 59 | return fileFilters.toArray(new FileFilter[0]);
|
---|
[7937] | 60 | }
|
---|
| 61 |
|
---|
| 62 | @Override
|
---|
| 63 | public File getCurrentDirectory() {
|
---|
| 64 | return new File(fileDialog.getDirectory());
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | public FileFilter getFileFilter() {
|
---|
| 69 | return fileFilter;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | @Override
|
---|
| 73 | public File getSelectedFile() {
|
---|
| 74 | return new File(fileDialog.getDirectory() + fileDialog.getFile());
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Override
|
---|
| 78 | public File[] getSelectedFiles() {
|
---|
| 79 | return fileDialog.getFiles();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Override
|
---|
| 83 | public boolean isMultiSelectionEnabled() {
|
---|
| 84 | return fileDialog.isMultipleMode();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @Override
|
---|
| 88 | public void setAcceptAllFileFilterUsed(boolean b) {
|
---|
| 89 | // TODO implement this after Oracle fixes JDK-4811090 / JDK-6192906
|
---|
| 90 | // https://bugs.openjdk.java.net/browse/JDK-4811090 : Extend awt filedialog
|
---|
| 91 | // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @Override
|
---|
| 95 | public void setCurrentDirectory(File f) {
|
---|
| 96 | fileDialog.setDirectory(f.toString());
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | @Override
|
---|
| 100 | public void setDialogTitle(String title) {
|
---|
| 101 | fileDialog.setTitle(title);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | @Override
|
---|
| 105 | public void setFileFilter(final FileFilter cff) {
|
---|
[10611] | 106 | FilenameFilter filter = (directory, fileName) -> cff.accept(new File(directory.getAbsolutePath() + fileName));
|
---|
[7937] | 107 | fileDialog.setFilenameFilter(filter);
|
---|
| 108 | fileFilter = cff;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | public void setFileSelectionMode(int selectionMode) {
|
---|
[8540] | 113 | // CHECKSTYLE.OFF: LineLength
|
---|
[7937] | 114 | // TODO implement this after Oracle fixes JDK-6192906 / JDK-6699863 / JDK-6927978 / JDK-7125172:
|
---|
| 115 | // https://bugs.openjdk.java.net/browse/JDK-6192906 : Add more features to java.awt.FileDialog
|
---|
| 116 | // https://bugs.openjdk.java.net/browse/JDK-6699863 : awt filedialog cannot select directories
|
---|
| 117 | // https://bugs.openjdk.java.net/browse/JDK-6927978 : Directory Selection standard dialog support
|
---|
| 118 | // https://bugs.openjdk.java.net/browse/JDK-7125172 : FileDialog objects don't allow directory AND files selection simultaneously
|
---|
| 119 |
|
---|
| 120 | // There is however a basic support for directory selection on OS X, with Java >= 7u40:
|
---|
| 121 | // http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x/1224744#1224744
|
---|
| 122 | // https://bugs.openjdk.java.net/browse/JDK-7161437 : [macosx] awt.FileDialog doesn't respond appropriately for mac when selecting folders
|
---|
[8540] | 123 | // CHECKSTYLE.ON: LineLength
|
---|
[7937] | 124 | this.selectionMode = selectionMode;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @Override
|
---|
| 128 | public void setMultiSelectionEnabled(boolean multiple) {
|
---|
| 129 | fileDialog.setMultipleMode(multiple);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | @Override
|
---|
| 133 | public void setSelectedFile(File file) {
|
---|
[9590] | 134 | if (file == null) return;
|
---|
| 135 | fileDialog.setDirectory(file.getParent());
|
---|
[7937] | 136 | fileDialog.setFile(file.getName());
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | @Override
|
---|
| 140 | public int showOpenDialog(Component parent) {
|
---|
[14138] | 141 | boolean appleProperty = PlatformManager.isPlatformOsx() && selectionMode == JFileChooser.DIRECTORIES_ONLY;
|
---|
[7937] | 142 | if (appleProperty) {
|
---|
[10931] | 143 | Utils.updateSystemProperty("apple.awt.fileDialogForDirectories", "true");
|
---|
[7937] | 144 | }
|
---|
| 145 | try {
|
---|
| 146 | fileDialog.setLocale(locale);
|
---|
| 147 | fileDialog.setMode(FileDialog.LOAD);
|
---|
| 148 | fileDialog.setVisible(true);
|
---|
| 149 | return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
|
---|
| 150 | } finally {
|
---|
| 151 | if (appleProperty) {
|
---|
[10931] | 152 | Utils.updateSystemProperty("apple.awt.fileDialogForDirectories", "false");
|
---|
[7937] | 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | @Override
|
---|
| 158 | public int showSaveDialog(Component parent) {
|
---|
| 159 | fileDialog.setLocale(locale);
|
---|
| 160 | fileDialog.setMode(FileDialog.SAVE);
|
---|
| 161 | fileDialog.setVisible(true);
|
---|
| 162 | return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[18113] | 165 | @Override
|
---|
| 166 | public ActionMap getActionMap() {
|
---|
| 167 | return new ActionMap();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[7937] | 170 | /**
|
---|
| 171 | * Determines if the selection mode is suuported by the native file chooser.
|
---|
| 172 | * @param selectionMode the selection mode
|
---|
| 173 | * @return {@code true} if the selection mode is supported, {@code false} otherwise
|
---|
| 174 | */
|
---|
| 175 | public static boolean supportsSelectionMode(int selectionMode) {
|
---|
| 176 | switch (selectionMode) {
|
---|
| 177 | case JFileChooser.FILES_AND_DIRECTORIES:
|
---|
[8540] | 178 | // CHECKSTYLE.OFF: LineLength
|
---|
[7937] | 179 | // https://bugs.openjdk.java.net/browse/JDK-7125172 : FileDialog objects don't allow directory AND files selection simultaneously
|
---|
| 180 | return false;
|
---|
| 181 | case JFileChooser.DIRECTORIES_ONLY:
|
---|
| 182 | // http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x/1224744#1224744
|
---|
[8540] | 183 | // CHECKSTYLE.ON: LineLength
|
---|
[14138] | 184 | return PlatformManager.isPlatformOsx();
|
---|
[7937] | 185 | case JFileChooser.FILES_ONLY:
|
---|
| 186 | default:
|
---|
| 187 | return true;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|