Styling: support Tree icons

This commit is contained in:
Karl Tauber
2021-06-24 13:43:19 +02:00
parent 06bc53692a
commit 4e7b0d11d0
7 changed files with 85 additions and 0 deletions

View File

@@ -37,6 +37,8 @@ public class FlatTreeClosedIcon
@Override
protected void paintIcon( Component c, Graphics2D g ) {
FlatTreeCollapsedIcon.setStyleColorFromTreeUI( c, g, ui -> ui.iconClosedColor );
/*
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<polygon fill="#6E6E6E" fill-rule="evenodd" points="1 2 6 2 8 4 15 4 15 13 1 13"/>

View File

@@ -19,7 +19,12 @@ package com.formdev.flatlaf.icons;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.util.function.Function;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.TreeUI;
import com.formdev.flatlaf.ui.FlatTreeUI;
import com.formdev.flatlaf.ui.FlatUIUtils;
/**
@@ -46,8 +51,12 @@ public class FlatTreeCollapsedIcon
@Override
protected void paintIcon( Component c, Graphics2D g ) {
setStyleColorFromTreeUI( c, g );
rotate( c, g );
String arrowType = getStyleFromTreeUI( c, ui -> ui.iconArrowType );
boolean chevron = (arrowType != null) ? FlatUIUtils.isChevron( arrowType ) : this.chevron;
if( chevron ) {
// chevron arrow
g.fill( FlatUIUtils.createPath( 3,1, 3,2.5, 6,5.5, 3,8.5, 3,10, 4.5,10, 9,5.5, 4.5,1 ) );
@@ -57,8 +66,34 @@ public class FlatTreeCollapsedIcon
}
}
void setStyleColorFromTreeUI( Component c, Graphics2D g ) {
setStyleColorFromTreeUI( c, g, ui -> ui.iconCollapsedColor );
}
void rotate( Component c, Graphics2D g ) {
if( !c.getComponentOrientation().isLeftToRight() )
g.rotate( Math.toRadians( 180 ), width / 2., height / 2. );
}
/**
* Because this icons are always shared for all trees,
* get icon specific style from FlatTreeUI.
*/
static <T> T getStyleFromTreeUI( Component c, Function<FlatTreeUI, T> f ) {
JTree tree = (c instanceof JTree)
? (JTree) c
: (JTree) SwingUtilities.getAncestorOfClass( JTree.class, c );
if( tree != null ) {
TreeUI ui = tree.getUI();
if( ui instanceof FlatTreeUI )
return f.apply( (FlatTreeUI) ui );
}
return null;
}
static void setStyleColorFromTreeUI( Component c, Graphics2D g, Function<FlatTreeUI, Color> f ) {
Color color = getStyleFromTreeUI( c, f );
if( color != null )
g.setColor( color );
}
}

View File

@@ -34,6 +34,11 @@ public class FlatTreeExpandedIcon
super( UIManager.getColor( "Tree.icon.expandedColor" ) );
}
@Override
void setStyleColorFromTreeUI( Component c, Graphics2D g ) {
setStyleColorFromTreeUI( c, g, ui -> ui.iconExpandedColor );
}
@Override
void rotate( Component c, Graphics2D g ) {
g.rotate( Math.toRadians( 90 ), width / 2., height / 2. );

View File

@@ -37,6 +37,8 @@ public class FlatTreeLeafIcon
@Override
protected void paintIcon( Component c, Graphics2D g ) {
FlatTreeCollapsedIcon.setStyleColorFromTreeUI( c, g, ui -> ui.iconLeafColor );
/*
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<g fill="none" fill-rule="evenodd">

View File

@@ -37,6 +37,8 @@ public class FlatTreeOpenIcon
@Override
protected void paintIcon( Component c, Graphics2D g ) {
FlatTreeCollapsedIcon.setStyleColorFromTreeUI( c, g, ui -> ui.iconOpenColor );
/*
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<g fill="none" fill-rule="evenodd">

View File

@@ -96,6 +96,28 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault Tree.wideSelection boolean
* @uiDefault Tree.showCellFocusIndicator boolean
*
* <!-- FlatTreeExpandedIcon -->
*
* @uiDefault Component.arrowType String chevron (default) or triangle
* @uiDefault Tree.icon.expandedColor Color
*
* <!-- FlatTreeCollapsedIcon -->
*
* @uiDefault Component.arrowType String chevron (default) or triangle
* @uiDefault Tree.icon.collapsedColor Color
*
* <!-- FlatTreeLeafIcon -->
*
* @uiDefault Tree.icon.leafColor Color
*
* <!-- FlatTreeClosedIcon -->
*
* @uiDefault Tree.icon.closedColor Color
*
* <!-- FlatTreeOpenIcon -->
*
* @uiDefault Tree.icon.openColor Color
*
* @author Karl Tauber
*/
public class FlatTreeUI
@@ -109,6 +131,15 @@ public class FlatTreeUI
@Styleable protected boolean wideSelection;
@Styleable protected boolean showCellFocusIndicator;
// for icons
// (needs to be public because icon classes are in another package)
@Styleable(dot=true) public String iconArrowType;
@Styleable(dot=true) public Color iconExpandedColor;
@Styleable(dot=true) public Color iconCollapsedColor;
@Styleable(dot=true) public Color iconLeafColor;
@Styleable(dot=true) public Color iconClosedColor;
@Styleable(dot=true) public Color iconOpenColor;
private Color defaultCellNonSelectionBackground;
private Color defaultSelectionBackground;
private Color defaultSelectionForeground;

View File

@@ -573,6 +573,14 @@ public class FlatStylingTests
ui.applyStyle( "selectionBorderColor: #fff" );
ui.applyStyle( "wideSelection: true" );
ui.applyStyle( "showCellFocusIndicator: true" );
// icons
ui.applyStyle( "icon.arrowType: chevron" );
ui.applyStyle( "icon.expandedColor: #fff" );
ui.applyStyle( "icon.collapsedColor: #fff" );
ui.applyStyle( "icon.leafColor: #fff" );
ui.applyStyle( "icon.closedColor: #fff" );
ui.applyStyle( "icon.openColor: #fff" );
}
//---- component borders --------------------------------------------------