From be23e5709d502f8ae74230763b7eab5362e0bb65 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 27 Apr 2020 11:52:11 +0200 Subject: [PATCH] Menus: support alignment and text position properties (issue #3) --- .../flatlaf/ui/FlatMenuItemRenderer.java | 145 ++++---- .../flatlaf/testing/FlatMenusTest.java | 323 +++++++++++++++++- .../formdev/flatlaf/testing/FlatMenusTest.jfd | 210 +++++++++++- 3 files changed, 613 insertions(+), 65 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java index 54ef57a1..1dec33df 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java @@ -18,6 +18,7 @@ package com.formdev.flatlaf.ui; import static com.formdev.flatlaf.util.UIScale.scale; import java.awt.Color; +import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; @@ -75,26 +76,21 @@ public class FlatMenuItemRenderer int height = 0; boolean isTopLevelMenu = isTopLevelMenu( menuItem ); - // icon size - Dimension iconSize = getIconSize(); - width += iconSize.width; - height = Math.max( iconSize.height, height ); + Rectangle viewRect = new Rectangle( 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE ); + Rectangle iconRect = new Rectangle(); + Rectangle textRect = new Rectangle(); - // gap between icon and text - if( iconSize.width > 0 ) - width += scale( menuItem.getIconTextGap() ); + // layout icon and text + SwingUtilities.layoutCompoundLabel( menuItem, + menuItem.getFontMetrics( menuItem.getFont() ), menuItem.getText(), getIconForLayout(), + menuItem.getVerticalAlignment(), menuItem.getHorizontalAlignment(), + menuItem.getVerticalTextPosition(), menuItem.getHorizontalTextPosition(), + viewRect, iconRect, textRect, scale( menuItem.getIconTextGap() ) ); - // text size - View htmlView = (View) menuItem.getClientProperty( BasicHTML.propertyKey ); - if( htmlView != null ) { - width += htmlView.getPreferredSpan( View.X_AXIS ); - height = Math.max( (int) htmlView.getPreferredSpan( View.Y_AXIS ), height ); - } else { - String text = menuItem.getText(); - FontMetrics fm = menuItem.getFontMetrics( menuItem.getFont() ); - width += SwingUtilities.computeStringWidth( fm, text ); - height = Math.max( fm.getHeight(), height ); - } + // union icon and text rectangles + Rectangle labelRect = iconRect.union( textRect ); + width += labelRect.width; + height = Math.max( labelRect.height, height ); // accelerator size String accelText = getAcceleratorText(); @@ -110,7 +106,8 @@ public class FlatMenuItemRenderer // arrow size if( !isTopLevelMenu && arrowIcon != null ) { // gap between text and arrow - width += scale( textArrowGap ); + if( accelText == null ) + width += scale( textArrowGap ); width += arrowIcon.getIconWidth(); height = Math.max( arrowIcon.getIconHeight(), height ); @@ -133,21 +130,15 @@ public class FlatMenuItemRenderer private void layout( Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle accelRect, Rectangle arrowRect ) { - // layout icon - iconRect.setSize( getIconSize() ); - iconRect.y = viewRect.y + ((viewRect.height - iconRect.height) / 2); - - // layout text - FontMetrics fm = menuItem.getFontMetrics( menuItem.getFont() ); - textRect.width = SwingUtilities.computeStringWidth( fm, menuItem.getText() ); - textRect.height = fm.getHeight(); - textRect.y = viewRect.y + ((viewRect.height - textRect.height) / 2); + boolean isTopLevelMenu = isTopLevelMenu( menuItem ); // layout arrow - Icon arrowIcon = !isTopLevelMenu( menuItem ) ? this.arrowIcon : null; - arrowRect.width = (arrowIcon != null) ? arrowIcon.getIconWidth() : 0; - arrowRect.height = (arrowIcon != null) ? arrowIcon.getIconHeight() : 0; - arrowRect.y = viewRect.y + ((viewRect.height - arrowRect.height) / 2); + if( !isTopLevelMenu && arrowIcon != null ) { + arrowRect.width = arrowIcon.getIconWidth(); + arrowRect.height = arrowIcon.getIconHeight(); + } else + arrowRect.setSize( 0, 0 ); + arrowRect.y = viewRect.y + centerOffset( viewRect.height, arrowRect.height ); // layout accelerator String accelText = getAcceleratorText(); @@ -156,27 +147,44 @@ public class FlatMenuItemRenderer accelRect.width = SwingUtilities.computeStringWidth( accelFm, accelText ); accelRect.height = accelFm.getHeight(); - accelRect.y = viewRect.y + ((viewRect.height - accelRect.height) / 2); + accelRect.y = viewRect.y + centerOffset( viewRect.height, accelRect.height ); } else accelRect.setBounds( 0, 0, 0, 0 ); + // compute horizontal positions of accelerator and arrow if( menuItem.getComponentOrientation().isLeftToRight() ) { // left-to-right - iconRect.x = viewRect.x; - textRect.x = iconRect.x + iconRect.width - + (iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0); arrowRect.x = viewRect.x + viewRect.width - arrowRect.width; - if( accelText != null ) - accelRect.x = arrowRect.x - accelRect.width; + accelRect.x = arrowRect.x - accelRect.width; } else { // right-to-left - iconRect.x = viewRect.x + viewRect.width - iconRect.width; - textRect.x = iconRect.x - textRect.width - - (iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0); arrowRect.x = viewRect.x; - if( accelText != null ) - accelRect.x = arrowRect.x + arrowRect.width; + accelRect.x = arrowRect.x + arrowRect.width; } + + // width of accelerator, arrow and gap + int accelArrowWidth = accelRect.width + arrowRect.width; + if( accelText != null ) + accelArrowWidth += scale( textAcceleratorGap ); + else if( !isTopLevelMenu && arrowIcon != null ) + accelArrowWidth += scale( textArrowGap ); + + // label rectangle is view rectangle subtracted by accelerator, arrow and gap + Rectangle labelRect = new Rectangle( viewRect ); + labelRect.width -= accelArrowWidth; + if( !menuItem.getComponentOrientation().isLeftToRight() ) + labelRect.x += accelArrowWidth; + + // layout icon and text + SwingUtilities.layoutCompoundLabel( menuItem, + menuItem.getFontMetrics( menuItem.getFont() ), menuItem.getText(), getIconForLayout(), + menuItem.getVerticalAlignment(), menuItem.getHorizontalAlignment(), + menuItem.getVerticalTextPosition(), menuItem.getHorizontalTextPosition(), + labelRect, iconRect, textRect, scale( menuItem.getIconTextGap() ) ); + } + + private static int centerOffset( int wh1, int wh2 ) { + return (wh1 != wh2) ? Math.round( (wh1 - wh2) / 2f ) : 0; } protected void paintMenuItem( Graphics g, Color selectionBackground, Color selectionForeground, @@ -206,13 +214,11 @@ public class FlatMenuItemRenderer g.setColor( Color.orange ); g.drawRect( arrowRect.x, arrowRect.y, arrowRect.width - 1, arrowRect.height - 1 ); debug*/ - boolean isTopLevelMenu = isTopLevelMenu( menuItem ); - paintBackground( g, selectionBackground ); paintIcon( g, iconRect, getIconForPainting() ); paintText( g, textRect, menuItem.getText(), selectionForeground, disabledForeground ); paintAccelerator( g, accelRect, getAcceleratorText(), acceleratorForeground, acceleratorSelectionForeground, disabledForeground ); - if( !isTopLevelMenu ) + if( !isTopLevelMenu( menuItem ) ) paintArrowIcon( g, arrowRect, arrowIcon ); } @@ -256,9 +262,9 @@ debug*/ if( icon == null ) return; - // center - int x = iconRect.x + ((iconRect.width - icon.getIconWidth()) / 2); - int y = iconRect.y + ((iconRect.height - icon.getIconHeight()) / 2); + // center because the real icon may be smaller than dimension in iconRect + int x = iconRect.x + centerOffset( iconRect.width, icon.getIconWidth() ); + int y = iconRect.y + centerOffset( iconRect.height, icon.getIconHeight() ); // paint icon.paintIcon( menuItem, g, x, y ); @@ -315,20 +321,13 @@ debug*/ return icon; } - private Dimension getIconSize() { + private Icon getIconForLayout() { if( isTopLevelMenu( menuItem ) ) { Icon icon = menuItem.getIcon(); - return (icon != null) - ? new Dimension( icon.getIconWidth(), icon.getIconHeight() ) - : new Dimension(); + return (icon != null) ? new MinSizeIcon( icon ) : null; } - Icon icon = (checkIcon != null) ? checkIcon : menuItem.getIcon(); - int iconWidth = (icon != null) ? icon.getIconWidth() : 0; - int iconHeight = (icon != null) ? icon.getIconHeight() : 0; - return new Dimension( - Math.max( iconWidth, scale( minimumIconSize.width ) ), - Math.max( iconHeight, scale( minimumIconSize.height ) ) ); + return new MinSizeIcon( (checkIcon != null) ? checkIcon : menuItem.getIcon() ); } private KeyStroke cachedAccelerator; @@ -358,4 +357,32 @@ debug*/ return cachedAcceleratorText; } + + //---- class MinSizeIcon -------------------------------------------------- + + private class MinSizeIcon + implements Icon + { + private final Icon delegate; + + MinSizeIcon( Icon delegate ) { + this.delegate = delegate; + } + + @Override + public int getIconWidth() { + int iconWidth = (delegate != null) ? delegate.getIconWidth() : 0; + return Math.max( iconWidth, scale( minimumIconSize.width ) ); + } + + @Override + public int getIconHeight() { + int iconHeight = (delegate != null) ? delegate.getIconHeight() : 0; + return Math.max( iconHeight, scale( minimumIconSize.height ) ); + } + + @Override + public void paintIcon( Component c, Graphics g, int x, int y ) { + } + } } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java index 78c75109..7070c7ed 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java @@ -18,8 +18,12 @@ package com.formdev.flatlaf.testing; import java.awt.Component; import java.awt.Container; +import java.awt.Dimension; +import java.awt.Graphics; import java.awt.event.*; +import java.util.function.Supplier; import javax.swing.*; +import com.formdev.flatlaf.util.UIScale; import net.miginfocom.swing.*; /** @@ -38,6 +42,8 @@ public class FlatMenusTest FlatMenusTest() { initComponents(); + + largerCheckBox.setSelected( LargerMenuItem.useLargerSize ); } private void armedChanged() { @@ -62,13 +68,65 @@ public class FlatMenusTest popupMenu.show( invoker, 0, invoker.getHeight() ); } + private void largerChanged() { + LargerMenuItem.useLargerSize = largerCheckBox.isSelected(); + menuBar2.revalidate(); + } + + private void accelChanged() { + updateAccel( menuBar2, () -> { + return accelCheckBox.isSelected() ? getRandomKeyStroke() : null; + } ); + } + + private void updateAccel( Component c, Supplier keyStrokeSupplier ) { + if( c instanceof JMenuItem && !(c instanceof JMenu) ) + ((JMenuItem)c).setAccelerator( keyStrokeSupplier.get() ); + + if( c instanceof Container ) { + for( Component c2 : ((Container)c).getComponents() ) + updateAccel( c2, keyStrokeSupplier ); + } + if( c instanceof JMenu ) { + randomKeyStrokeIndex = 0; + JMenu menu = (JMenu) c; + int itemCount = menu.getItemCount(); + for( int i = 0; i < itemCount; i++ ) + updateAccel( menu.getItem( i ), keyStrokeSupplier ); + } + } + + private KeyStroke getRandomKeyStroke() { + if( randomKeyStrokeIndex >= randomKeyStrokes.length ) + randomKeyStrokeIndex = 0; + return randomKeyStrokes[randomKeyStrokeIndex++]; + } + + private int randomKeyStrokeIndex = 0; + private final KeyStroke[] randomKeyStrokes = { + KeyStroke.getKeyStroke( KeyEvent.VK_F2, 0 ), + KeyStroke.getKeyStroke( KeyEvent.VK_A, KeyEvent.CTRL_MASK ), + KeyStroke.getKeyStroke( KeyEvent.VK_B, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK ), + KeyStroke.getKeyStroke( KeyEvent.VK_BACK_SPACE, 0 ), + KeyStroke.getKeyStroke( KeyEvent.VK_PAGE_UP, 0 ), + KeyStroke.getKeyStroke( KeyEvent.VK_C, KeyEvent.ALT_MASK ), + KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ), + KeyStroke.getKeyStroke( KeyEvent.VK_F10, 0 ), + KeyStroke.getKeyStroke( KeyEvent.VK_0, 0 ), + }; + private void initComponents() { // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents JLabel menuBarLabel = new JLabel(); JMenuBar menuBar1 = new JMenuBar(); JMenu menu5 = new JMenu(); + JMenuItem menuItem35 = new JMenuItem(); JMenuItem menuItem7 = new JMenuItem(); + JMenuItem menuItem34 = new JMenuItem(); JMenuItem menuItem8 = new JMenuItem(); + JMenu menu11 = new JMenu(); + JMenuItem menuItem36 = new JMenuItem(); + JMenuItem menuItem37 = new JMenuItem(); JCheckBoxMenuItem checkBoxMenuItem6 = new JCheckBoxMenuItem(); JRadioButtonMenuItem radioButtonMenuItem5 = new JRadioButtonMenuItem(); JRadioButtonMenuItem radioButtonMenuItem6 = new JRadioButtonMenuItem(); @@ -76,6 +134,34 @@ public class FlatMenusTest JMenu menu6 = new JMenu(); JMenuItem menuItem5 = new JMenuItem(); JMenuItem menuItem6 = new JMenuItem(); + menuBar2 = new JMenuBar(); + JMenu menu8 = new JMenu(); + FlatMenusTest.LargerMenuItem menuItem13 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem14 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem27 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem15 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem16 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem28 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem18 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem17 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem19 = new FlatMenusTest.LargerMenuItem(); + JMenuItem menuItem31 = new JMenuItem(); + JMenu menu9 = new JMenu(); + FlatMenusTest.LargerMenuItem menuItem20 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem21 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem29 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem22 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem23 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem30 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem25 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem24 = new FlatMenusTest.LargerMenuItem(); + FlatMenusTest.LargerMenuItem menuItem26 = new FlatMenusTest.LargerMenuItem(); + JMenuItem menuItem32 = new JMenuItem(); + JMenu menu10 = new JMenu(); + FlatMenusTest.LargerMenuItem menuItem33 = new FlatMenusTest.LargerMenuItem(); + JPanel panel5 = new JPanel(); + largerCheckBox = new JCheckBox(); + accelCheckBox = new JCheckBox(); JPanel panel1 = new JPanel(); JLabel menuLabel = new JLabel(); JMenu menu1 = new JMenu(); @@ -134,18 +220,43 @@ public class FlatMenusTest menu5.setMnemonic('T'); menu5.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess.png"))); + //---- menuItem35 ---- + menuItem35.setText("text"); + menu5.add(menuItem35); + //---- menuItem7 ---- menuItem7.setText("text"); menuItem7.setMnemonic('X'); menuItem7.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showWriteAccess.png"))); + menuItem7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J, KeyEvent.CTRL_MASK|KeyEvent.SHIFT_MASK)); menu5.add(menuItem7); + //---- menuItem34 ---- + menuItem34.setText("longer text longer text"); + menuItem34.setMnemonic('E'); + menuItem34.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0)); + menu5.add(menuItem34); + //---- menuItem8 ---- - menuItem8.setText("text"); + menuItem8.setText("longer text longer text longer"); menuItem8.setMnemonic('E'); - menuItem8.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess@2x.png"))); menu5.add(menuItem8); + //======== menu11 ======== + { + menu11.setText("sub menu"); + + //---- menuItem36 ---- + menuItem36.setText("text"); + menu11.add(menuItem36); + + //---- menuItem37 ---- + menuItem37.setText("text"); + menu11.add(menuItem37); + } + menu5.add(menu11); + menu5.addSeparator(); + //---- checkBoxMenuItem6 ---- checkBoxMenuItem6.setText("check"); checkBoxMenuItem6.setSelected(true); @@ -180,7 +291,182 @@ public class FlatMenusTest } menuBar1.add(menu6); } - add(menuBar1, "cell 1 0 4 1,growx"); + add(menuBar1, "cell 1 0 2 1,growx"); + + //======== menuBar2 ======== + { + + //======== menu8 ======== + { + menu8.setText("text position"); + menu8.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste.png"))); + menu8.setHorizontalTextPosition(SwingConstants.CENTER); + menu8.setVerticalTextPosition(SwingConstants.BOTTOM); + + //---- menuItem13 ---- + menuItem13.setText("vert top"); + menuItem13.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem13.setVerticalTextPosition(SwingConstants.TOP); + menu8.add(menuItem13); + + //---- menuItem14 ---- + menuItem14.setText("vert bottom"); + menuItem14.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem14.setVerticalTextPosition(SwingConstants.BOTTOM); + menu8.add(menuItem14); + + //---- menuItem27 ---- + menuItem27.setText("horz leading"); + menuItem27.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem27.setHorizontalTextPosition(SwingConstants.LEADING); + menu8.add(menuItem27); + + //---- menuItem15 ---- + menuItem15.setText("horz left"); + menuItem15.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem15.setHorizontalTextPosition(SwingConstants.LEFT); + menu8.add(menuItem15); + + //---- menuItem16 ---- + menuItem16.setText("horz right"); + menuItem16.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem16.setHorizontalTextPosition(SwingConstants.RIGHT); + menu8.add(menuItem16); + + //---- menuItem28 ---- + menuItem28.setText("horz trailing"); + menuItem28.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menu8.add(menuItem28); + + //---- menuItem18 ---- + menuItem18.setText("horz center / vert top"); + menuItem18.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem18.setHorizontalTextPosition(SwingConstants.CENTER); + menuItem18.setVerticalTextPosition(SwingConstants.TOP); + menu8.add(menuItem18); + + //---- menuItem17 ---- + menuItem17.setText("horz center / vert bottom"); + menuItem17.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem17.setHorizontalTextPosition(SwingConstants.CENTER); + menuItem17.setVerticalTextPosition(SwingConstants.BOTTOM); + menu8.add(menuItem17); + + //---- menuItem19 ---- + menuItem19.setText("horz center / vert center"); + menuItem19.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem19.setHorizontalTextPosition(SwingConstants.CENTER); + menu8.add(menuItem19); + + //---- menuItem31 ---- + menuItem31.setText("1234567890123456789012345678901234567890"); + menu8.add(menuItem31); + } + menuBar2.add(menu8); + + //======== menu9 ======== + { + menu9.setText("alignment"); + menu9.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste.png"))); + menu9.setHorizontalTextPosition(SwingConstants.CENTER); + menu9.setVerticalTextPosition(SwingConstants.TOP); + + //---- menuItem20 ---- + menuItem20.setText("vert top"); + menuItem20.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem20.setVerticalAlignment(SwingConstants.TOP); + menu9.add(menuItem20); + + //---- menuItem21 ---- + menuItem21.setText("vert bottom"); + menuItem21.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem21.setVerticalAlignment(SwingConstants.BOTTOM); + menu9.add(menuItem21); + + //---- menuItem29 ---- + menuItem29.setText("horz leading"); + menuItem29.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menu9.add(menuItem29); + + //---- menuItem22 ---- + menuItem22.setText("horz left"); + menuItem22.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem22.setHorizontalAlignment(SwingConstants.LEFT); + menu9.add(menuItem22); + + //---- menuItem23 ---- + menuItem23.setText("horz right"); + menuItem23.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem23.setHorizontalAlignment(SwingConstants.RIGHT); + menu9.add(menuItem23); + + //---- menuItem30 ---- + menuItem30.setText("horz trailing"); + menuItem30.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem30.setHorizontalAlignment(SwingConstants.TRAILING); + menu9.add(menuItem30); + + //---- menuItem25 ---- + menuItem25.setText("horz center / vert top"); + menuItem25.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem25.setHorizontalAlignment(SwingConstants.CENTER); + menuItem25.setVerticalAlignment(SwingConstants.TOP); + menu9.add(menuItem25); + + //---- menuItem24 ---- + menuItem24.setText("horz center / vert bottom"); + menuItem24.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem24.setHorizontalAlignment(SwingConstants.CENTER); + menuItem24.setVerticalAlignment(SwingConstants.BOTTOM); + menu9.add(menuItem24); + + //---- menuItem26 ---- + menuItem26.setText("horz center / vert center"); + menuItem26.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menuItem26.setHorizontalAlignment(SwingConstants.CENTER); + menu9.add(menuItem26); + + //---- menuItem32 ---- + menuItem32.setText("1234567890123456789012345678901234567890"); + menu9.add(menuItem32); + } + menuBar2.add(menu9); + + //======== menu10 ======== + { + menu10.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menu10.setHorizontalTextPosition(SwingConstants.CENTER); + menu10.setVerticalTextPosition(SwingConstants.TOP); + + //---- menuItem33 ---- + menuItem33.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png"))); + menu10.add(menuItem33); + } + menuBar2.add(menu10); + } + add(menuBar2, "cell 3 0 2 1,growx"); + + //======== panel5 ======== + { + panel5.setLayout(new MigLayout( + "ltr,insets 0,hidemode 3", + // columns + "[]", + // rows + "[]0" + + "[]")); + + //---- largerCheckBox ---- + largerCheckBox.setText("larger"); + largerCheckBox.addActionListener(e -> largerChanged()); + panel5.add(largerCheckBox, "cell 0 0"); + + //---- accelCheckBox ---- + accelCheckBox.setText("accel"); + accelCheckBox.addActionListener(e -> accelChanged()); + panel5.add(accelCheckBox, "cell 0 1"); + } + add(panel5, "cell 3 0 2 1"); //======== panel1 ======== { @@ -397,9 +683,14 @@ public class FlatMenusTest } // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables + private JMenuBar menuBar2; + private JCheckBox largerCheckBox; + private JCheckBox accelCheckBox; private JCheckBox armedCheckBox; // JFormDesigner - End of variables declaration //GEN-END:variables + //---- class PopupMenu ---------------------------------------------------- + private class PopupMenu extends JPopupMenu { private PopupMenu() { initComponents(); @@ -449,4 +740,30 @@ public class FlatMenusTest // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables // JFormDesigner - End of variables declaration //GEN-END:variables } + + //---- class LargerMenuItem ----------------------------------------------- + + public static class LargerMenuItem + extends JMenuItem + { + static boolean useLargerSize = true; + + @Override + public Dimension getPreferredSize() { + Dimension size = super.getPreferredSize(); + return useLargerSize + ? new Dimension( size.width + UIScale.scale( 40 ), + size.height + UIScale.scale( 30 ) ) + : size; + } + + @Override + protected void paintComponent( Graphics g ) { + super.paintComponent( g ); + + g.setColor( UIManager.getColor( "Separator.foreground" ) ); + g.drawLine( 0, 0, getWidth(), 0 ); + g.drawLine( 0, getHeight(), getWidth(), getHeight() ); + } + } } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd index d294631d..fa1eb75b 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd @@ -25,17 +25,42 @@ new FormModel { "text": "text" "mnemonic": 84 "icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess.png" ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem35" + "text": "text" + } ) add( new FormComponent( "javax.swing.JMenuItem" ) { name: "menuItem7" "text": "text" "mnemonic": 88 "icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showWriteAccess.png" ) + "accelerator": static javax.swing.KeyStroke getKeyStroke( 74, 195, false ) + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem34" + "text": "longer text longer text" + "mnemonic": 69 + "accelerator": static javax.swing.KeyStroke getKeyStroke( 115, 0, false ) } ) add( new FormComponent( "javax.swing.JMenuItem" ) { name: "menuItem8" - "text": "text" + "text": "longer text longer text longer" "mnemonic": 69 - "icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess@2x.png" ) + } ) + add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) { + name: "menu11" + "text": "sub menu" + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem36" + "text": "text" + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem37" + "text": "text" + } ) + } ) + add( new FormComponent( "javax.swing.JPopupMenu$Separator" ) { + name: "separator3" } ) add( new FormComponent( "javax.swing.JCheckBoxMenuItem" ) { name: "checkBoxMenuItem6" @@ -72,7 +97,186 @@ new FormModel { } ) } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 1 0 4 1,growx" + "value": "cell 1 0 2 1,growx" + } ) + add( new FormContainer( "javax.swing.JMenuBar", new FormLayoutManager( class javax.swing.JMenuBar ) ) { + name: "menuBar2" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) { + name: "menu8" + "text": "text position" + "icon": &SwingIcon0 new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste.png" ) + "horizontalTextPosition": 0 + "verticalTextPosition": 3 + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem13" + "text": "vert top" + "icon": &SwingIcon1 new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png" ) + "verticalTextPosition": 1 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem14" + "text": "vert bottom" + "icon": #SwingIcon1 + "verticalTextPosition": 3 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem27" + "text": "horz leading" + "icon": &SwingIcon2 new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png" ) + "horizontalTextPosition": 10 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem15" + "text": "horz left" + "icon": #SwingIcon2 + "horizontalTextPosition": 2 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem16" + "text": "horz right" + "icon": #SwingIcon2 + "horizontalTextPosition": 4 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem28" + "text": "horz trailing" + "icon": #SwingIcon2 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem18" + "text": "horz center / vert top" + "icon": #SwingIcon2 + "horizontalTextPosition": 0 + "verticalTextPosition": 1 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem17" + "text": "horz center / vert bottom" + "icon": #SwingIcon2 + "horizontalTextPosition": 0 + "verticalTextPosition": 3 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem19" + "text": "horz center / vert center" + "icon": #SwingIcon2 + "horizontalTextPosition": 0 + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem31" + "text": "1234567890123456789012345678901234567890" + } ) + } ) + add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) { + name: "menu9" + "text": "alignment" + "icon": #SwingIcon0 + "horizontalTextPosition": 0 + "verticalTextPosition": 1 + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem20" + "text": "vert top" + "icon": #SwingIcon1 + "verticalAlignment": 1 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem21" + "text": "vert bottom" + "icon": #SwingIcon1 + "verticalAlignment": 3 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem29" + "text": "horz leading" + "icon": #SwingIcon2 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem22" + "text": "horz left" + "icon": #SwingIcon2 + "horizontalAlignment": 2 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem23" + "text": "horz right" + "icon": #SwingIcon2 + "horizontalAlignment": 4 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem30" + "text": "horz trailing" + "icon": #SwingIcon2 + "horizontalAlignment": 11 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem25" + "text": "horz center / vert top" + "icon": #SwingIcon2 + "horizontalAlignment": 0 + "verticalAlignment": 1 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem24" + "text": "horz center / vert bottom" + "icon": #SwingIcon2 + "horizontalAlignment": 0 + "verticalAlignment": 3 + } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem26" + "text": "horz center / vert center" + "icon": #SwingIcon2 + "horizontalAlignment": 0 + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem32" + "text": "1234567890123456789012345678901234567890" + } ) + } ) + add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) { + name: "menu10" + "icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-menu-paste@2x.png" ) + "horizontalTextPosition": 0 + "verticalTextPosition": 1 + add( new FormComponent( "com.formdev.flatlaf.testing.FlatMenusTest$LargerMenuItem" ) { + name: "menuItem33" + "icon": #SwingIcon1 + } ) + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 3 0 2 1,growx" + } ) + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$columnConstraints": "[]" + "$rowConstraints": "[]0[]" + "$layoutConstraints": "ltr,insets 0,hidemode 3" + } ) { + name: "panel5" + add( new FormComponent( "javax.swing.JCheckBox" ) { + name: "largerCheckBox" + "text": "larger" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "largerChanged", false ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 0" + } ) + add( new FormComponent( "javax.swing.JCheckBox" ) { + name: "accelCheckBox" + "text": "accel" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "accelChanged", false ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 1" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 3 0 2 1" } ) add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { "$columnConstraints": "[125,left][fill]"