mgarin / weblaf

WebLaF is a fully open-source Look & Feel and component library written in pure Java for cross-platform desktop Swing applications.
http://weblookandfeel.com
GNU General Public License v3.0
1.14k stars 235 forks source link

JFileChooser file filters issues #278

Open mgarin opened 9 years ago

mgarin commented 9 years ago

By invian-kmartimo

We have a third party library (let's call it Foo) which does something similar to this when saving a file:

JFileChooser chooser = new JFileChooser();

for (FooFileFilter f : saveFileFilters) {
    // f is a class that inherits FooFileFilter
    chooser.addChoosableFileFilter(f);
}

chooser.setFileFilter(saveFileFilters.get(0));

if (c.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
    FileFilter filter = chooser.getFileFilter();
    File file = chooser.getSelectedFile();

    if (filter instanceof FooFileFilter) {
        // the filter itself contains the logic for saving the data as the selected file format (!!!)
        filter.save(sourceData, file);
    } else {
        new FooDefaultFileFilter().save(sourceData, file);
    }
}

Now, this doesn't work for two reasons:

1) When selecting a filter from the file chooser dropdown, WebFileChooserPanel doesn't tell the underlying JFileChooser instance that the filter has been changed, so chooser.getFileFilter() always returns the first one

I fixed this by patching WebFileChooserPanel.setActiveFileFilter(AbstactFileFilter fileFilter, boolean select) to do fileChooser.setFileFilter(fileFilter) also, which fixed the problem.

However, this didn't fix the overall situation, because 2) WebFileChooserPanel converts all FileFilters it gets to com.alee.utils.CustomFileFilter instances and throws away the originals, so chooser.getFileFilter() returns CustomFileFilter instances which is not what the Foo library expects and the end result is that FooDefaultFileFilter is always called. I think baking the file-format specific saving logic in the filter itself is fairly dumb, but I wouldn't expect the filters to be transformed when put through JFileChooser either ;)

Original issue: http://weblookandfeel.com/forum/viewtopic.php?f=4&t=215

AdelDima commented 9 years ago

We have the same issues here with the JasperViewer always returns the first one.

iMichka commented 9 years ago

Same problem for me.

One quick solution was to use WebLaf's getActiveFileFilter method instead of getFileFilter to get the currently selected file filter. But this is not really a nice solution.

iMichka commented 8 years ago

On the latest styling branch, getActiveFileFilter is now gone (which is good). getFileFilter seems to work correctly now. @mgarin did you fix it or is this just some side-effect of the refactoring ?

mgarin commented 8 years ago

That is a good question. I certainly did a lot of changes since the bug was submitted and this might have been fixed or it simply mutated into some other issue which is also highly possible. I'll have to investigate this again to see what exactly happened.