avoid using sun.swing.SwingUtilities2

This commit is contained in:
Karl Tauber
2019-09-01 19:13:19 +02:00
parent 0211b983e6
commit 03f4cda907
5 changed files with 85 additions and 7 deletions

View File

@@ -29,7 +29,6 @@ import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;
import sun.swing.SwingUtilities2;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JButton}.
@@ -109,11 +108,11 @@ public class FlatButtonUI
@Override
protected void paintText( Graphics g, JComponent c, Rectangle textRect, String text ) {
AbstractButton b = (AbstractButton) c;
FontMetrics fm = SwingUtilities2.getFontMetrics( c, g );
FontMetrics fm = c.getFontMetrics( c.getFont() );
int mnemonicIndex = b.getDisplayedMnemonicIndex();
g.setColor( b.getModel().isEnabled() ? getForeground( c ) : disabledText );
SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex,
FlatUIUtils.drawStringUnderlineCharAt( c, g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset() );
}

View File

@@ -23,7 +23,6 @@ import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicLabelUI;
import sun.swing.SwingUtilities2;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JLabel}.
@@ -59,6 +58,6 @@ public class FlatLabelUI
protected void paintDisabledText( JLabel l, Graphics g, String s, int textX, int textY ) {
int mnemIndex = l.getDisplayedMnemonicIndex();
g.setColor( disabledForeground );
SwingUtilities2.drawStringUnderlineCharAt( l, g, s, mnemIndex, textX, textY );
FlatUIUtils.drawStringUnderlineCharAt( l, g, s, mnemIndex, textX, textY );
}
}

View File

@@ -33,7 +33,6 @@ import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JTabbedPane}.
@@ -261,7 +260,7 @@ public class FlatTabbedPaneUI
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt( tabIndex );
g.setColor( color );
SwingUtilities2.drawStringUnderlineCharAt( tabPane, g, title, mnemIndex,
FlatUIUtils.drawStringUnderlineCharAt( tabPane, g, title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent() );
}

View File

@@ -27,6 +27,7 @@ import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.UIManager;
import com.formdev.flatlaf.util.JavaCompatibility;
import com.formdev.flatlaf.util.UIScale;
/**
@@ -156,4 +157,13 @@ public class FlatUIUtils
rect.closePath();
return rect;
}
/**
* Replacement for SwingUtilities2.drawStringUnderlineCharAt()
*/
public static void drawStringUnderlineCharAt( JComponent c, Graphics g,
String text, int underlinedIndex, int x, int y )
{
JavaCompatibility.drawStringUnderlineCharAt( c, g, text, underlinedIndex, x, y );
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.awt.Graphics;
import java.awt.Graphics2D;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JComponent;
/**
* Provides Java version compatibility methods.
*
* WARNING: This is private API and may change.
*
* @author Karl Tauber
*/
public class JavaCompatibility
{
private static Method drawStringUnderlineCharAtMethod;
/**
* Java 8: sun.swing.SwingUtilities2.drawStringUnderlineCharAt( JComponent c,
* Graphics g, String text, int underlinedIndex, int x, int y )
* Java 9: javax.swing.plaf.basic.BasicGraphicsUtils.drawStringUnderlineCharAt( JComponent c,
* Graphics2D g, String string, int underlinedIndex, float x, float y )
*/
public static void drawStringUnderlineCharAt( JComponent c, Graphics g,
String text, int underlinedIndex, int x, int y )
{
synchronized( JavaCompatibility.class ) {
if( drawStringUnderlineCharAtMethod == null ) {
try {
Class<?> cls = Class.forName( SystemInfo.IS_JAVA_9_OR_LATER
? "javax.swing.plaf.basic.BasicGraphicsUtils"
: "sun.swing.SwingUtilities2" );
drawStringUnderlineCharAtMethod = cls.getMethod( "drawStringUnderlineCharAt", SystemInfo.IS_JAVA_9_OR_LATER
? new Class[] { JComponent.class, Graphics2D.class, String.class, int.class, float.class, float.class }
: new Class[] { JComponent.class, Graphics.class, String.class, int.class, int.class, int.class } );
} catch( Exception ex ) {
ex.printStackTrace();
throw new RuntimeException( ex );
}
}
}
try {
if( SystemInfo.IS_JAVA_9_OR_LATER )
drawStringUnderlineCharAtMethod.invoke( null, c, g, text, underlinedIndex, (float) x, (float) y );
else
drawStringUnderlineCharAtMethod.invoke( null, c, g, text, underlinedIndex, x, y );
} catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException ex ) {
ex.printStackTrace();
throw new RuntimeException( ex );
}
}
}