下面列出了怎么用javax.swing.UIDefaults.LazyValue的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Object createValue( UIDefaults table ) {
// get base input map
InputMap inputMap = (baseInputMap instanceof LazyValue)
? (InputMap) ((LazyValue)baseInputMap).createValue( table )
: (InputMap) baseInputMap;
// modify input map (replace or remove)
for( int i = 0; i < bindings.length; i += 2 ) {
KeyStroke keyStroke = KeyStroke.getKeyStroke( (String) bindings[i] );
if( bindings[i + 1] != null )
inputMap.put( keyStroke, bindings[i + 1] );
else
inputMap.remove( keyStroke );
}
return inputMap;
}
private static Object parseBorder( String value, Function<String, String> resolver, List<ClassLoader> addonClassLoaders ) {
if( value.indexOf( ',' ) >= 0 ) {
// top,left,bottom,right[,lineColor[,lineThickness]]
List<String> parts = split( value, ',' );
Insets insets = parseInsets( value );
ColorUIResource lineColor = (parts.size() >= 5)
? (ColorUIResource) parseColorOrFunction( resolver.apply( parts.get( 4 ) ), resolver, true )
: null;
float lineThickness = (parts.size() >= 6) ? parseFloat( parts.get( 5 ), true ) : 1f;
return (LazyValue) t -> {
return (lineColor != null)
? new FlatLineBorder( insets, lineColor, lineThickness )
: new FlatEmptyBorder( insets );
};
} else
return parseInstance( value, addonClassLoaders );
}
private void dumpValue( PrintWriter out, Object value ) {
if( value == null ||
value instanceof String ||
value instanceof Number ||
value instanceof Boolean )
{
out.print( value );
} else if( value instanceof Character ) {
char ch = ((Character)value).charValue();
if( ch >= ' ' && ch <= '~' )
out.printf( "'%c'", value );
else
out.printf( "'\\u%h'", (int) ch );
} else if( value.getClass().isArray() )
dumpArray( out, value );
else if( value instanceof List )
dumpList( out, (List<?>) value );
else if( value instanceof Color )
dumpColor( out, (Color) value );
else if( value instanceof Font )
dumpFont( out, (Font) value );
else if( value instanceof Insets )
dumpInsets( out, (Insets) value );
else if( value instanceof Dimension )
dumpDimension( out, (Dimension) value );
else if( value instanceof Border )
dumpBorder( out, (Border) value, null );
else if( value instanceof Icon )
dumpIcon( out, (Icon) value );
else if( value instanceof ListCellRenderer )
dumpListCellRenderer( out, (ListCellRenderer<?>) value );
else if( value instanceof InputMap )
dumpInputMap( out, (InputMap) value, null );
else if( value instanceof LazyValue )
dumpLazyValue( out, (LazyValue) value );
else if( value instanceof ActiveValue )
dumpActiveValue( out, (ActiveValue) value );
else
out.printf( "[unknown type] %s", dumpClass( value ) );
}
private static Object parseInstance( String value, List<ClassLoader> addonClassLoaders ) {
return (LazyValue) t -> {
try {
return findClass( value, addonClassLoaders ).newInstance();
} catch( InstantiationException | IllegalAccessException | ClassNotFoundException ex ) {
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to instantiate '" + value + "'.", ex );
return null;
}
};
}
private static Object parseClass( String value, List<ClassLoader> addonClassLoaders ) {
return (LazyValue) t -> {
try {
return findClass( value, addonClassLoaders );
} catch( ClassNotFoundException ex ) {
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to find class '" + value + "'.", ex );
return null;
}
};
}
private static Object parseGrayFilter( String value ) {
List<String> numbers = split( value, ',' );
try {
int brightness = Integer.parseInt( numbers.get( 0 ) );
int contrast = Integer.parseInt( numbers.get( 1 ) );
int alpha = Integer.parseInt( numbers.get( 2 ) );
return (LazyValue) t -> {
return new GrayFilter( brightness, contrast, alpha );
};
} catch( NumberFormatException ex ) {
throw new IllegalArgumentException( "invalid gray filter '" + value + "'" );
}
}
private void dumpLazyValue( PrintWriter out, LazyValue value ) {
out.print( "[lazy] " );
dumpValue( out, value.createValue( defaults ) );
}
/**
* Syntax: lighten(color,amount[,options]) or darken(color,amount[,options]) or
* saturate(color,amount[,options]) or desaturate(color,amount[,options])
* - color: a color (e.g. #f00) or a color function
* - amount: percentage 0-100%
* - options: [relative] [autoInverse] [noAutoInverse] [lazy] [derived]
*/
private static Object parseColorHSLIncreaseDecrease( int hslIndex, boolean increase,
List<String> params, Function<String, String> resolver, boolean reportError )
{
String colorStr = params.get( 0 );
int amount = parsePercentage( params.get( 1 ) );
boolean relative = false;
boolean autoInverse = false;
boolean lazy = false;
boolean derived = false;
if( params.size() > 2 ) {
String options = params.get( 2 );
relative = options.contains( "relative" );
autoInverse = options.contains( "autoInverse" );
lazy = options.contains( "lazy" );
derived = options.contains( "derived" );
// use autoInverse by default for derived colors, except if noAutoInverse is set
if( derived && !options.contains( "noAutoInverse" ) )
autoInverse = true;
}
// create function
ColorFunction function = new ColorFunctions.HSLIncreaseDecrease(
hslIndex, increase, amount, relative, autoInverse );
if( lazy ) {
return (LazyValue) t -> {
Object color = lazyUIManagerGet( colorStr );
return (color instanceof Color)
? new ColorUIResource( ColorFunctions.applyFunctions( (Color) color, function ) )
: null;
};
}
// parse base color
String resolvedColorStr = resolver.apply( colorStr );
ColorUIResource baseColor = (ColorUIResource) parseColorOrFunction( resolvedColorStr, resolver, reportError );
// apply this function to base color
Color newColor = ColorFunctions.applyFunctions( baseColor, function );
if( derived ) {
ColorFunction[] functions;
if( baseColor instanceof DerivedColor && resolvedColorStr == colorStr ) {
// if the base color is also derived, join the color functions
// but only if base color function is specified directly in this function
ColorFunction[] baseFunctions = ((DerivedColor)baseColor).getFunctions();
functions = new ColorFunction[baseFunctions.length + 1];
System.arraycopy( baseFunctions, 0, functions, 0, baseFunctions.length );
functions[baseFunctions.length] = function;
} else
functions = new ColorFunction[] { function };
return new DerivedColor( newColor, functions );
}
return new ColorUIResource( newColor );
}