From 6d39c1e565b1d3ba5267309d4b1f9fecdb5b940a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 23 Jul 2016 10:08:21 +0200 Subject: [PATCH] Workbench - Improved search box Can now find exact matches using = or use regexp if starting with / --- src/java/mmm/tech/base/workbench/TBWBGui.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/java/mmm/tech/base/workbench/TBWBGui.java b/src/java/mmm/tech/base/workbench/TBWBGui.java index f556343..1325443 100644 --- a/src/java/mmm/tech/base/workbench/TBWBGui.java +++ b/src/java/mmm/tech/base/workbench/TBWBGui.java @@ -4,6 +4,9 @@ package mmm.tech.base.workbench; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.function.Predicate; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import org.lwjgl.input.Keyboard; @@ -224,10 +227,42 @@ public class TBWBGui 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( ); for ( int i = 0 ; i < nRecipes ; 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 ); } }