From 815d9d6012b744f7552f6689ba21974abdf53ada Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 2 Nov 2020 11:51:51 +0100 Subject: [PATCH] SplitPane: hide not applicable expand/collapse buttons (issue #198) --- CHANGELOG.md | 1 + .../formdev/flatlaf/ui/FlatSplitPaneUI.java | 78 +++++++++++++++++-- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f9a9123..c3a05f91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ FlatLaf Change Log `TitlePane.borderColor`). (issue #184) - Extras: `FlatSVGIcon` now allows specifying icon width and height in constructors. (issue #196) +- SplitPane: Hide not applicable expand/collapse buttons. (issue #198) #### Fixed bugs diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSplitPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSplitPaneUI.java index c3b33bca..a1859c01 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSplitPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSplitPaneUI.java @@ -17,7 +17,10 @@ package com.formdev.flatlaf.ui; import java.awt.Color; +import java.awt.Container; import java.awt.Cursor; +import java.awt.Insets; +import java.beans.PropertyChangeEvent; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JSplitPane; @@ -54,8 +57,8 @@ public class FlatSplitPaneUI { protected String arrowType; private Boolean continuousLayout; - private Color oneTouchArrowColor; - private Color oneTouchHoverArrowColor; + protected Color oneTouchArrowColor; + protected Color oneTouchHoverArrowColor; public static ComponentUI createUI( JComponent c ) { return new FlatSplitPaneUI(); @@ -92,6 +95,8 @@ public class FlatSplitPaneUI { protected FlatSplitPaneDivider( BasicSplitPaneUI ui ) { super( ui ); + + setLayout( new FlatDividerLayout() ); } @Override @@ -109,14 +114,42 @@ public class FlatSplitPaneUI return new FlatOneTouchButton( false ); } + @Override + public void propertyChange( PropertyChangeEvent e ) { + super.propertyChange( e ); + + switch( e.getPropertyName() ) { + case JSplitPane.DIVIDER_LOCATION_PROPERTY: + // necessary to show/hide one-touch buttons on expand/collapse + revalidate(); + break; + } + } + + protected boolean isLeftCollapsed() { + int location = splitPane.getDividerLocation(); + Insets insets = splitPane.getInsets(); + return (orientation == JSplitPane.VERTICAL_SPLIT) + ? location == insets.top + : location == insets.left; + } + + protected boolean isRightCollapsed() { + int location = splitPane.getDividerLocation(); + Insets insets = splitPane.getInsets(); + return (orientation == JSplitPane.VERTICAL_SPLIT) + ? location == (splitPane.getHeight() - getHeight() - insets.bottom) + : location == (splitPane.getWidth() - getWidth() - insets.right); + } + //---- class FlatOneTouchButton --------------------------------------- - private class FlatOneTouchButton + protected class FlatOneTouchButton extends FlatArrowButton { - private final boolean left; + protected final boolean left; - public FlatOneTouchButton( boolean left ) { + protected FlatOneTouchButton( boolean left ) { super( SwingConstants.NORTH, arrowType, oneTouchArrowColor, null, oneTouchHoverArrowColor, null ); setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) ); @@ -130,6 +163,39 @@ public class FlatSplitPaneUI : (left ? SwingConstants.WEST : SwingConstants.EAST); } } - } + //---- class FlatDividerLayout ---------------------------------------- + + protected class FlatDividerLayout + extends DividerLayout + { + @Override + public void layoutContainer( Container c ) { + super.layoutContainer( c ); + + if( leftButton == null || rightButton == null || !splitPane.isOneTouchExpandable() ) + return; + + // increase side of buttons, which makes them easier to hit by the user + // and avoids cut arrows at small divider sizes + int extraSize = UIScale.scale( 4 ); + if( orientation == JSplitPane.VERTICAL_SPLIT ) { + leftButton.setSize( leftButton.getWidth() + extraSize, leftButton.getHeight() ); + rightButton.setBounds( leftButton.getX() + leftButton.getWidth(), rightButton.getY(), + rightButton.getWidth() + extraSize, rightButton.getHeight() ); + } else { + leftButton.setSize( leftButton.getWidth(), leftButton.getHeight() + extraSize ); + rightButton.setBounds( rightButton.getX(), leftButton.getY() + leftButton.getHeight(), + rightButton.getWidth(), rightButton.getHeight() + extraSize ); + } + + // hide buttons if not applicable + boolean leftCollapsed = isLeftCollapsed(); + if( leftCollapsed ) + rightButton.setLocation( leftButton.getLocation() ); + leftButton.setVisible( !leftCollapsed ); + rightButton.setVisible( !isRightCollapsed() ); + } + } + } }