Skip to content

Commit

Permalink
Make file filter show the extension, instead of cryptic stuff
Browse files Browse the repository at this point in the history
* Override FileFilter toString() method in SwingFileWidget so that it shows the extension(s) that are filtered by the file choose dialog instead of the standard toString() of the
* Related to
  * bigdataviewer/bigdataviewer-playground#110
  * https://forum.image.sc/t/scijava-command-file-extensions/29869/2
  • Loading branch information
stefanhahmann committed May 28, 2024
1 parent 2770bbf commit 205cdc4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/scijava/ui/swing/widget/SwingFileWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import javax.swing.Box;
Expand Down Expand Up @@ -235,9 +236,24 @@ public boolean accept(final File pathname) {
}
return false;
}

@Override
public String toString() {
return formatFileFilterExtensions( exts );
}
};
}

static String formatFileFilterExtensions( final List< String > extensions ) {
List< String > extsWithPrefix = new ArrayList<>();
for ( String extension : extensions ) {
extsWithPrefix.add( "*." + extension );
}
StringJoiner joiner = new StringJoiner( ";" );
extsWithPrefix.forEach( joiner::add );
return joiner.toString();
}

/**
* Checks whether the given drag and drop operation offers a list of files as
* one of its flavors.
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/scijava/ui/swing/widget/SwingFileWidgetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.scijava.ui.swing.widget;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SwingFileWidgetTest
{

@Test
public void testFormatFileFilterExtensions() {
final List< String > multipleExtensions = Arrays.asList( "jpg", "jpeg", "png", "gif", "bmp" );
final List< String > singleExtension = Collections.singletonList( "xml" );
assertEquals( "*.jpg;*.jpeg;*.png;*.gif;*.bmp", SwingFileWidget.formatFileFilterExtensions( multipleExtensions ) );
assertEquals( "*.xml", SwingFileWidget.formatFileFilterExtensions( singleExtension ) );
}
}

0 comments on commit 205cdc4

Please sign in to comment.