Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make file filter show the extension, instead of cryptic stuff #82

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) );
}
}