Styling: support specifying multiple style classes in single string

This commit is contained in:
Karl Tauber
2021-09-29 23:49:40 +02:00
parent 3d8c535ffa
commit c4b016c9c8
5 changed files with 262 additions and 10 deletions

View File

@@ -146,8 +146,8 @@ public interface FlatClientProperties
String STYLE = "FlatLaf.style";
/**
* Specifies the style class(es) of a component as String
* or as {@link java.util.List}<String>.
* Specifies the style class(es) of a component as String (single class or multiple classes separated by space characters)
* or as {@code String[]} or {@link java.util.List}<String> (multiple classes).
* <p>
* The style rules must be defined in UI defaults either as strings (in CSS syntax)
* or as {@link java.util.Map}&lt;String, Object&gt; (with binary values).

View File

@@ -127,7 +127,7 @@ public class FlatStylingSupport
* UIManager.get( "[style]Button.foo" ) );
* }</pre>
*
* @param styleClass the style class(es) either as string (single class)
* @param styleClass the style class(es) either as string (single class or multiple classes separated by space characters)
* or as {@code String[]} or {@link java.util.List}&lt;String&gt; (multiple classes)
* @param type the type of the component
* @return the styles
@@ -136,8 +136,11 @@ public class FlatStylingSupport
if( styleClass == null )
return null;
if( styleClass instanceof String && ((String)styleClass).indexOf( ' ' ) >= 0 )
styleClass = StringUtils.split( (String) styleClass, ' ', true, true );
if( styleClass instanceof String )
return getStyleForClass( (String) styleClass, type );
return getStyleForClass( ((String)styleClass).trim(), type );
else if( styleClass instanceof String[] ) {
Object style = null;
for( String cls : (String[]) styleClass )

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@@ -46,16 +47,50 @@ public class StringUtils
}
public static List<String> split( String str, char delim ) {
ArrayList<String> strs = new ArrayList<>();
return split( str, delim, false, false );
}
/**
* Splits a string at the specified delimiter.
* If trimming is enabled, then leading and trailing whitespace characters are removed.
* If excludeEmpty is {@code true}, then only non-empty strings are returned.
*
* @since 2
*/
public static List<String> split( String str, char delim, boolean trim, boolean excludeEmpty ) {
int delimIndex = str.indexOf( delim );
if( delimIndex < 0 ) {
if( trim )
str = str.trim();
return !excludeEmpty || !str.isEmpty()
? Collections.singletonList( str )
: Collections.emptyList();
}
ArrayList<String> strs = new ArrayList<>();
int index = 0;
while( delimIndex >= 0 ) {
strs.add( str.substring( index, delimIndex ) );
add( strs, str, index, delimIndex, trim, excludeEmpty );
index = delimIndex + 1;
delimIndex = str.indexOf( delim, index );
}
strs.add( str.substring( index ) );
add( strs, str, index, str.length(), trim, excludeEmpty );
return strs;
}
private static void add( List<String> strs, String str, int begin, int end, boolean trim, boolean excludeEmpty ) {
if( trim ) {
// skip leading whitespace
while( begin < end && str.charAt( begin ) <= ' ' )
begin++;
// skip trailing whitespace
while( begin < end && str.charAt( end - 1 ) <= ' ' )
end--;
}
if( !excludeEmpty || end > begin )
strs.add( str.substring( begin, end ) );
}
}