mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-08 15:00:54 +03:00
FlatLaf: allow specifying value type in value for cases where auto-detecting value type from key or value does not work
This commit is contained in:
@@ -35,6 +35,7 @@ import java.io.InputStream;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
@@ -68,6 +69,8 @@ import com.formdev.flatlaf.util.UIScale;
|
|||||||
public abstract class FlatLaf
|
public abstract class FlatLaf
|
||||||
extends BasicLookAndFeel
|
extends BasicLookAndFeel
|
||||||
{
|
{
|
||||||
|
private static final String TYPE_PREFIX = "{";
|
||||||
|
private static final String TYPE_PREFIX_END = "}";
|
||||||
private static final String VARIABLE_PREFIX = "@";
|
private static final String VARIABLE_PREFIX = "@";
|
||||||
private static final String REF_PREFIX = VARIABLE_PREFIX + "@";
|
private static final String REF_PREFIX = VARIABLE_PREFIX + "@";
|
||||||
private static final String OPTIONAL_PREFIX = "?";
|
private static final String OPTIONAL_PREFIX = "?";
|
||||||
@@ -356,6 +359,8 @@ public abstract class FlatLaf
|
|||||||
return resolveValue( properties, newValue );
|
return resolveValue( properties, newValue );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum ValueType { UNKNOWN, STRING, INTEGER, BORDER, ICON, INSETS, SIZE, COLOR, SCALEDNUMBER }
|
||||||
|
|
||||||
private Object parseValue( String key, String value, Function<String, String> resolver ) {
|
private Object parseValue( String key, String value, Function<String, String> resolver ) {
|
||||||
value = value.trim();
|
value = value.trim();
|
||||||
|
|
||||||
@@ -366,32 +371,51 @@ public abstract class FlatLaf
|
|||||||
case "true": return true;
|
case "true": return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// borders
|
ValueType valueType = ValueType.UNKNOWN;
|
||||||
|
|
||||||
|
// check whether value type is specified in the value
|
||||||
|
if( value.startsWith( TYPE_PREFIX ) ) {
|
||||||
|
int end = value.indexOf( TYPE_PREFIX_END );
|
||||||
|
if( end != -1 ) {
|
||||||
|
try {
|
||||||
|
String typeStr = value.substring( TYPE_PREFIX.length(), end );
|
||||||
|
valueType = ValueType.valueOf( typeStr.toUpperCase( Locale.ENGLISH ) );
|
||||||
|
|
||||||
|
// remove type from value
|
||||||
|
value = value.substring( end + TYPE_PREFIX_END.length() );
|
||||||
|
} catch( IllegalArgumentException ex ) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// determine value type from key
|
||||||
|
if( valueType == ValueType.UNKNOWN ) {
|
||||||
if( key.endsWith( ".border" ) || key.endsWith( "Border" ) )
|
if( key.endsWith( ".border" ) || key.endsWith( "Border" ) )
|
||||||
return parseBorder( value, resolver );
|
valueType = ValueType.BORDER;
|
||||||
|
else if( key.endsWith( ".icon" ) || key.endsWith( "Icon" ) )
|
||||||
// icons
|
valueType = ValueType.ICON;
|
||||||
if( key.endsWith( ".icon" ) || key.endsWith( "Icon" ) )
|
else if( key.endsWith( ".margin" ) || key.endsWith( ".padding" ) ||
|
||||||
return parseInstance( value );
|
|
||||||
|
|
||||||
// insets
|
|
||||||
if( key.endsWith( ".margin" ) || key.endsWith( ".padding" ) ||
|
|
||||||
key.endsWith( "Margins" ) || key.endsWith( "Insets" ) )
|
key.endsWith( "Margins" ) || key.endsWith( "Insets" ) )
|
||||||
return parseInsets( value );
|
valueType = ValueType.INSETS;
|
||||||
|
else if( key.endsWith( "Size" ) )
|
||||||
// scaled number
|
valueType = ValueType.SIZE;
|
||||||
ScaledNumber scaledNumber = parseScaledNumber( key, value );
|
else if( key.endsWith( "Width" ) || key.endsWith( "Height" ) )
|
||||||
if( scaledNumber != null )
|
valueType = ValueType.INTEGER;
|
||||||
return scaledNumber;
|
}
|
||||||
|
|
||||||
// size
|
|
||||||
if( key.endsWith( "Size" ) && !key.equals( "SplitPane.dividerSize" ))
|
|
||||||
return parseSize( value );
|
|
||||||
|
|
||||||
// width, height
|
|
||||||
if( key.endsWith( "Width" ) || key.endsWith( "Height" ) )
|
|
||||||
return parseInteger( value, true );
|
|
||||||
|
|
||||||
|
// parse value
|
||||||
|
switch( valueType ) {
|
||||||
|
case STRING: return value;
|
||||||
|
case INTEGER: return parseInteger( value, true );
|
||||||
|
case BORDER: return parseBorder( value, resolver );
|
||||||
|
case ICON: return parseInstance( value );
|
||||||
|
case INSETS: return parseInsets( value );
|
||||||
|
case SIZE: return parseSize( value );
|
||||||
|
case COLOR: return parseColor( value, true );
|
||||||
|
case SCALEDNUMBER: return parseScaledNumber( value );
|
||||||
|
case UNKNOWN:
|
||||||
|
default:
|
||||||
// colors
|
// colors
|
||||||
ColorUIResource color = parseColor( value, false );
|
ColorUIResource color = parseColor( value, false );
|
||||||
if( color != null )
|
if( color != null )
|
||||||
@@ -405,6 +429,7 @@ public abstract class FlatLaf
|
|||||||
// string
|
// string
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Object parseBorder( String value, Function<String, String> resolver ) {
|
private Object parseBorder( String value, Function<String, String> resolver ) {
|
||||||
if( value.indexOf( ',' ) >= 0 ) {
|
if( value.indexOf( ',' ) >= 0 ) {
|
||||||
@@ -493,12 +518,7 @@ public abstract class FlatLaf
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ScaledNumber parseScaledNumber( String key, String value ) {
|
private ScaledNumber parseScaledNumber( String value ) {
|
||||||
if( !key.equals( "OptionPane.buttonMinimumWidth" ) &&
|
|
||||||
!key.equals( "SplitPane.oneTouchButtonSize" ) &&
|
|
||||||
!key.equals( "SplitPane.oneTouchButtonOffset" ) )
|
|
||||||
return null; // not supported
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new ScaledNumber( Integer.parseInt( value ) );
|
return new ScaledNumber( Integer.parseInt( value ) );
|
||||||
} catch( NumberFormatException ex ) {
|
} catch( NumberFormatException ex ) {
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ OptionPane.maxCharactersPerLine=80
|
|||||||
OptionPane.iconMessageGap=16
|
OptionPane.iconMessageGap=16
|
||||||
OptionPane.messagePadding=3
|
OptionPane.messagePadding=3
|
||||||
OptionPane.buttonPadding=8
|
OptionPane.buttonPadding=8
|
||||||
OptionPane.buttonMinimumWidth=72
|
OptionPane.buttonMinimumWidth={scaledNumber}72
|
||||||
OptionPane.sameSizeButtons=true
|
OptionPane.sameSizeButtons=true
|
||||||
OptionPane.setButtonMargin=false
|
OptionPane.setButtonMargin=false
|
||||||
OptionPane.buttonOrientation=4
|
OptionPane.buttonOrientation=4
|
||||||
@@ -307,12 +307,12 @@ Spinner.editorBorderPainted=false
|
|||||||
|
|
||||||
#---- SplitPane ----
|
#---- SplitPane ----
|
||||||
|
|
||||||
SplitPane.dividerSize=5
|
SplitPane.dividerSize={integer}5
|
||||||
SplitPane.continuousLayout=true
|
SplitPane.continuousLayout=true
|
||||||
SplitPane.border=null
|
SplitPane.border=null
|
||||||
SplitPane.centerOneTouchButtons=true
|
SplitPane.centerOneTouchButtons=true
|
||||||
SplitPane.oneTouchButtonSize=6
|
SplitPane.oneTouchButtonSize={scaledNumber}6
|
||||||
SplitPane.oneTouchButtonOffset=2
|
SplitPane.oneTouchButtonOffset={scaledNumber}2
|
||||||
|
|
||||||
SplitPaneDivider.border=null
|
SplitPaneDivider.border=null
|
||||||
SplitPaneDivider.oneTouchArrowColor=@@ComboBox.buttonArrowColor
|
SplitPaneDivider.oneTouchArrowColor=@@ComboBox.buttonArrowColor
|
||||||
|
|||||||
Reference in New Issue
Block a user