HelpButton added

This commit is contained in:
Karl Tauber
2019-09-10 23:46:48 +02:00
parent daf026f8c7
commit b517f64884
13 changed files with 273 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
/*
* 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;
import java.util.Objects;
import javax.swing.JComponent;
/**
* @author Karl Tauber
*/
public interface FlatClientProperties
{
String BUTTON_TYPE = "JButton.buttonType";
String BUTTON_TYPE_HELP = "help";
/**
* Checks whether a client property of a component has the given value.
*/
static boolean clientPropertyEquals( JComponent c, String key, Object value ) {
return Objects.equals( c.getClientProperty( key ), value );
}
}

View File

@@ -0,0 +1,123 @@
/*
* 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.icons;
import static com.formdev.flatlaf.util.UIScale.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import javax.swing.UIManager;
/**
* Help button icon for {@link javax.swing.JButton}.
*
* @uiDefault Component.focusWidth int
* @uiDefault Component.focusColor Color
* @uiDefault HelpButton.borderColor Color
* @uiDefault HelpButton.disabledBorderColor Color
* @uiDefault HelpButton.focusedBorderColor Color
* @uiDefault HelpButton.background Color
* @uiDefault HelpButton.disabledBackground Color
* @uiDefault HelpButton.questionMarkColor Color
* @uiDefault HelpButton.disabledQuestionMarkColor Color
*
* @author Karl Tauber
*/
public class FlatHelpButtonIcon
extends FlatAbstractIcon
{
protected final int focusWidth = UIManager.getInt( "Component.focusWidth" );
protected final Color focusColor = UIManager.getColor( "Component.focusColor" );
protected final Color borderColor = UIManager.getColor( "HelpButton.borderColor" );
protected final Color disabledBorderColor = UIManager.getColor( "HelpButton.disabledBorderColor" );
protected final Color focusedBorderColor = UIManager.getColor( "HelpButton.focusedBorderColor" );
protected final Color background = UIManager.getColor( "HelpButton.background" );
protected final Color disabledBackground = UIManager.getColor( "HelpButton.disabledBackground" );
protected final Color questionMarkColor = UIManager.getColor( "HelpButton.questionMarkColor" );
protected final Color disabledQuestionMarkColor = UIManager.getColor( "HelpButton.disabledQuestionMarkColor" );
protected final int iconSize = 22 + (focusWidth * 2);
public FlatHelpButtonIcon() {
super( 0, 0, null );
}
@Override
protected void paintIcon( Component c, Graphics2D g2 ) {
/*
<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<circle cx="11" cy="11" r="10.5" fill="#6E6E6E"/>
<circle cx="11" cy="11" r="9.5" fill="#FFF"/>
<path fill="#6E6E6E" d="M10,17 L12,17 L12,15 L10,15 L10,17 Z M11,5 C8.8,5 7,6.8 7,9 L9,9 C9,7.9 9.9,7 11,7 C12.1,7 13,7.9 13,9 C13,11 10,10.75 10,14 L12,14 C12,11.75 15,11.5 15,9 C15,6.8 13.2,5 11,5 Z"/>
</g>
</svg>
*/
boolean enabled = c.isEnabled();
boolean focused = c.hasFocus();
// paint focused border
if( focused ) {
g2.setColor( focusColor );
g2.fill( new Ellipse2D.Float( 0.5f, 0.5f, iconSize - 1, iconSize - 1 ) );
}
// paint border
g2.setColor( enabled
? (focused ? focusedBorderColor : borderColor)
: disabledBorderColor );
g2.fill( new Ellipse2D.Float( focusWidth + 0.5f, focusWidth + 0.5f, 21, 21 ) );
// paint background
g2.setColor( enabled
? background
: disabledBackground );
g2.fill( new Ellipse2D.Float( focusWidth + 1.5f, focusWidth + 1.5f, 19, 19 ) );
// paint question mark
Path2D q = new Path2D.Float();
q.moveTo( 11, 5 );
q.curveTo( 8.8,5, 7,6.8, 7,9 );
q.lineTo( 9, 9 );
q.curveTo( 9,7.9, 9.9,7, 11,7 );
q.curveTo( 12.1,7, 13,7.9, 13,9 );
q.curveTo( 13,11, 10,10.75, 10,14 );
q.lineTo( 12, 14 );
q.curveTo( 12,11.75, 15,11.5, 15,9 );
q.curveTo( 15,6.8, 13.2,5, 11,5 );
q.closePath();
g2.translate( focusWidth, focusWidth );
g2.setColor( enabled ? questionMarkColor : disabledQuestionMarkColor );
g2.fill( q );
g2.fillRect( 10, 15, 2, 2 );
}
@Override
public int getIconWidth() {
return scale( iconSize );
}
@Override
public int getIconHeight() {
return scale( iconSize );
}
}

