mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 22:10:54 +03:00
use AtomicReference for method parameters that return values
This commit is contained in:
@@ -45,6 +45,7 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import javax.swing.Icon;
|
||||
@@ -431,7 +432,7 @@ class UIDefaultsLoader
|
||||
enum ValueType { UNKNOWN, STRING, BOOLEAN, CHARACTER, INTEGER, INTEGERORFLOAT, FLOAT, BORDER, ICON, INSETS, DIMENSION, COLOR, FONT,
|
||||
SCALEDINTEGER, SCALEDFLOAT, SCALEDINSETS, SCALEDDIMENSION, INSTANCE, CLASS, GRAYFILTER, NULL, LAZY }
|
||||
|
||||
private static final ValueType[] tempResultValueType = new ValueType[1];
|
||||
private static final AtomicReference<ValueType> tempResultValueType = new AtomicReference<>();
|
||||
private static Map<Class<?>, ValueType> javaValueTypes;
|
||||
private static Map<String, ValueType> knownValueTypes;
|
||||
|
||||
@@ -441,7 +442,7 @@ class UIDefaultsLoader
|
||||
return parseValue( key, value, valueType, null, v -> v, Collections.emptyList() );
|
||||
}
|
||||
|
||||
static Object parseValue( String key, String value, Class<?> javaValueType, ValueType[] resultValueType,
|
||||
static Object parseValue( String key, String value, Class<?> javaValueType, AtomicReference<ValueType> resultValueType,
|
||||
Function<String, String> resolver, List<ClassLoader> addonClassLoaders )
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
@@ -450,7 +451,7 @@ class UIDefaultsLoader
|
||||
|
||||
// do not parse styles here
|
||||
if( key.startsWith( "[style]" ) ) {
|
||||
resultValueType[0] = ValueType.STRING;
|
||||
resultValueType.set( ValueType.STRING );
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -458,7 +459,7 @@ class UIDefaultsLoader
|
||||
|
||||
// null
|
||||
if( value.equals( "null" ) || value.isEmpty() ) {
|
||||
resultValueType[0] = ValueType.NULL;
|
||||
resultValueType.set( ValueType.NULL );
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -514,14 +515,14 @@ class UIDefaultsLoader
|
||||
} else {
|
||||
// false, true
|
||||
switch( value ) {
|
||||
case "false": resultValueType[0] = ValueType.BOOLEAN; return false;
|
||||
case "true": resultValueType[0] = ValueType.BOOLEAN; return true;
|
||||
case "false": resultValueType.set( ValueType.BOOLEAN ); return false;
|
||||
case "true": resultValueType.set( ValueType.BOOLEAN ); return true;
|
||||
}
|
||||
|
||||
// check for function "lazy"
|
||||
// Syntax: lazy(uiKey)
|
||||
if( value.startsWith( "lazy(" ) && value.endsWith( ")" ) ) {
|
||||
resultValueType[0] = ValueType.LAZY;
|
||||
resultValueType.set( ValueType.LAZY );
|
||||
String uiKey = StringUtils.substringTrimmed( value, 5, value.length() - 1 );
|
||||
return (LazyValue) t -> {
|
||||
return lazyUIManagerGet( uiKey );
|
||||
@@ -602,7 +603,7 @@ class UIDefaultsLoader
|
||||
}
|
||||
}
|
||||
|
||||
resultValueType[0] = valueType;
|
||||
resultValueType.set( valueType );
|
||||
|
||||
// parse value
|
||||
switch( valueType ) {
|
||||
@@ -629,14 +630,14 @@ class UIDefaultsLoader
|
||||
default:
|
||||
// string
|
||||
if( value.startsWith( "\"" ) && value.endsWith( "\"" ) ) {
|
||||
resultValueType[0] = ValueType.STRING;
|
||||
resultValueType.set( ValueType.STRING );
|
||||
return value.substring( 1, value.length() - 1 );
|
||||
}
|
||||
|
||||
// colors
|
||||
if( value.startsWith( "#" ) || value.endsWith( ")" ) ) {
|
||||
Object color = parseColorOrFunction( value, resolver );
|
||||
resultValueType[0] = (color != null) ? ValueType.COLOR : ValueType.NULL;
|
||||
resultValueType.set( (color != null) ? ValueType.COLOR : ValueType.NULL );
|
||||
return color;
|
||||
}
|
||||
|
||||
@@ -648,7 +649,7 @@ class UIDefaultsLoader
|
||||
// integer
|
||||
try {
|
||||
Integer integer = parseInteger( value );
|
||||
resultValueType[0] = ValueType.INTEGER;
|
||||
resultValueType.set( ValueType.INTEGER );
|
||||
return integer;
|
||||
} catch( NumberFormatException ex ) {
|
||||
// ignore
|
||||
@@ -657,7 +658,7 @@ class UIDefaultsLoader
|
||||
// float
|
||||
try {
|
||||
Float f = parseFloat( value );
|
||||
resultValueType[0] = ValueType.FLOAT;
|
||||
resultValueType.set( ValueType.FLOAT );
|
||||
return f;
|
||||
} catch( NumberFormatException ex ) {
|
||||
// ignore
|
||||
@@ -665,7 +666,7 @@ class UIDefaultsLoader
|
||||
}
|
||||
|
||||
// string
|
||||
resultValueType[0] = ValueType.STRING;
|
||||
resultValueType.set( ValueType.STRING );
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.prefs.Preferences;
|
||||
@@ -310,7 +311,7 @@ public class FlatUIDefaultsInspector
|
||||
Set<Entry<Object, Object>> defaultsSet = defaults.entrySet();
|
||||
ArrayList<Item> items = new ArrayList<>( defaultsSet.size() );
|
||||
HashSet<Object> keys = new HashSet<>( defaultsSet.size() );
|
||||
Color[] pBaseColor = new Color[1];
|
||||
AtomicReference<Color> pBaseColor = new AtomicReference<>();
|
||||
for( Entry<Object,Object> e : defaultsSet ) {
|
||||
Object key = e.getKey();
|
||||
|
||||
@@ -336,7 +337,7 @@ public class FlatUIDefaultsInspector
|
||||
if( value instanceof DerivedColor ) {
|
||||
Color resolvedColor = resolveDerivedColor( defaults, (String) key, (DerivedColor) value, pBaseColor );
|
||||
if( resolvedColor != value )
|
||||
info = new Color[] { resolvedColor, pBaseColor[0] };
|
||||
info = new Color[] { resolvedColor, pBaseColor.get() };
|
||||
}
|
||||
|
||||
// check whether key was overridden using UIManager.put(key,value)
|
||||
@@ -351,9 +352,9 @@ public class FlatUIDefaultsInspector
|
||||
return items.toArray( new Item[items.size()] );
|
||||
}
|
||||
|
||||
private Color resolveDerivedColor( UIDefaults defaults, String key, Color color, Color[] pBaseColor ) {
|
||||
private Color resolveDerivedColor( UIDefaults defaults, String key, Color color, AtomicReference<Color> pBaseColor ) {
|
||||
if( pBaseColor != null )
|
||||
pBaseColor[0] = null;
|
||||
pBaseColor.set( null );
|
||||
|
||||
if( !(color instanceof DerivedColor) )
|
||||
return color;
|
||||
@@ -377,7 +378,7 @@ public class FlatUIDefaultsInspector
|
||||
baseColor = resolveDerivedColor( defaults, (String) baseKey, baseColor, null );
|
||||
|
||||
if( pBaseColor != null )
|
||||
pBaseColor[0] = baseColor;
|
||||
pBaseColor.set( baseColor );
|
||||
|
||||
Color newColor = FlatUIUtils.deriveColor( color, baseColor );
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.awt.event.ComponentListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowListener;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
@@ -143,7 +144,7 @@ public class FlatWindowMaximizeTest
|
||||
}
|
||||
|
||||
private static void testMaximize( String msg, int expectedState, boolean showLater, Consumer<JFrame> testFunc ) {
|
||||
JFrame[] pFrame = new JFrame[1];
|
||||
AtomicReference<JFrame> pFrame = new AtomicReference<>();
|
||||
EventQueue.invokeLater( () -> {
|
||||
JFrame frame = new JFrame( "test" );
|
||||
frame.setName( msg );
|
||||
@@ -164,7 +165,7 @@ public class FlatWindowMaximizeTest
|
||||
testFunc.accept( frame );
|
||||
if( !showLater )
|
||||
frame.setVisible( true );
|
||||
pFrame[0] = frame;
|
||||
pFrame.set( frame );
|
||||
} );
|
||||
|
||||
try {
|
||||
@@ -172,14 +173,14 @@ public class FlatWindowMaximizeTest
|
||||
Thread.sleep( 500 );
|
||||
|
||||
EventQueue.invokeLater( () -> {
|
||||
pFrame[0].setVisible( true );
|
||||
pFrame.get().setVisible( true );
|
||||
} );
|
||||
}
|
||||
|
||||
Thread.sleep( 500 );
|
||||
|
||||
EventQueue.invokeAndWait( () -> {
|
||||
int state = pFrame[0].getExtendedState();
|
||||
int state = pFrame.get().getExtendedState();
|
||||
System.out.printf( " %-15s: %d %s\n", msg, state, (state != expectedState ? " FAILED" : "") );
|
||||
} );
|
||||
} catch( Exception ex ) {
|
||||
|
||||
@@ -645,14 +645,14 @@ public class UIDefaultsDump
|
||||
}
|
||||
|
||||
private void dumpColor( PrintWriter out, String key, Color color ) {
|
||||
Color[] retBaseColor = new Color[1];
|
||||
AtomicReference<Color> retBaseColor = new AtomicReference<>();
|
||||
Color resolvedColor = resolveDerivedColor( key, color, retBaseColor );
|
||||
if( resolvedColor != color && resolvedColor.getRGB() != color.getRGB() ) {
|
||||
if( !isIntelliJTheme ) {
|
||||
System.err.println( "Key '" + key + "': derived colors not equal" );
|
||||
System.err.println( " Default color: " + dumpColorHexAndHSL( color ) );
|
||||
System.err.println( " Resolved color: " + dumpColorHexAndHSL( resolvedColor ) );
|
||||
System.err.println( " Base color: " + dumpColorHexAndHSL( retBaseColor[0] ) );
|
||||
System.err.println( " Base color: " + dumpColorHexAndHSL( retBaseColor.get() ) );
|
||||
}
|
||||
|
||||
out.printf( "%s / ",
|
||||
@@ -891,7 +891,7 @@ public class UIDefaultsDump
|
||||
return properties;
|
||||
}
|
||||
|
||||
private Color resolveDerivedColor( String key, Color color, Color[] retBaseColor ) {
|
||||
private Color resolveDerivedColor( String key, Color color, AtomicReference<Color> retBaseColor ) {
|
||||
if( !(color instanceof DerivedColor) )
|
||||
return color;
|
||||
|
||||
@@ -914,7 +914,7 @@ public class UIDefaultsDump
|
||||
if( baseColor instanceof DerivedColor )
|
||||
baseColor = resolveDerivedColor( (String) baseKey, baseColor, retBaseColor );
|
||||
|
||||
retBaseColor[0] = baseColor;
|
||||
retBaseColor.set( baseColor );
|
||||
|
||||
Color newColor = FlatUIUtils.deriveColor( color, baseColor );
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.formdev.flatlaf;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Function;
|
||||
import com.formdev.flatlaf.UIDefaultsLoader.ValueType;
|
||||
|
||||
@@ -57,14 +58,14 @@ public class UIDefaultsLoaderAccessor
|
||||
return UIDefaultsLoader.resolveValue( value, propertiesGetter );
|
||||
}
|
||||
|
||||
public static Object parseValue( String key, String value, Object[] resultValueType,
|
||||
public static Object parseValue( String key, String value, AtomicReference<Object> resultValueType,
|
||||
Function<String, String> resolver )
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
ValueType[] resultValueType2 = new ValueType[1];
|
||||
AtomicReference<ValueType> resultValueType2 = new AtomicReference<>();
|
||||
Object result = UIDefaultsLoader.parseValue( key, value, null,
|
||||
resultValueType2, resolver, Collections.emptyList() );
|
||||
resultValueType[0] = resultValueType2[0];
|
||||
resultValueType.set( resultValueType2.get() );
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Function;
|
||||
import javax.swing.UIDefaults;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
@@ -96,7 +97,7 @@ class FlatThemePropertiesSupport
|
||||
return null;
|
||||
|
||||
try {
|
||||
Object[] resultValueType = new Object[1];
|
||||
AtomicReference<Object> resultValueType = new AtomicReference<>();
|
||||
String value = resolveValue( keyValue.value );
|
||||
parsedValue = UIDefaultsLoaderAccessor.parseValue( keyValue.key, value, resultValueType, resolver );
|
||||
parsedValueCache.put( lineKey, parsedValue );
|
||||
@@ -156,7 +157,7 @@ class FlatThemePropertiesSupport
|
||||
return null;
|
||||
|
||||
try {
|
||||
Object[] resultValueType = new Object[1];
|
||||
AtomicReference<Object> resultValueType = new AtomicReference<>();
|
||||
String value = resolveValue( str );
|
||||
parsedValue = UIDefaultsLoaderAccessor.parseValue( key, value, resultValueType, resolver );
|
||||
parsedValueCache2.put( key, parsedValue );
|
||||
|
||||
Reference in New Issue
Block a user