TabbedPane: support horizontal alignment of tab title and icon

This commit is contained in:
Karl Tauber
2020-11-03 20:13:14 +01:00
parent 3a784375d0
commit a7e2a10403
12 changed files with 352 additions and 95 deletions

View File

@@ -33,8 +33,12 @@ public interface FlatClientProperties
* <p>
* <strong>Components</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}<br>
* <strong>Value type</strong> {@link java.lang.String}<br>
* <strong>Allowed Values</strong> {@link #BUTTON_TYPE_SQUARE}, {@link #BUTTON_TYPE_ROUND_RECT},
* {@link #BUTTON_TYPE_TAB}, {@link #BUTTON_TYPE_HELP} and {@link BUTTON_TYPE_TOOLBAR_BUTTON}
* <strong>Allowed Values</strong>
* {@link #BUTTON_TYPE_SQUARE},
* {@link #BUTTON_TYPE_ROUND_RECT},
* {@link #BUTTON_TYPE_TAB},
* {@link #BUTTON_TYPE_HELP} or
* {@link BUTTON_TYPE_TOOLBAR_BUTTON}
*/
String BUTTON_TYPE = "JButton.buttonType";
@@ -134,9 +138,12 @@ public interface FlatClientProperties
* {@link javax.swing.JScrollPane}, {@link javax.swing.JSpinner},
* {@link javax.swing.JTextField} and {@link javax.swing.JToggleButton}<br>
* <strong>Value type</strong> {@link java.lang.String} or {@link java.awt.Color} or {@link java.awt.Color}[2]<br>
* <strong>Allowed Values</strong> {@link #OUTLINE_ERROR}, {@link #OUTLINE_WARNING},
* any color (type {@link java.awt.Color}) or an array of two colors (type {@link java.awt.Color}[2])
* where the first color is for focused state and the second for unfocused state
* <strong>Allowed Values</strong>
* {@link #OUTLINE_ERROR},
* {@link #OUTLINE_WARNING},
* any color (type {@link java.awt.Color}) or
* an array of two colors (type {@link java.awt.Color}[2]) where the first color
* is for focused state and the second for unfocused state
*/
String OUTLINE = "JComponent.outline";
@@ -316,6 +323,8 @@ public interface FlatClientProperties
* <strong>Component</strong> {@link javax.swing.JTabbedPane}
* or tab content components (see {@link javax.swing.JTabbedPane#setComponentAt(int, java.awt.Component)})<br>
* <strong>Value type</strong> {@link java.lang.String}
*
* @see #TABBED_PANE_TAB_CLOSABLE
*/
String TABBED_PANE_TAB_CLOSE_TOOLTIPTEXT = "JTabbedPane.tabCloseToolTipText";
@@ -384,48 +393,74 @@ public interface FlatClientProperties
* Specifies the alignment of the tab area.
* <p>
* <strong>Component</strong> {@link javax.swing.JTabbedPane}<br>
* <strong>Value type</strong> {@link java.lang.String}<br>
* <strong>Allowed Values</strong> {@link #TABBED_PANE_TAB_AREA_ALIGN_LEADING} (default),
* {@link #TABBED_PANE_TAB_AREA_ALIGN_TRAILING}, {@link #TABBED_PANE_TAB_AREA_ALIGN_CENTER}
* or {@link #TABBED_PANE_TAB_AREA_ALIGN_FILL}
* <strong>Value type</strong> {@link java.lang.Integer} or {@link java.lang.String}<br>
* <strong>Allowed Values</strong>
* {@link SwingConstants#LEADING} (default)
* {@link SwingConstants#TRAILING},
* {@link SwingConstants#CENTER},
* {@link #TABBED_PANE_ALIGN_LEADING} (default),
* {@link #TABBED_PANE_ALIGN_TRAILING},
* {@link #TABBED_PANE_ALIGN_CENTER} or
* {@link #TABBED_PANE_ALIGN_FILL}
*/
String TABBED_PANE_TAB_AREA_ALIGNMENT = "JTabbedPane.tabAreaAlignment";
/**
* Align the tab area to the leading edge.
*
* @see #TABBED_PANE_TAB_AREA_ALIGNMENT
* Specifies the horizontal alignment of the tab title and icon.
* <p>
* <strong>Component</strong> {@link javax.swing.JTabbedPane}
* or tab content components (see {@link javax.swing.JTabbedPane#setComponentAt(int, java.awt.Component)})<br>
* <strong>Value type</strong> {@link java.lang.Integer} or {@link java.lang.String}<br>
* <strong>Allowed Values</strong>
* {@link SwingConstants#LEADING},
* {@link SwingConstants#TRAILING},
* {@link SwingConstants#CENTER} (default),
* {@link #TABBED_PANE_ALIGN_LEADING},
* {@link #TABBED_PANE_ALIGN_TRAILING} or
* {@link #TABBED_PANE_ALIGN_CENTER} (default)
*/
String TABBED_PANE_TAB_AREA_ALIGN_LEADING = "leading";
String TABBED_PANE_TAB_ALIGNMENT = "JTabbedPane.tabAlignment";
/**
* Align the tab area to the trailing edge.
* Align to the leading edge.
*
* @see #TABBED_PANE_TAB_AREA_ALIGNMENT
* @see #TABBED_PANE_TAB_ALIGNMENT
*/
String TABBED_PANE_TAB_AREA_ALIGN_TRAILING = "trailing";
String TABBED_PANE_ALIGN_LEADING = "leading";
/**
* Align the tab area to center.
* Align to the trailing edge.
*
* @see #TABBED_PANE_TAB_AREA_ALIGNMENT
* @see #TABBED_PANE_TAB_ALIGNMENT
*/
String TABBED_PANE_TAB_AREA_ALIGN_CENTER = "center";
String TABBED_PANE_ALIGN_TRAILING = "trailing";
/**
* Stretch tabs to fill all available space.
* Align to center.
*
* @see #TABBED_PANE_TAB_AREA_ALIGNMENT
* @see #TABBED_PANE_TAB_ALIGNMENT
*/
String TABBED_PANE_ALIGN_CENTER = "center";
/**
* Stretch to fill all available space.
*
* @see #TABBED_PANE_TAB_AREA_ALIGNMENT
*/
String TABBED_PANE_TAB_AREA_ALIGN_FILL = "fill";
String TABBED_PANE_ALIGN_FILL = "fill";
/**
* Specifies how the tabs should be sized.
* <p>
* <strong>Component</strong> {@link javax.swing.JTabbedPane}<br>
* <strong>Value type</strong> {@link java.lang.String}<br>
* <strong>Allowed Values</strong> {@link #TABBED_PANE_TAB_WIDTH_MODE_PREFERRED} (default),
* {@link #TABBED_PANE_TAB_WIDTH_MODE_EQUAL} or {@link #TABBED_PANE_TAB_WIDTH_MODE_COMPACT}
* <strong>Allowed Values</strong>
* {@link #TABBED_PANE_TAB_WIDTH_MODE_PREFERRED} (default),
* {@link #TABBED_PANE_TAB_WIDTH_MODE_EQUAL} or
* {@link #TABBED_PANE_TAB_WIDTH_MODE_COMPACT}
*/
String TABBED_PANE_TAB_WIDTH_MODE = "JTabbedPane.tabWidthMode";
@@ -456,9 +491,11 @@ public interface FlatClientProperties
* <p>
* <strong>Component</strong> {@link javax.swing.JTabbedPane}<br>
* <strong>Value type</strong> {@link java.lang.Integer}<br>
* <strong>Allowed Values</strong> {@link SwingConstants#LEADING} (default),
* {@link SwingConstants#TRAILING}, {@link SwingConstants#TOP}
* or {@link SwingConstants#BOTTOM}
* <strong>Allowed Values</strong>
* {@link SwingConstants#LEADING} (default),
* {@link SwingConstants#TRAILING},
* {@link SwingConstants#TOP} or
* {@link SwingConstants#BOTTOM}
*/
String TABBED_PANE_TAB_ICON_PLACEMENT = "JTabbedPane.tabIconPlacement";
@@ -495,9 +532,10 @@ public interface FlatClientProperties
* <p>
* <strong>Component</strong> {@link javax.swing.JTextField} (and subclasses)<br>
* <strong>Value type</strong> {@link java.lang.String}<br>
* <strong>Allowed Values</strong> {@link #SELECT_ALL_ON_FOCUS_POLICY_NEVER},
* {@link #SELECT_ALL_ON_FOCUS_POLICY_ONCE} (default) or
* {@link #SELECT_ALL_ON_FOCUS_POLICY_ALWAYS}
* <strong>Allowed Values</strong>
* {@link #SELECT_ALL_ON_FOCUS_POLICY_NEVER},
* {@link #SELECT_ALL_ON_FOCUS_POLICY_ONCE} (default) or
* {@link #SELECT_ALL_ON_FOCUS_POLICY_ALWAYS}
*/
String SELECT_ALL_ON_FOCUS_POLICY = "JTextField.selectAllOnFocusPolicy";

View File

@@ -128,6 +128,7 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault TabbedPane.hasFullBorder boolean
* @uiDefault TabbedPane.hiddenTabsNavigation String moreTabsButton (default) or arrowButtons
* @uiDefault TabbedPane.tabAreaAlignment String leading (default), center, trailing or fill
* @uiDefault TabbedPane.tabAlignment String leading, center (default) or trailing
* @uiDefault TabbedPane.tabWidthMode String preferred (default), equal or compact
* @uiDefault ScrollPane.smoothScrolling boolean
* @uiDefault TabbedPane.closeIcon Icon
@@ -144,10 +145,7 @@ public class FlatTabbedPaneUI
protected static final int ARROW_BUTTONS = 1;
// tab area alignment
protected static final int ALIGN_LEADING = 0;
protected static final int ALIGN_TRAILING = 1;
protected static final int ALIGN_CENTER = 2;
protected static final int ALIGN_FILL = 3;
protected static final int FILL = 100;
protected static final int WIDTH_MODE_PREFERRED = 0;
protected static final int WIDTH_MODE_EQUAL = 1;
@@ -178,7 +176,8 @@ public class FlatTabbedPaneUI
protected boolean tabsOpaque = true;
private String hiddenTabsNavigationStr;
private String tabAreaAlignmentStr;
protected int tabAreaAlignment;
protected int tabAlignment;
private String tabWidthModeStr;
protected Icon closeIcon;
@@ -246,7 +245,8 @@ public class FlatTabbedPaneUI
hasFullBorder = UIManager.getBoolean( "TabbedPane.hasFullBorder" );
tabsOpaque = UIManager.getBoolean( "TabbedPane.tabsOpaque" );
hiddenTabsNavigationStr = UIManager.getString( "TabbedPane.hiddenTabsNavigation" );
tabAreaAlignmentStr = UIManager.getString( "TabbedPane.tabAreaAlignment" );
tabAreaAlignment = parseAlignment( UIManager.getString( "TabbedPane.tabAreaAlignment" ), LEADING );
tabAlignment = parseAlignment( UIManager.getString( "TabbedPane.tabAlignment" ), CENTER );
tabWidthModeStr = UIManager.getString( "TabbedPane.tabWidthMode" );
closeIcon = UIManager.getIcon( "TabbedPane.closeIcon" );
@@ -1003,11 +1003,12 @@ public class FlatTabbedPaneUI
}
// icon placement
int iconPlacement = clientPropertyInt( tabPane, TABBED_PANE_TAB_ICON_PLACEMENT, LEADING );
int verticalTextPosition = CENTER;
int horizontalTextPosition = TRAILING;
switch( iconPlacement ) {
case TRAILING: horizontalTextPosition = LEADING; break;
int verticalTextPosition;
int horizontalTextPosition;
switch( clientPropertyInt( tabPane, TABBED_PANE_TAB_ICON_PLACEMENT, LEADING ) ) {
default:
case LEADING: verticalTextPosition = CENTER; horizontalTextPosition = TRAILING; break;
case TRAILING: verticalTextPosition = CENTER; horizontalTextPosition = LEADING; break;
case TOP: verticalTextPosition = BOTTOM; horizontalTextPosition = CENTER; break;
case BOTTOM: verticalTextPosition = TOP; horizontalTextPosition = CENTER; break;
}
@@ -1023,7 +1024,7 @@ public class FlatTabbedPaneUI
// layout label
String clippedTitle = SwingUtilities.layoutCompoundLabel( tabPane, metrics, title, icon,
CENTER, CENTER, verticalTextPosition, horizontalTextPosition,
CENTER, getTabAlignment( tabIndex ), verticalTextPosition, horizontalTextPosition,
tabRect, iconRect, textRect, scale( textIconGapUnscaled ) );
// remove temporary client property
@@ -1164,10 +1165,23 @@ public class FlatTabbedPaneUI
}
protected int getTabAreaAlignment() {
String str = (String) tabPane.getClientProperty( TABBED_PANE_TAB_AREA_ALIGNMENT );
if( str == null )
str = tabAreaAlignmentStr;
return parseTabAreaAlignment( str );
Object value = tabPane.getClientProperty( TABBED_PANE_TAB_AREA_ALIGNMENT );
if( value instanceof Integer )
return (Integer) value;
return (value instanceof String)
? parseAlignment( (String) value, LEADING )
: tabAreaAlignment;
}
protected int getTabAlignment( int tabIndex ) {
Object value = getTabClientProperty( tabIndex, TABBED_PANE_TAB_ALIGNMENT );
if( value instanceof Integer )
return (Integer) value;
return (value instanceof String)
? parseAlignment( (String) value, CENTER )
: tabAlignment;
}
protected int getTabWidthMode() {
@@ -1188,16 +1202,16 @@ public class FlatTabbedPaneUI
}
}
protected static int parseTabAreaAlignment( String str ) {
protected static int parseAlignment( String str, int defaultValue ) {
if( str == null )
return ALIGN_LEADING;
return defaultValue;
switch( str ) {
default:
case TABBED_PANE_TAB_AREA_ALIGN_LEADING: return ALIGN_LEADING;
case TABBED_PANE_TAB_AREA_ALIGN_TRAILING: return ALIGN_TRAILING;
case TABBED_PANE_TAB_AREA_ALIGN_CENTER: return ALIGN_CENTER;
case TABBED_PANE_TAB_AREA_ALIGN_FILL: return ALIGN_FILL;
case TABBED_PANE_ALIGN_LEADING: return LEADING;
case TABBED_PANE_ALIGN_TRAILING: return TRAILING;
case TABBED_PANE_ALIGN_CENTER: return CENTER;
case TABBED_PANE_ALIGN_FILL: return FILL;
default: return defaultValue;
}
}
@@ -1972,6 +1986,7 @@ public class FlatTabbedPaneUI
case TABBED_PANE_TAB_AREA_INSETS:
case TABBED_PANE_HIDDEN_TABS_NAVIGATION:
case TABBED_PANE_TAB_AREA_ALIGNMENT:
case TABBED_PANE_TAB_ALIGNMENT:
case TABBED_PANE_TAB_WIDTH_MODE:
case TABBED_PANE_TAB_ICON_PLACEMENT:
case TABBED_PANE_TAB_CLOSABLE:
@@ -2013,6 +2028,7 @@ public class FlatTabbedPaneUI
case TABBED_PANE_MINIMUM_TAB_WIDTH:
case TABBED_PANE_MAXIMUM_TAB_WIDTH:
case TABBED_PANE_TAB_INSETS:
case TABBED_PANE_TAB_ALIGNMENT:
case TABBED_PANE_TAB_CLOSABLE:
tabPane.revalidate();
tabPane.repaint();
@@ -2161,22 +2177,22 @@ public class FlatTabbedPaneUI
int diff = availWidth - totalTabWidth;
switch( tabAreaAlignment ) {
case ALIGN_LEADING:
case LEADING:
trailingWidth += diff;
break;
case ALIGN_TRAILING:
case TRAILING:
shiftTabs( leftToRight ? diff : -diff, 0 );
leadingWidth += diff;
break;
case ALIGN_CENTER:
case CENTER:
shiftTabs( (leftToRight ? diff : -diff) / 2, 0 );
leadingWidth += diff / 2;
trailingWidth += diff - (diff / 2);
break;
case ALIGN_FILL:
case FILL:
stretchTabsWidth( diff, leftToRight );
break;
}
@@ -2220,22 +2236,22 @@ public class FlatTabbedPaneUI
int diff = availHeight - totalTabHeight;
switch( tabAreaAlignment ) {
case ALIGN_LEADING:
case LEADING:
bottomHeight += diff;
break;
case ALIGN_TRAILING:
case TRAILING:
shiftTabs( 0, diff );
topHeight += diff;
break;
case ALIGN_CENTER:
case CENTER:
shiftTabs( 0, (diff) / 2 );
topHeight += diff / 2;
bottomHeight += diff - (diff / 2);
break;
case ALIGN_FILL:
case FILL:
stretchTabsHeight( diff );
break;
}
@@ -2405,20 +2421,20 @@ public class FlatTabbedPaneUI
if( totalTabWidth < availWidth && rects.length > 0 ) {
int diff = availWidth - totalTabWidth;
switch( tabAreaAlignment ) {
case ALIGN_LEADING:
case LEADING:
trailingWidth += diff;
break;
case ALIGN_TRAILING:
case TRAILING:
leadingWidth += diff;
break;
case ALIGN_CENTER:
case CENTER:
leadingWidth += diff / 2;
trailingWidth += diff - (diff / 2);
break;
case ALIGN_FILL:
case FILL:
stretchTabsWidth( diff, leftToRight );
totalTabWidth = rectsTotalWidth( leftToRight );
break;
@@ -2492,20 +2508,20 @@ public class FlatTabbedPaneUI
if( totalTabHeight < availHeight && rects.length > 0 ) {
int diff = availHeight - totalTabHeight;
switch( tabAreaAlignment ) {
case ALIGN_LEADING:
case LEADING:
bottomHeight += diff;
break;
case ALIGN_TRAILING:
case TRAILING:
topHeight += diff;
break;
case ALIGN_CENTER:
case CENTER:
topHeight += diff / 2;
bottomHeight += diff - (diff / 2);
break;
case ALIGN_FILL:
case FILL:
stretchTabsHeight( diff );
totalTabHeight = rectsTotalHeight();
break;

View File

@@ -552,6 +552,7 @@ TabbedPane.shadow=@background
TabbedPane.contentBorderInsets=null
TabbedPane.hiddenTabsNavigation=moreTabsButton
TabbedPane.tabAreaAlignment=leading
TabbedPane.tabAlignment=center
TabbedPane.closeIcon=com.formdev.flatlaf.icons.FlatTabbedPaneCloseIcon
TabbedPane.closeSize=16,16