On Thu, 8 May 2025 04:08:30 GMT, Tejesh R <t...@openjdk.org> wrote:

>> @aivanov-jdk @TejeshR13 
>> This seems stalled because there are conflicting statements
>> - Alexei : you can create a custom FIleSystemView on windows with <html> in 
>> path names.
>> - Tejesh : no you can't, or at least it doesn't work when you try to use it.
>> 
>> Can we please get that reconciled ?
>> Tejesh pls show the code you used, and Alexei please show how you would do 
>> it.
>
> I tried with this code snippet. 
> 
> import javax.swing.JFileChooser;
> import javax.swing.SwingUtilities;
> import javax.swing.UIManager;
> import javax.swing.UnsupportedLookAndFeelException;
> import javax.swing.filechooser.FileSystemView;
> import java.io.File;
> import java.util.Arrays;
> 
> public class VirtualFileChooser {
> 
>     public static void main(String[] args) throws 
> UnsupportedLookAndFeelException, ClassNotFoundException, 
> InstantiationException, IllegalAccessException {
>           
> UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
>         
> //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
> 
> 
>         SwingUtilities.invokeLater(() -> {
>             JFileChooser chooser = new JFileChooser(new 
> VirtualFileSystemView());
> 
>             int result = chooser.showOpenDialog(null);
>             if (result == JFileChooser.APPROVE_OPTION) {
>                 File selectedFile = chooser.getSelectedFile();
>                 System.out.println("Selected: " + 
> selectedFile.getAbsolutePath());
>             }
>         });
>     }
> 
>     static class VirtualFileSystemView extends FileSystemView {
> 
>         private final File[] roots;
> 
>         public VirtualFileSystemView() {
>             // Create a dummy root folder
>             String name = "<html><h1 color=#ff00ff><font face="Comic Sans 
> MS">SWING ROCKS!!!111";
>             roots = new File[]{new File(name)};
>         }
> 
>         @Override
>         public File createNewFolder(File containingDir) {
> 
>             File folder = new File(containingDir, "New Folder");
>             folder.mkdir();
>             return folder;
>         }
> 
>         @Override
>         public File[] getRoots() {
>             return roots;
>         }
> 
>         @Override
>         public File getHomeDirectory() {
>             return roots[0];
>         }
> 
>         @Override
>         public File getDefaultDirectory() {
>             return roots[0];
>         }
> 
>         @Override
>         public File[] getFiles(File dir, boolean useFileHiding) {
>             // Simulate a virtual folder structure
>             return new File[]{
>                     new File(dir, "virtualFile1.txt"),
>                     new File(dir, "virtualFile2.txt"),
>                     new File(dir, "virtualFolder")
>             };
>         }
> 
>         @Override
>         public boolean isRoot(File file) {
>             return Arrays.asList(roots).contains(file);
>         }
> 
>         @Override
>         public Boolean isTraversable(File f) {
>             return true;
>         }
>    ...

> Exception in thread "AWT-EventQueue-0" java.nio.file.InvalidPathException: 
> Illegal char <<> at index 0: <html><h1 color=#ff00ff><font face="Comic Sans 
> MS">SWING ROCKS!!!111
>         at 
> java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:191)
>         at 
> java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:142)
>         at 
> java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:46)
>         at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
>         at 
> java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:203)
>         at java.base/java.nio.file.Path.of(Path.java:148)
>         at java.base/java.nio.file.Paths.get(Paths.java:69)
>         at 
> java.desktop/sun.awt.shell.ShellFolder.getShellFolder(ShellFolder.java:260)
>         at 
> java.desktop/javax.swing.filechooser.FileSystemView.getShellFolder(FileSystemView.java:724)
>         at 
> java.desktop/javax.swing.filechooser.FileSystemView.getSystemIcon(FileSystemView.java:242)
>         at 
> java.desktop/com.sun.java.swing.plaf.windows.WindowsFileChooserUI$WindowsFileView.getIcon(WindowsFileChooserUI.java:1398)
>         at 
> java.desktop/javax.swing.JFileChooser.getIcon(JFileChooser.java:1614)


This exception looks like a bug? I think if the file was not found then null is 
an expected result.
For now it can be bypassed by this:


import java.io.File;
import java.lang.Override;

import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;

public class VirtualFileChooser {

    public static void main(String[] args) throws 
UnsupportedLookAndFeelException, ClassNotFoundException, 
InstantiationException, IllegalAccessException {
        
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");


        SwingUtilities.invokeLater(() -> {
            JFileChooser chooser = new JFileChooser(new 
VirtualFileSystemView());

            int result = chooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = chooser.getSelectedFile();
                System.out.println("Selected: " + 
selectedFile.getAbsolutePath());
            }
        });
    }

    static class VirtualFileSystemView extends FileSystemView {

        @Override
        public File createNewFolder(File containingDir) {
            return null;
        }

        @Override
        public File[] getRoots() {
            return new File[]{
                    new File("/", "<html><h1 color=#ff00ff><font face="Comic 
Sans MS">SWING ROCKS!!!111"),
                    new File("/", "virtualFile2.txt"),
                    new File("/", "virtualFolder")
            };
        }

        @Override
        public File getHomeDirectory() {
            return new File("/");
        }

        @Override
        public File getDefaultDirectory() {
            return new File("/");
        }

        @Override
        public File[] getFiles(File dir, boolean useFileHiding) {
            // Simulate a virtual folder structure
            return new File[]{
                    new File("/", "<html><h1 color=#ff00ff><font face="Comic 
Sans MS">SWING ROCKS!!!111"),
                    new File(dir, "virtualFile2.txt"),
                    new File(dir, "virtualFolder")
            };
        }

        @Override
        public Icon getSystemIcon(File f) {
            return null;
        }
    }
}

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/24439#discussion_r2085839647

Reply via email to