MenuBar basic implementation

This commit is contained in:
Karl Tauber
2019-08-31 14:24:46 +02:00
parent 611296037c
commit 9b1214572b
8 changed files with 220 additions and 16 deletions

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Rectangle2D;
import javax.swing.JMenuBar;
import javax.swing.UIManager;
/**
* Border for {@link javax.swing.JMenuBar}.
*
* @uiDefault MenuBar.borderColor Color
*
* @author Karl Tauber
*/
public class FlatMenuBarBorder
extends FlatMarginBorder
{
private final Color borderColor = UIManager.getColor( "MenuBar.borderColor" );
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
Graphics2D g2 = (Graphics2D) g.create();
try {
float lineHeight = scale( (float) 1 );
FlatUIUtils.setRenderingHints( g2 );
g2.setColor( borderColor );
g2.fill( new Rectangle2D.Float( x, y + height - lineHeight, width, lineHeight ) );
} finally {
g2.dispose();
}
}
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
// BasicBorders.MarginBorder does not handle JMenuBar margin
Insets margin = (c instanceof JMenuBar) ? ((JMenuBar)c).getMargin() : new Insets( 0, 0, 0, 0 );
insets.top = scale( margin.top );
insets.left = scale( margin.left );
insets.bottom = scale( margin.bottom + 1 );
insets.right = scale( margin.right );
return insets;
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicMenuBarUI;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JMenuBar}.
*
* @author Karl Tauber
*/
public class FlatMenuBarUI
extends BasicMenuBarUI
{
public static ComponentUI createUI( JComponent c ) {
return new FlatMenuBarUI();
}
}

View File

@@ -111,6 +111,11 @@ Menu.icon.arrowColor=A7A7A7
Menu.icon.disabledArrowColor=606060
#---- MenuBar ----
MenuBar.borderColor=515151
#---- MenuItemCheckBox ----
MenuItemCheckBox.icon.checkmarkColor=A7A7A7

View File

@@ -24,6 +24,7 @@ EditorPaneUI=com.formdev.flatlaf.ui.FlatEditorPaneUI
FormattedTextFieldUI=com.formdev.flatlaf.ui.FlatFormattedTextFieldUI
LabelUI=com.formdev.flatlaf.ui.FlatLabelUI
MenuUI=com.formdev.flatlaf.ui.FlatMenuUI
MenuBarUI=com.formdev.flatlaf.ui.FlatMenuBarUI
MenuItemUI=com.formdev.flatlaf.ui.FlatMenuItemUI
PasswordFieldUI=com.formdev.flatlaf.ui.FlatPasswordFieldUI
ProgressBarUI=com.formdev.flatlaf.ui.FlatProgressBarUI
@@ -104,6 +105,11 @@ Menu.border=com.formdev.flatlaf.ui.FlatMarginBorder
Menu.arrowIcon=com.formdev.flatlaf.icons.FlatMenuArrowIcon
#---- MenuBar ----
MenuBar.border=com.formdev.flatlaf.ui.FlatMenuBarBorder
#---- MenuItem ----
MenuItem.border=com.formdev.flatlaf.ui.FlatMarginBorder

View File

@@ -111,6 +111,11 @@ Menu.icon.arrowColor=666666
Menu.icon.disabledArrowColor=ABABAB
#---- MenuBar ----
MenuBar.borderColor=cdcdcd
#---- MenuItemCheckBox ----
MenuItemCheckBox.icon.checkmarkColor=4D89C9

View File

