@ -21,12 +21,16 @@
* /
package com.github.weisj.darklaf.util ;
import java.awt.Component ;
import java.awt.Container ;
import java.awt.FocusTraversalPolicy ;
import java.awt.Font ;
import java.awt.FontMetrics ;
import java.awt.Graphics ;
import java.awt.event.MouseEvent ;
import javax.swing.JComponent ;
import javax.swing.ListSelectionModel ;
import javax.swing.SwingUtilities ;
import sun.swing.SwingUtilities2 ;
@ -35,6 +39,19 @@ public final class SwingUtil {
private SwingUtil ( ) { }
public static void drawStringUnderlineCharAt ( JComponent c , Graphics g ,
String text , int underlinedIndex , int x , int y ) {
SwingUtilities2 . drawStringUnderlineCharAt ( c , g , text , underlinedIndex , x , y ) ;
}
public static void drawString ( final JComponent c , final Graphics g , final String text , int x , int y ) {
SwingUtilities2 . drawString ( c , g , text , x , y ) ;
}
public static void setSkipClickCount ( final Component comp , int count ) {
SwingUtilities2 . setSkipClickCount ( comp , count ) ;
}
public static FontMetrics getFontMetrics ( final JComponent c , final Graphics g ) {
if ( g = = null ) throw new IllegalArgumentException ( "Graphics must not be null" ) ;
return getFontMetrics ( c , g . getFont ( ) ) ;
@ -49,15 +66,6 @@ public final class SwingUtil {
return c . getFontMetrics ( font ) ;
}
public static void drawStringUnderlineCharAt ( JComponent c , Graphics g ,
String text , int underlinedIndex , int x , int y ) {
SwingUtilities2 . drawStringUnderlineCharAt ( c , g , text , underlinedIndex , x , y ) ;
}
public static void drawString ( final JComponent c , final Graphics g , final String text , int x , int y ) {
SwingUtilities2 . drawString ( c , g , text , x , y ) ;
}
public static int stringWidth ( final JComponent c , final FontMetrics fm , final String string ) {
return SwingUtilities2 . stringWidth ( c , fm , string ) ;
}
@ -67,4 +75,54 @@ public final class SwingUtil {
| | ! SwingUtilities . isLeftMouseButton ( me )
| | me . isConsumed ( ) ;
}
public static void adjustFocus ( JComponent c ) {
if ( ! c . hasFocus ( ) & & c . isRequestFocusEnabled ( ) ) {
c . requestFocus ( ) ;
}
}
public static void compositeRequestFocus ( final Component component ) {
if ( component instanceof Container ) {
Container container = ( Container ) component ;
if ( container . isFocusCycleRoot ( ) ) {
FocusTraversalPolicy policy = container . getFocusTraversalPolicy ( ) ;
Component comp = policy . getDefaultComponent ( container ) ;
if ( comp ! = null ) {
comp . requestFocus ( ) ;
return ;
}
}
Container rootAncestor = container . getFocusCycleRootAncestor ( ) ;
if ( rootAncestor ! = null ) {
FocusTraversalPolicy policy = rootAncestor . getFocusTraversalPolicy ( ) ;
Component comp = policy . getComponentAfter ( rootAncestor , container ) ;
if ( comp ! = null & & SwingUtilities . isDescendingFrom ( comp , container ) ) {
comp . requestFocus ( ) ;
return ;
}
}
}
if ( component . isFocusable ( ) ) {
component . requestFocus ( ) ;
}
}
public static void setLeadAnchorWithoutSelection ( final ListSelectionModel model , int lead , int anchor ) {
if ( anchor = = - 1 ) {
anchor = lead ;
}
if ( lead = = - 1 ) {
model . setAnchorSelectionIndex ( - 1 ) ;
model . setLeadSelectionIndex ( - 1 ) ;
} else {
if ( model . isSelectedIndex ( lead ) ) {
model . addSelectionInterval ( lead , lead ) ;
} else {
model . removeSelectionInterval ( lead , lead ) ;
}
model . setAnchorSelectionIndex ( anchor ) ;
}
}
}