Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File chooser shortcuts panel #522

Merged
merged 3 commits into from May 28, 2022
Merged

File chooser shortcuts panel #522

merged 3 commits into from May 28, 2022

Conversation

DevCharly
Copy link
Collaborator

This PR adds a shortcuts panel to the left side of all file choosers (class JFileChooser):

image

image

On Windows, the content of the shortcuts panel is the same as in the Java Windows L&F.
On macOS and Linux the shortcuts panel is empty and hidden.

However you can customize the content. E.g.

UIManager.put( "FileChooser.shortcuts.filesFunction", (Function<File[], File[]>) files -> {
    ArrayList<File> list = new ArrayList<>( Arrays.asList( files ) );
    list.add( 0, new File( System.getProperty( "user.home" ) ) );
    return list.toArray( new File[list.size()] );
} );

UIManager.put( "FileChooser.shortcuts.displayNameFunction", (Function<File, String>) file -> {
    if( file.getAbsolutePath().equals( System.getProperty( "user.home" ) ) )
        return "Home";
    return null;
} );
UIManager.put( "FileChooser.shortcuts.iconFunction", (Function<File, Icon>) file -> {
    if( file.getAbsolutePath().equals( System.getProperty( "user.home" ) ) )
        return new FlatFileChooserHomeFolderIcon();
    return null;
} );

image

Downsides

The get the list of shortcut folders and to get the large icons, FlatLaf uses API that is not available in all Java versions.

  • the Java API to get list of shortcut folders is available since Java 12; reflection is used for older Java versions
  • the Java API to get large icons is available since Java 17; reflection is used for older Java versions

So for Java 9 to 16 you'll get following warning on stdout when a file chooser is opened.

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.formdev.flatlaf.ui.FlatFileChooserUI$FlatShortcutsPanel (file:...) to method sun.awt.shell.ShellFolder.get(java.lang.String)
WARNING: Please consider reporting this to the maintainers of com.formdev.flatlaf.ui.FlatFileChooserUI$FlatShortcutsPanel
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

If you use --illegal-access=deny, then the shortcuts panel will be empty (Java 9 to 11),
and small icons are scaled up (Java 9 to 18).

CC @Chrriis

@DevCharly DevCharly added this to the 2.3 milestone Apr 27, 2022
@Chrriis
Copy link
Contributor

Chrriis commented Apr 30, 2022

There are several expectations with file choosers:

  1. Faster navigation with shortcuts and perhaps some who want to customize these shortcuts.
  2. People who want a more native file chooser in line with the look and feel.
  3. People who want the native file chooser, independent from the look and feel.

I don't think FlatLaf should care about item 3 because there are other libraries that do it and it breaks the idea of a look and feel (I saw discussions on that topic lately, this is just my opinion).

This PR is more about 1 and 2:

  • It does give people a way to customize the file chooser shortcuts.
  • It feels more native because it has a navigation pane.

That being said, and correct me if I am wrong, it looks like the Windows 7 file chooser, or the AWT file chooser. All the file choosers that I can see on my Windows 10 have a tree on the left. My native file chooser are more like the second one from this (random) link:
https://stackoverflow.com/questions/39819319/windows-native-file-chooser-in-java

So, it is definitely an improvement, it solves item 1, but it is a partial answer to item 2. I understand the constraints so I know that it is not possible to support item 2 completely (like having a tree on the left, and editable crumb bar at the top).

I do welcome this change! Keep up the good work!

@DevCharly DevCharly merged commit 92b53bf into main May 28, 2022
@DevCharly DevCharly deleted the filechooser-shortcuts branch May 28, 2022 20:46
DevCharly added a commit that referenced this pull request Oct 21, 2022
… too large right margin (issue #604; regression since implementing PR #522 in FlatLaf 2.3)
DevCharly added a commit that referenced this pull request Aug 7, 2023
…va 17 64-bit is not affected. (regression since FlatLaf 2.3; PR #522, see also issue #403)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants