kaikramer / keystore-explorer

KeyStore Explorer is a free GUI replacement for the Java command-line utilities keytool and jarsigner.
https://keystore-explorer.org/
GNU General Public License v3.0
1.7k stars 275 forks source link

"Open" dialogs default to "All Files" #385

Closed ciis0 closed 1 year ago

ciis0 commented 2 years ago

Thank you for creating and maintaining KSE!

I have been using it regularly lately and found this small annoyance.

Describe the bug

When opening file, e.g. for Certificate Extension Templates, the Open (and Save) chooser default to "All Files", although appropriate *.cet filters are present.

To Reproduce Steps to reproduce the behavior:

  1. Go to Generate Key Pair
  2. Algorithm most likely not relevant here, I have used RSA and key length 3096 (bits?)
  3. Add Extensions
  4. Load Template
  5. Observe Files of Type 5.1. is set to All Files. 5.2. has available filter for Certificate Extensions Template Files (*.cet)

Expected behavior

I would expect the *.cet filter to be pre-selected.

Screenshots

image

Environment

ciis0 commented 2 years ago

SO suggests this is due to using addChoosableFileFilter and not setFileFilter.

https://github.com/kaikramer/keystore-explorer/blob/dab085174bcc3d67dc115742e0cf421df17ba3ef/kse/src/org/kse/gui/FileChooserFactory.java#L228-L237

I'll check whether that does what I want.

Colbix commented 2 years ago

According to the same link, reading further down, the default is true. The dialog creation for the intended extension doesn't set the setAcceptAllFileFilterUsed to false. To expand on this, the preset methods which add specific extension for the file chooser currently doesn't set a default extension. I can look into setting the default extensions, most likely alphabetical order if there are more than one extension for the given preset.

https://github.com/kaikramer/keystore-explorer/blob/dab085174bcc3d67dc115742e0cf421df17ba3ef/kse/src/org/kse/gui/dialogs/extensions/DAddExtensions.java#L756-L761

ciis0 commented 2 years ago

I would prefer to keep the All Files, it should just not be the default. :)

kaikramer commented 2 years ago

@Colbix Are there any disadvantages in replacing addChoosableFileFilter() with setFileFilter()? It seems to work, at least with the Swing FileChooser.

Colbix commented 2 years ago

@kaikramer The Swing FileChooser will work, no disadvantages at all. The JavaFXFileChooser that will need to be updated to accept the method for setSelectedExtensionFilter. At the moment it's just adding the extensions instead of setting the default filter.

Colbix commented 2 years ago

Created PR #386 to update the JavaFXFileChooser class. Should allow for setSelectedExtensionFilter. Tested with multifile selection, single file selections, and with/without All Files. If you can think of anything else to test please let me know. I also took the opportunity to move some of the logic loops into their own function to reduce repeated actions.

After this update the FileChooserFactory class can be updated to reflect which ever default file extension is needed.

ciis0 commented 1 year ago

Works wonderfully, thank you! :)