View File

@@ -54,7 +54,7 @@ public class FlatButtonBorder
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
if( FlatButtonUI.isContentAreaFilled( c ) )
if( FlatButtonUI.isContentAreaFilled( c ) && !FlatButtonUI.isHelpButton( c ) )
super.paintBorder( c, g, x, y, width, height );
}

View File

@@ -16,15 +16,18 @@
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.FlatClientProperties.*;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.UIManager;
@@ -59,6 +62,8 @@ public class FlatButtonUI
protected Color toolbarHoverBackground;
protected Color toolbarPressedBackground;
private Icon helpButtonIcon;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) {
@@ -81,6 +86,8 @@ public class FlatButtonUI
defaultForeground = UIManager.getColor( prefix + "default.foreground" );
toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" );
toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" );
helpButtonIcon = UIManager.getIcon( "HelpButton.icon" );
}
static boolean isContentAreaFilled( Component c ) {
@@ -91,8 +98,18 @@ public class FlatButtonUI
return c instanceof JButton && ((JButton)c).isDefaultButton();
}
static boolean isHelpButton( Component c ) {
return c instanceof JButton && clientPropertyEquals( (JButton) c, BUTTON_TYPE, BUTTON_TYPE_HELP );
}
@Override
public void update( Graphics g, JComponent c ) {
if( isHelpButton( c ) ) {
FlatUIUtils.paintParentBackground( g, c );
helpButtonIcon.paintIcon( c, g, 0, 0 );
return;
}
if( c.isOpaque() && isContentAreaFilled( c ) ) {
FlatUIUtils.paintParentBackground( g, c );
@@ -119,6 +136,9 @@ public class FlatButtonUI
@Override
protected void paintText( Graphics g, JComponent c, Rectangle textRect, String text ) {
if( isHelpButton( c ) )
return;
AbstractButton b = (AbstractButton) c;
FontMetrics fm = c.getFontMetrics( c.getFont() );
int mnemonicIndex = b.getDisplayedMnemonicIndex();
@@ -154,4 +174,12 @@ public class FlatButtonUI
boolean def = isDefaultButton( c );
return def ? defaultForeground : c.getForeground();
}
@Override
public Dimension getPreferredSize( JComponent c ) {
if( isHelpButton( c ) )
return new Dimension( helpButtonIcon.getIconWidth(), helpButtonIcon.getIconHeight() );
return super.getPreferredSize( c );
}
}

View File

@@ -141,6 +141,18 @@ FormattedTextField.background=@textComponentBackground
FormattedTextField.margin=@textComponentMargin
#---- HelpButton ----
HelpButton.icon=com.formdev.flatlaf.icons.FlatHelpButtonIcon
HelpButton.borderColor=@@CheckBox.icon.borderColor
HelpButton.disabledBorderColor=@@CheckBox.icon.disabledBorderColor
HelpButton.focusedBorderColor=@@CheckBox.icon.focusedBorderColor
HelpButton.background=@@CheckBox.icon.background
HelpButton.disabledBackground=@@CheckBox.icon.disabledBackground
HelpButton.questionMarkColor=@@CheckBox.icon.checkmarkColor
HelpButton.disabledQuestionMarkColor=@@CheckBox.icon.disabledCheckmarkColor
#---- List ----
List.border=1,0,1,0

View File

@@ -109,6 +109,11 @@ Component.focusedBorderColor=87afda
Component.focusColor=97c3f3
#---- HelpButton ----
HelpButton.questionMarkColor=4D89C9
#---- List ----
List.background=@textComponentBackground

View File

@@ -53,6 +53,8 @@ public class FlatComponentsTest
JButton button1 = new JButton();
JButton button2 = new JButton();
FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton();
JButton button3 = new JButton();
JButton button12 = new JButton();
JLabel toggleButtonLabel = new JLabel();
JToggleButton toggleButton1 = new JToggleButton();
JToggleButton toggleButton2 = new JToggleButton();
@@ -231,6 +233,17 @@ public class FlatComponentsTest
button5.setDisplayedMnemonicIndex(0);
add(button5, "cell 3 1");
//---- button3 ----
button3.setText("Help");
button3.putClientProperty("JButton.buttonType", "help");
add(button3, "cell 4 1");
//---- button12 ----
button12.setText("Help");
button12.putClientProperty("JButton.buttonType", "help");
button12.setEnabled(false);
add(button12, "cell 4 1");
//---- toggleButtonLabel ----
toggleButtonLabel.setText("JToggleButton:");
add(toggleButtonLabel, "cell 0 2");

View File

@@ -61,6 +61,21 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button3"
"text": "Help"
"$client.JButton.buttonType": "help"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button12"
"text": "Help"
"$client.JButton.buttonType": "help"
"enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "toggleButtonLabel"
"text": "JToggleButton:"

View File

@@ -95,6 +95,11 @@ Component.focusColor=97c3f3
#Component.arc=8
#---- HelpButton ----
HelpButton.questionMarkColor=0000ff
#---- Label ----
Label.foreground=008800

View File

@@ -0,0 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<circle cx="11" cy="11" r="10.5" fill="#6E6E6E"/>
<circle cx="11" cy="11" r="9.5" fill="#FFF"/>
<path fill="#6E6E6E" d="M10,17 L12,17 L12,15 L10,15 L10,17 Z M11,5 C8.8,5 7,6.8 7,9 L9,9 C9,7.9 9.9,7 11,7 C12.1,7 13,7.9 13,9 C13,11 10,10.75 10,14 L12,14 C12,11.75 15,11.5 15,9 C15,6.8 13.2,5 11,5 Z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 448 B

View File

@@ -37,6 +37,8 @@ class BasicComponentsPanel
JLabel buttonLabel = new JLabel();
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JLabel toggleButtonLabel = new JLabel();
JToggleButton toggleButton1 = new JToggleButton();
JToggleButton toggleButton2 = new JToggleButton();
@@ -161,6 +163,17 @@ class BasicComponentsPanel
button2.setEnabled(false);
add(button2, "cell 2 1");
//---- button3 ----
button3.setText("Help");
button3.putClientProperty("JButton.buttonType", "help");
add(button3, "cell 3 1");
//---- button4 ----
button4.setText("Help");
button4.putClientProperty("JButton.buttonType", "help");
button4.setEnabled(false);
add(button4, "cell 4 1");
//---- toggleButtonLabel ----
toggleButtonLabel.setText("JToggleButton:");
add(toggleButtonLabel, "cell 0 2");

View File

@@ -54,6 +54,21 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button3"
"text": "Help"
"$client.JButton.buttonType": "help"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button4"
"text": "Help"
"$client.JButton.buttonType": "help"
"enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "toggleButtonLabel"
"text": "JToggleButton:"