Skip to content

Commit

Permalink
fix(gui): support filtering files with multiple extensions in file di…
Browse files Browse the repository at this point in the history
…alog (PR #2185)

* fix(gui): support filtering files with multiple extensions in file dialog

* lint
  • Loading branch information
xxr0ss authored May 19, 2024
1 parent 09fa35f commit 82e2104
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

import jadx.api.plugins.utils.CommonFileUtils;
import jadx.core.utils.Utils;
Expand Down Expand Up @@ -44,7 +43,7 @@ public List<Path> showDialog() {
List<String> fileExtList = data.getFileExtList();
if (Utils.notEmpty(fileExtList)) {
String description = NLS.str("file_dialog.supported_files") + ": (" + Utils.listToString(fileExtList) + ')';
setFileFilter(new FileNameExtensionFilter(description, fileExtList.toArray(new String[0])));
setFileFilter(new FileNameMultiExtensionFilter(description, fileExtList.toArray(new String[0])));
}
if (data.getSelectedFile() != null) {
setSelectedFile(data.getSelectedFile().toFile());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jadx.gui.ui.filedialog;

import java.io.File;

import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
* Custom file filter for filtering files with multiple extensions.
* It overcomes the limitation of {@link FileNameExtensionFilter},
* which treats only the last file extension split by dots as the
* file extension, and does not support multiple extensions such as
* {@code .jadx.kts}.
*/
class FileNameMultiExtensionFilter extends FileFilter {
private final FileNameExtensionFilter delegate;
private final String[] extensions;

public FileNameMultiExtensionFilter(String description, String... extensions) {
this.delegate = new FileNameExtensionFilter(description, extensions[0]);
this.extensions = extensions;
}

@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
String fileName = file.getName();
for (String extension : extensions) {
if (fileName.endsWith(extension)) {
return true;
}
}
return false;
}

@Override
public String getDescription() {
return delegate.getDescription();
}
}

0 comments on commit 82e2104

Please sign in to comment.