@@ -54,6 +54,14 @@ public class FlatMenusTest
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 menuItem7 = new JMenuItem();
JMenuItem menuItem8 = new JMenuItem();
JMenu menu6 = new JMenu();
JMenuItem menuItem5 = new JMenuItem();
JMenuItem menuItem6 = new JMenuItem();
JPanel panel1 = new JPanel();
JLabel menuLabel = new JLabel();
JMenu menu1 = new JMenu();
@@ -84,20 +92,60 @@ public class FlatMenusTest
setLayout(new MigLayout(
"insets 0,hidemode 3,gap 5 5,ltr",
// columns
"[125]" +
"[]" +
"[]" +
"[]" +
"[]",
// rows
"[]" +
"[top]" +
"[]" +
"[]"));
//---- menuBarLabel ----
menuBarLabel.setText("JMenuBar:");
add(menuBarLabel, "cell 0 0");
//======== menuBar1 ========
{
//======== menu5 ========
{
menu5.setText("text");
//---- menuItem7 ----
menuItem7.setText("text");
menu5.add(menuItem7);
//---- menuItem8 ----
menuItem8.setText("text");
menu5.add(menuItem8);
}
menuBar1.add(menu5);
//======== menu6 ========
{
menu6.setText("text");
//---- menuItem5 ----
menuItem5.setText("text");
menu6.add(menuItem5);
//---- menuItem6 ----
menuItem6.setText("text");
menu6.add(menuItem6);
}
menuBar1.add(menu6);
}
add(menuBar1, "cell 1 0 4 1,growx");
//======== panel1 ========
{
panel1.setLayout(new MigLayout(
"insets 0,hidemode 3,gap 5 5,ltr",
// columns
"[left]" +
"[125,left]" +
"[fill]",
// rows
"[]" +
@@ -142,12 +190,12 @@ public class FlatMenusTest
radioButtonMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
panel1.add(radioButtonMenuItem1, "cell 1 3");
}
add(panel1, "cell 0 0");
add(panel1, "cell 0 1 2 1");
//======== panel2 ========
{
panel2.setLayout(new MigLayout(
"insets 0,hidemode 3,gap 5 5",
"insets 0,gap 5 5",
// columns
"[fill]",
// rows
@@ -181,7 +229,7 @@ public class FlatMenusTest
radioButtonMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
panel2.add(radioButtonMenuItem2, "cell 0 3");
}
add(panel2, "cell 1 0");
add(panel2, "cell 2 1");
//======== panel3 ========
{
@@ -220,7 +268,7 @@ public class FlatMenusTest
radioButtonMenuItem3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
panel3.add(radioButtonMenuItem3, "cell 0 3");
}
add(panel3, "cell 2 0");
add(panel3, "cell 3 1");
//======== panel4 ========
{
@@ -262,13 +310,13 @@ public class FlatMenusTest
radioButtonMenuItem4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
panel4.add(radioButtonMenuItem4, "cell 0 3");
}
add(panel4, "cell 3 0");
add(panel4, "cell 4 1");
//---- armedCheckBox ----
armedCheckBox.setText("armed");
armedCheckBox.setMnemonic('A');
armedCheckBox.addActionListener(e -> armedChanged());
add(armedCheckBox, "cell 0 1");
add(armedCheckBox, "cell 0 2 2 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

View File

@@ -8,12 +8,47 @@ new FormModel {
}
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets 0,hidemode 3,gap 5 5,ltr"
"$columnConstraints": "[][][][]"
"$rowConstraints": "[top][]"
"$columnConstraints": "[125][][][][]"
"$rowConstraints": "[][top][][]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
name: "menuBarLabel"
"text": "JMenuBar:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormContainer( "javax.swing.JMenuBar", new FormLayoutManager( class javax.swing.JMenuBar ) ) {
name: "menuBar1"
add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) {
name: "menu5"
"text": "text"
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem7"
"text": "text"
} )
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem8"
"text": "text"
} )
} )
add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) {
name: "menu6"
"text": "text"
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem5"
"text": "text"
} )
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem6"
"text": "text"
} )
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0 4 1,growx"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[left][fill]"
"$columnConstraints": "[125,left][fill]"
"$rowConstraints": "[][][][]"
"$layoutConstraints": "insets 0,hidemode 3,gap 5 5,ltr"
} ) {
@@ -70,12 +105,12 @@ new FormModel {
"value": "cell 1 3"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
"value": "cell 0 1 2 1"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[fill]"
"$rowConstraints": "[][][][]"
"$layoutConstraints": "insets 0,hidemode 3,gap 5 5"
"$layoutConstraints": "insets 0,gap 5 5"
} ) {
name: "panel2"
add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) {
@@ -110,7 +145,7 @@ new FormModel {
"value": "cell 0 3"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
"value": "cell 2 1"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[fill]"
@@ -150,7 +185,7 @@ new FormModel {
"value": "cell 0 3"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 0"
"value": "cell 3 1"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[fill]"
@@ -193,7 +228,7 @@ new FormModel {
"value": "cell 0 3"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 0"
"value": "cell 4 1"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "armedCheckBox"
@@ -204,7 +239,7 @@ new FormModel {
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "armedChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
"value": "cell 0 2 2 1"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )

View File

@@ -110,6 +110,11 @@ Menu.icon.arrowColor=4D89C9
Menu.icon.disabledArrowColor=ABABAB
#---- MenuBar ----
MenuBar.borderColor=4444ff
#---- MenuItemCheckBox ----
MenuItemCheckBox.icon.checkmarkColor=4D89C9