mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 22:10:54 +03:00
System File Chooser: support macOS in class SystemFileChooser
This commit is contained in:
@@ -72,7 +72,7 @@ public class FlatNativeMacLibrary
|
|||||||
|
|
||||||
/** @since 3.6 */
|
/** @since 3.6 */
|
||||||
public static final int
|
public static final int
|
||||||
// NSOpenPanel
|
// NSOpenPanel (extends NSSavePanel)
|
||||||
FC_canChooseFiles = 1 << 0, // default
|
FC_canChooseFiles = 1 << 0, // default
|
||||||
FC_canChooseDirectories = 1 << 1,
|
FC_canChooseDirectories = 1 << 1,
|
||||||
FC_resolvesAliases = 1 << 2, // default
|
FC_resolvesAliases = 1 << 2, // default
|
||||||
@@ -105,7 +105,8 @@ public class FlatNativeMacLibrary
|
|||||||
* @param optionsSet options to set; see {@code FC_*} constants
|
* @param optionsSet options to set; see {@code FC_*} constants
|
||||||
* @param optionsClear options to clear; see {@code FC_*} constants
|
* @param optionsClear options to clear; see {@code FC_*} constants
|
||||||
* @param allowedFileTypes allowed filename extensions (e.g. "txt")
|
* @param allowedFileTypes allowed filename extensions (e.g. "txt")
|
||||||
* @return file path(s) that the user selected, or {@code null} if canceled
|
* @return file path(s) that the user selected; an empty array if canceled;
|
||||||
|
* or {@code null} on failures (no dialog shown)
|
||||||
*
|
*
|
||||||
* @since 3.6
|
* @since 3.6
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import javax.swing.UIManager;
|
|||||||
import javax.swing.filechooser.FileSystemView;
|
import javax.swing.filechooser.FileSystemView;
|
||||||
import com.formdev.flatlaf.FlatSystemProperties;
|
import com.formdev.flatlaf.FlatSystemProperties;
|
||||||
import com.formdev.flatlaf.ui.FlatNativeLinuxLibrary;
|
import com.formdev.flatlaf.ui.FlatNativeLinuxLibrary;
|
||||||
|
import com.formdev.flatlaf.ui.FlatNativeMacLibrary;
|
||||||
import com.formdev.flatlaf.ui.FlatNativeWindowsLibrary;
|
import com.formdev.flatlaf.ui.FlatNativeWindowsLibrary;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,7 +62,11 @@ import com.formdev.flatlaf.ui.FlatNativeWindowsLibrary;
|
|||||||
* If user selects "Yes", the file chooser closes. If user selects "No", the file chooser stays open.
|
* If user selects "Yes", the file chooser closes. If user selects "No", the file chooser stays open.
|
||||||
* It is not possible to customize that question dialog.
|
* It is not possible to customize that question dialog.
|
||||||
* <li><b>Save File</b> dialog does not support multi-selection.
|
* <li><b>Save File</b> dialog does not support multi-selection.
|
||||||
|
* <li>For selection mode {@link #DIRECTORIES_ONLY}, dialog type {@link #SAVE_DIALOG} is ignored.
|
||||||
|
* Operating system file dialogs support folder selection only in "Open" dialogs.
|
||||||
* <li>{@link JFileChooser#FILES_AND_DIRECTORIES} is not supported.
|
* <li>{@link JFileChooser#FILES_AND_DIRECTORIES} is not supported.
|
||||||
|
* <li>{@link #getSelectedFiles()} returns selected file also in single selection mode.
|
||||||
|
* {@link JFileChooser#getSelectedFiles()} only in multi selection mode.
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author Karl Tauber
|
* @author Karl Tauber
|
||||||
@@ -289,6 +294,8 @@ public class SystemFileChooser
|
|||||||
|
|
||||||
if( SystemInfo.isWindows_10_orLater && FlatNativeWindowsLibrary.isLoaded() )
|
if( SystemInfo.isWindows_10_orLater && FlatNativeWindowsLibrary.isLoaded() )
|
||||||
return new WindowsFileChooserProvider();
|
return new WindowsFileChooserProvider();
|
||||||
|
else if( SystemInfo.isMacOS && FlatNativeMacLibrary.isLoaded() )
|
||||||
|
return new MacFileChooserProvider();
|
||||||
else if( SystemInfo.isLinux && FlatNativeLinuxLibrary.isLoaded() )
|
else if( SystemInfo.isLinux && FlatNativeLinuxLibrary.isLoaded() )
|
||||||
return new LinuxFileChooserProvider();
|
return new LinuxFileChooserProvider();
|
||||||
else // unknown platform or FlatLaf native library not loaded
|
else // unknown platform or FlatLaf native library not loaded
|
||||||
@@ -413,7 +420,52 @@ public class SystemFileChooser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//---- class LinuxFileChooserProvider -----------------------------------..
|
//---- class MacFileChooserProvider ---------------------------------------
|
||||||
|
|
||||||
|
private static class MacFileChooserProvider
|
||||||
|
extends SystemFileChooserProvider
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
String[] showSystemDialog( Window owner, SystemFileChooser fc ) {
|
||||||
|
boolean open = (fc.getDialogType() == OPEN_DIALOG);
|
||||||
|
String nameFieldStringValue = null;
|
||||||
|
String directoryURL = null;
|
||||||
|
|
||||||
|
// paths
|
||||||
|
File currentDirectory = fc.getCurrentDirectory();
|
||||||
|
File selectedFile = fc.getSelectedFile();
|
||||||
|
if( selectedFile != null ) {
|
||||||
|
if( selectedFile.isDirectory() )
|
||||||
|
directoryURL = selectedFile.getAbsolutePath();
|
||||||
|
else {
|
||||||
|
nameFieldStringValue = selectedFile.getName();
|
||||||
|
directoryURL = selectedFile.getParent();
|
||||||
|
}
|
||||||
|
} else if( currentDirectory != null )
|
||||||
|
directoryURL = currentDirectory.getAbsolutePath();
|
||||||
|
|
||||||
|
// options
|
||||||
|
int optionsSet = 0;
|
||||||
|
int optionsClear = 0;
|
||||||
|
if( fc.isDirectorySelectionEnabled() ) {
|
||||||
|
optionsSet |= FlatNativeMacLibrary.FC_canChooseDirectories;
|
||||||
|
optionsClear |= FlatNativeMacLibrary.FC_canChooseFiles;
|
||||||
|
open = true;
|
||||||
|
}
|
||||||
|
if( fc.isMultiSelectionEnabled() )
|
||||||
|
optionsSet |= FlatNativeMacLibrary.FC_allowsMultipleSelection;
|
||||||
|
if( !fc.isFileHidingEnabled() )
|
||||||
|
optionsSet |= FlatNativeMacLibrary.FC_showsHiddenFiles;
|
||||||
|
|
||||||
|
// show system file dialog
|
||||||
|
return FlatNativeMacLibrary.showFileChooser( open,
|
||||||
|
fc.getDialogTitle(), fc.getApproveButtonText(), null, null,
|
||||||
|
nameFieldStringValue, directoryURL,
|
||||||
|
optionsSet, optionsClear );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---- class LinuxFileChooserProvider -------------------------------------
|
||||||
|
|
||||||
private static class LinuxFileChooserProvider
|
private static class LinuxFileChooserProvider
|
||||||
extends SystemFileChooserProvider
|
extends SystemFileChooserProvider
|
||||||
|
|||||||
@@ -23,15 +23,23 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Karl Tauber
|
* @author Karl Tauber
|
||||||
|
* @since 3.6
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//---- helper -----------------------------------------------------------------
|
||||||
|
|
||||||
#define isOptionSet( option ) ((optionsSet & com_formdev_flatlaf_ui_FlatNativeMacLibrary_ ## option) != 0)
|
#define isOptionSet( option ) ((optionsSet & com_formdev_flatlaf_ui_FlatNativeMacLibrary_ ## option) != 0)
|
||||||
#define isOptionClear( option ) ((optionsClear & com_formdev_flatlaf_ui_FlatNativeMacLibrary_ ## option) != 0)
|
#define isOptionClear( option ) ((optionsClear & com_formdev_flatlaf_ui_FlatNativeMacLibrary_ ## option) != 0)
|
||||||
#define isOptionSetOrClear( option ) isOptionSet( option ) || isOptionClear( option )
|
#define isOptionSetOrClear( option ) isOptionSet( option ) || isOptionClear( option )
|
||||||
|
|
||||||
// declare internal methods
|
// declare methods
|
||||||
NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window );
|
NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window );
|
||||||
|
|
||||||
|
jobjectArray newJavaStringArray( JNIEnv* env, jsize count ) {
|
||||||
|
jclass stringClass = env->FindClass( "java/lang/String" );
|
||||||
|
return env->NewObjectArray( count, stringClass, NULL );
|
||||||
|
}
|
||||||
|
|
||||||
//---- JNI methods ------------------------------------------------------------
|
//---- JNI methods ------------------------------------------------------------
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
@@ -135,19 +143,18 @@ JNIEXPORT jobjectArray JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_
|
|||||||
urls = @[url];
|
urls = @[url];
|
||||||
|
|
||||||
if( urls == NULL )
|
if( urls == NULL )
|
||||||
return NULL;
|
return newJavaStringArray( env, 0 );
|
||||||
|
|
||||||
// convert URLs to Java string array
|
// convert URLs to Java string array
|
||||||
jsize count = urls.count;
|
jsize count = urls.count;
|
||||||
jclass stringClass = env->FindClass( "java/lang/String" );
|
jobjectArray array = newJavaStringArray( env, count );
|
||||||
jobjectArray result = env->NewObjectArray( count, stringClass, NULL );
|
|
||||||
for( int i = 0; i < count; i++ ) {
|
for( int i = 0; i < count; i++ ) {
|
||||||
jstring filename = NormalizedPathJavaFromNSString( env, [urls[i] path] );
|
jstring filename = NormalizedPathJavaFromNSString( env, [urls[i] path] );
|
||||||
env->SetObjectArrayElement( result, i, filename );
|
env->SetObjectArrayElement( array, i, filename );
|
||||||
env->DeleteLocalRef( filename );
|
env->DeleteLocalRef( filename );
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return array;
|
||||||
|
|
||||||
JNI_COCOA_EXIT()
|
JNI_COCOA_EXIT()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user