diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index 4304dc94..86b2482a 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -241,10 +241,6 @@ public abstract class FlatLaf defaults.put( "MenuItem.acceleratorFont", uiFont ); } - public static List split( String str, char delim ) { - return UIDefaultsLoader.split( str, delim ); - } - private static void reSetLookAndFeel() { EventQueue.invokeLater( () -> { try { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java index 068fa268..b7236c4f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java @@ -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 map = (Map) palette; - for( Map.Entry e : map.entrySet() ) { + Map colorPalette = (Map) palette; + for( Map.Entry 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 ) { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java index 15d3544c..d31c82ed 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java @@ -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 strs = FlatLaf.split( generalFont, ',' ); + List strs = StringUtils.split( generalFont, ',' ); try { family = strs.get( 0 ); size = Integer.parseInt( strs.get( 1 ) ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java index 53254d96..6c8b0f42 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java @@ -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 resolver ) { if( value.indexOf( ',' ) >= 0 ) { // top,left,bottom,right[,lineColor] - List parts = split( value, ',' ); + List 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 numbers = split( value, ',' ); + List 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 numbers = split( value, ',' ); + List 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 params = split( value.substring( paramsStart + 1, value.length() - 1 ), ',' ); + List 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 split( String str, char delim ) { - ArrayList 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; - } } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.java index 42a60609..ad3b501e 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.java @@ -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 lines = FlatLaf.split( ((JToolTip)c).getTipText(), '\n' ); + List 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 lines = FlatLaf.split( ((JToolTip)c).getTipText(), '\n' ); + List lines = StringUtils.split( ((JToolTip)c).getTipText(), '\n' ); int x = insets.left; int x2 = c.getWidth() - insets.right; diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/StringUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/StringUtils.java new file mode 100644 index 00000000..d886c119 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/StringUtils.java @@ -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 split( String str, char delim ) { + ArrayList 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; + } +}