Workbench - Improved search box

Can now find exact matches using = or use regexp if starting with /
This commit is contained in:
Emmanuel BENOîT 2016-07-23 10:08:21 +02:00
parent b7448b213d
commit 6d39c1e565

View file

@ -4,6 +4,9 @@ package mmm.tech.base.workbench;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
@ -224,10 +227,42 @@ public class TBWBGui
this.recipes.clear( ); this.recipes.clear( );
} }
Predicate< String > matcher;
if ( newText.charAt( 0 ) == '=' && newText.length( ) > 1 ) {
final String searchText = newText.substring( 1 );
matcher = ( x ) -> x.equals( searchText );
} else {
Pattern pattern;
if ( newText.charAt( 0 ) == '/' && newText.length( ) > 1 ) {
try {
pattern = Pattern.compile( newText.substring( 1 ) ,
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE );
} catch ( final PatternSyntaxException e ) {
pattern = null;
}
} else {
pattern = null;
}
if ( pattern != null ) {
matcher = pattern.asPredicate( );
} else {
final String[] parts = newText.split( "\\s+" );
matcher = ( x ) -> {
for ( int i = 0 ; i < parts.length ; i++ ) {
if ( !x.contains( parts[ i ] ) ) {
return false;
}
}
return true;
};
}
}
final int nRecipes = fullList.size( ); final int nRecipes = fullList.size( );
for ( int i = 0 ; i < nRecipes ; i++ ) { for ( int i = 0 ; i < nRecipes ; i++ ) {
final I_CraftingRecipeWrapper recipe = fullList.get( i ); final I_CraftingRecipeWrapper recipe = fullList.get( i );
if ( I18n.format( recipe.getName( ) ).toLowerCase( ).contains( newText ) ) { if ( matcher.test( I18n.format( recipe.getName( ) ).toLowerCase( ) ) ) {
this.recipes.add( recipe ); this.recipes.add( recipe );
} }
} }