|
| 1 | +/***************************************************************** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + ****************************************************************/ |
| 19 | + |
| 20 | +package org.apache.cayenne.modeler.platform.mac; |
| 21 | + |
| 22 | +import org.apache.cayenne.modeler.toolkit.filechooser.FileChooserFactory; |
| 23 | + |
| 24 | +import javax.swing.SwingUtilities; |
| 25 | +import javax.swing.filechooser.FileFilter; |
| 26 | +import java.awt.Component; |
| 27 | +import java.awt.FileDialog; |
| 28 | +import java.awt.Frame; |
| 29 | +import java.awt.Window; |
| 30 | +import java.io.File; |
| 31 | +import java.io.FilenameFilter; |
| 32 | + |
| 33 | +/** |
| 34 | + * {@link FileChooserFactory} implementation backed by the native OS {@link FileDialog}. |
| 35 | + * On macOS this delegates to NSOpenPanel / NSSavePanel, providing Quick Look preview, |
| 36 | + * Spotlight search, and the standard Finder sidebar. |
| 37 | + * |
| 38 | + * <p>Directory picking requires {@code apple.awt.fileDialogForDirectories=true} to be set |
| 39 | + * as a global JVM property before Swing starts (set by {@code MacUIInitializer}). |
| 40 | + */ |
| 41 | +public class MacFileChooserFactory extends FileChooserFactory { |
| 42 | + |
| 43 | + private static final String DIRS_DIALOG_PROPERTY = "apple.awt.fileDialogForDirectories"; |
| 44 | + |
| 45 | + @Override |
| 46 | + public File openFile(Component parent, String title, File initialDir, FileFilter filter) { |
| 47 | + Frame frame = toFrame(parent); |
| 48 | + String titleStr = title != null ? title : ""; |
| 49 | + FilenameFilter fnFilter = filter != null |
| 50 | + ? (dir, name) -> filter.accept(new File(dir, name)) |
| 51 | + : null; |
| 52 | + |
| 53 | + File startDir = initialDir; |
| 54 | + while (true) { |
| 55 | + FileDialog fd = new FileDialog(frame, titleStr, FileDialog.LOAD); |
| 56 | + if (startDir != null) { |
| 57 | + fd.setDirectory(startDir.getAbsolutePath()); |
| 58 | + } |
| 59 | + if (fnFilter != null) { |
| 60 | + fd.setFilenameFilter(fnFilter); |
| 61 | + } |
| 62 | + fd.setVisible(true); |
| 63 | + |
| 64 | + String file = fd.getFile(); |
| 65 | + if (file == null) { |
| 66 | + return null; // canceled |
| 67 | + } |
| 68 | + File selected = new File(fd.getDirectory(), file); |
| 69 | + if (selected.isFile()) { |
| 70 | + return selected; |
| 71 | + } |
| 72 | + // User selected a directory (possible when apple.awt.fileDialogForDirectories=true); |
| 73 | + // re-show starting inside it so they can navigate to a file. |
| 74 | + startDir = selected; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public File saveFile(Component parent, String title, File initialDir, String defaultName) { |
| 80 | + FileDialog fd = new FileDialog(toFrame(parent), title != null ? title : "", FileDialog.SAVE); |
| 81 | + if (initialDir != null) { |
| 82 | + fd.setDirectory(initialDir.getAbsolutePath()); |
| 83 | + } |
| 84 | + if (defaultName != null) { |
| 85 | + fd.setFile(defaultName); |
| 86 | + } |
| 87 | + fd.setVisible(true); |
| 88 | + String file = fd.getFile(); |
| 89 | + return file != null ? new File(fd.getDirectory(), file) : null; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public File openDirectory(Component parent, String title, File initialDir) { |
| 94 | + // Toggle the property only for the duration of this modal dialog. |
| 95 | + // FileDialog is EDT-blocking, so no other dialog can run concurrently. |
| 96 | + System.setProperty(DIRS_DIALOG_PROPERTY, "true"); |
| 97 | + try { |
| 98 | + FileDialog fd = new FileDialog(toFrame(parent), title != null ? title : "", FileDialog.LOAD); |
| 99 | + if (initialDir != null) { |
| 100 | + fd.setDirectory(initialDir.getAbsolutePath()); |
| 101 | + } |
| 102 | + fd.setVisible(true); |
| 103 | + String file = fd.getFile(); |
| 104 | + // getFile() returns the selected directory name; getDirectory() is its parent. |
| 105 | + return file != null ? new File(fd.getDirectory(), file) : null; |
| 106 | + } finally { |
| 107 | + System.clearProperty(DIRS_DIALOG_PROPERTY); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static Frame toFrame(Component c) { |
| 112 | + Window w = SwingUtilities.getWindowAncestor(c); |
| 113 | + while (w != null) { |
| 114 | + if (w instanceof Frame f) { |
| 115 | + return f; |
| 116 | + } |
| 117 | + w = w.getOwner(); |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | +} |
0 commit comments