StringUtils added

This commit is contained in:
Karl Tauber
2019-11-12 10:19:56 +01:00
parent f011468819
commit 924abde89e
6 changed files with 69 additions and 32 deletions

View File

@@ -241,10 +241,6 @@ public abstract class FlatLaf
defaults.put( "MenuItem.acceleratorFont", uiFont );
}
public static List<String> split( String str, char delim ) {
return UIDefaultsLoader.split( str, delim );
}
private static void reSetLookAndFeel() {
EventQueue.invokeLater( () -> {
try {

View File

@@ -31,6 +31,7 @@ import javax.swing.UIDefaults;
import javax.swing.plaf.ColorUIResource;
import com.formdev.flatlaf.json.Json;
import com.formdev.flatlaf.json.ParseException;
import com.formdev.flatlaf.util.StringUtils;
/**
* @author Karl Tauber
@@ -183,15 +184,14 @@ public class IntelliJTheme
boolean checkboxModified = false;
@SuppressWarnings( "unchecked" )
Map<String, Object> map = (Map<String, Object>) palette;
for( Map.Entry<String, Object> e : map.entrySet() ) {
Map<String, Object> colorPalette = (Map<String, Object>) palette;
for( Map.Entry<String, Object> e : colorPalette.entrySet() ) {
String key = e.getKey();
Object value = e.getValue();
if( !key.startsWith( "Checkbox." ) || !(value instanceof String) )
continue;
if( key.endsWith( ".Dark" ) )
key = key.substring( 0, key.length() - 5 );
key = StringUtils.removeTrailing( key, ".Dark" );
String newKey = checkboxKeyMapping.get( key );
if( newKey != null ) {

View File

@@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.text.StyleContext;
import com.formdev.flatlaf.util.StringUtils;
import com.formdev.flatlaf.util.SystemInfo;
/**
@@ -151,7 +152,7 @@ class LinuxFontPolicy
int size = 10;
if( generalFont != null ) {
List<String> strs = FlatLaf.split( generalFont, ',' );
List<String> strs = StringUtils.split( generalFont, ',' );
try {
family = strs.get( 0 );
size = Integer.parseInt( strs.get( 1 ) );

View File

@@ -39,6 +39,7 @@ import com.formdev.flatlaf.ui.FlatLineBorder;
import com.formdev.flatlaf.util.ColorFunctions;
import com.formdev.flatlaf.util.DerivedColor;
import com.formdev.flatlaf.util.ScaledNumber;
import com.formdev.flatlaf.util.StringUtils;
/**
* Load UI defaults from properties files associated to Flat LaF classes and add to UI defaults.
@@ -255,7 +256,7 @@ class UIDefaultsLoader
private static Object parseBorder( String value, Function<String, String> resolver ) {
if( value.indexOf( ',' ) >= 0 ) {
// top,left,bottom,right[,lineColor]
List<String> parts = split( value, ',' );
List<String> parts = StringUtils.split( value, ',' );
Insets insets = parseInsets( value );
ColorUIResource lineColor = (parts.size() == 5)
? parseColorOrFunction( resolver.apply( parts.get( 4 ) ), true )
@@ -282,7 +283,7 @@ class UIDefaultsLoader
}
private static Insets parseInsets( String value ) {
List<String> numbers = split( value, ',' );
List<String> numbers = StringUtils.split( value, ',' );
try {
return new InsetsUIResource(
Integer.parseInt( numbers.get( 0 ) ),
@@ -295,7 +296,7 @@ class UIDefaultsLoader
}
private static Dimension parseSize( String value ) {
List<String> numbers = split( value, ',' );
List<String> numbers = StringUtils.split( value, ',' );
try {
return new DimensionUIResource(
Integer.parseInt( numbers.get( 0 ) ),
@@ -317,8 +318,7 @@ class UIDefaultsLoader
}
private static ColorUIResource parseColor( String value, boolean reportError ) {
if( value.startsWith( "#" ) )
value = value.substring( 1 );
value = StringUtils.removeLeading( value, "#" );
int valueLength = value.length();
if( valueLength != 6 && valueLength != 8 )
@@ -351,7 +351,7 @@ class UIDefaultsLoader
}
String function = value.substring( 0, paramsStart ).trim();
List<String> params = split( value.substring( paramsStart + 1, value.length() - 1 ), ',' );
List<String> params = StringUtils.split( value.substring( paramsStart + 1, value.length() - 1 ), ',' );
if( params.isEmpty() )
throw new IllegalArgumentException( "missing parameters in function '" + value + "'" );
@@ -417,18 +417,4 @@ class UIDefaultsLoader
throw new NumberFormatException( "invalid integer '" + value + "'" );
}
}
static List<String> split( String str, char delim ) {
ArrayList<String> strs = new ArrayList<>();
int delimIndex = str.indexOf( delim );
int index = 0;
while( delimIndex >= 0 ) {
strs.add( str.substring( index, delimIndex ) );
index = delimIndex + 1;
delimIndex = str.indexOf( delim, index );
}
strs.add( str.substring( index ) );
return strs;
}
}

View File

@@ -27,7 +27,7 @@ import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicToolTipUI;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.StringUtils;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JToolTip}.
@@ -61,7 +61,7 @@ public class FlatToolTipUI
FontMetrics fm = c.getFontMetrics( c.getFont() );
Insets insets = c.getInsets();
List<String> lines = FlatLaf.split( ((JToolTip)c).getTipText(), '\n' );
List<String> lines = StringUtils.split( ((JToolTip)c).getTipText(), '\n' );
int width = 0;
int height = fm.getHeight() * Math.max( lines.size(), 1 );
for( String line : lines )
@@ -81,7 +81,7 @@ public class FlatToolTipUI
FlatUIUtils.setRenderingHints( (Graphics2D) g );
g.setColor( c.getForeground() );
List<String> lines = FlatLaf.split( ((JToolTip)c).getTipText(), '\n' );
List<String> lines = StringUtils.split( ((JToolTip)c).getTipText(), '\n' );
int x = insets.left;
int x2 = c.getWidth() - insets.right;

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.util;
import java.util.ArrayList;
import java.util.List;
/**
* Utility methods for strings.
*
* @author Karl Tauber
*/
public class StringUtils
{
public static String removeLeading( String string, String leading ) {
return string.startsWith( leading )
? string.substring( leading.length() )
: string;
}
public static String removeTrailing( String string, String trailing ) {
return string.endsWith( trailing )
? string.substring( 0, string.length() - trailing.length() )
: string;
}
public static List<String> split( String str, char delim ) {
ArrayList<String> strs = new ArrayList<>();
int delimIndex = str.indexOf( delim );
int index = 0;
while( delimIndex >= 0 ) {
strs.add( str.substring( index, delimIndex ) );
index = delimIndex + 1;
delimIndex = str.indexOf( delim, index );
}
strs.add( str.substring( index ) );
return strs;
}
}