From b52250037942ca107d9a8aeafde306f7cc2e91f8 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 17 Feb 2020 22:39:16 +0100 Subject: [PATCH] TextField, FormattedTextField and PasswordField: support round border (issue #65) --- CHANGELOG.md | 6 +++ .../flatlaf/ui/FlatFormattedTextFieldUI.java | 1 + .../flatlaf/ui/FlatPasswordFieldUI.java | 5 ++- .../formdev/flatlaf/ui/FlatTextBorder.java | 39 +++++++++++++++++++ .../formdev/flatlaf/ui/FlatTextFieldUI.java | 17 +++++--- .../com/formdev/flatlaf/FlatLaf.properties | 7 ++-- 6 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextBorder.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 658ae0c19..926a6eeb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ FlatLaf Change Log ================== +## Unreleased + +- TextField, FormattedTextField and PasswordField: Support round borders (see UI + default value `TextComponent.arc`). (issue #65) + + ## 0.27 - Support `JInternalFrame` and `JDesktopPane`. (issues #39 and #11) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFormattedTextFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFormattedTextFieldUI.java index 8322ba641..917726889 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFormattedTextFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatFormattedTextFieldUI.java @@ -39,6 +39,7 @@ * * * + * @uiDefault TextComponent.arc int * @uiDefault Component.focusWidth int * @uiDefault Component.minimumWidth int * @uiDefault Component.isIntelliJTheme boolean diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java index 76a04a62a..f0f6184f0 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java @@ -51,6 +51,7 @@ * * * + * @uiDefault TextComponent.arc int * @uiDefault Component.focusWidth int * @uiDefault Component.minimumWidth int * @uiDefault Component.isIntelliJTheme boolean @@ -62,6 +63,7 @@ public class FlatPasswordFieldUI extends BasicPasswordFieldUI { + protected int arc; protected int focusWidth; protected int minimumWidth; protected boolean isIntelliJTheme; @@ -78,6 +80,7 @@ protected void installDefaults() { super.installDefaults(); String prefix = getPropertyPrefix(); + arc = UIManager.getInt( "TextComponent.arc" ); focusWidth = UIManager.getInt( "Component.focusWidth" ); minimumWidth = UIManager.getInt( "Component.minimumWidth" ); isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" ); @@ -128,7 +131,7 @@ protected void propertyChange( PropertyChangeEvent e ) { @Override protected void paintSafely( Graphics g ) { - FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth, isIntelliJTheme ); + FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth, arc, isIntelliJTheme ); FlatTextFieldUI.paintPlaceholder( g, getComponent(), placeholderForeground ); super.paintSafely( g ); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextBorder.java new file mode 100644 index 000000000..2e24358ca --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextBorder.java @@ -0,0 +1,39 @@ +/* + * 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.Component; +import javax.swing.UIManager; + +/** + * Border for various text components (e.g. {@link javax.swing.JTextField}). + * + * @uiDefault Component.arc int + * + * @author Karl Tauber + */ +public class FlatTextBorder + extends FlatBorder +{ + protected final int arc = UIManager.getInt( "TextComponent.arc" ); + + @Override + protected float getArc( Component c ) { + return scale( (float) arc ); + } +} diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java index 35deedbb0..e566b07b5 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java @@ -32,6 +32,7 @@ import javax.swing.JTextField; import javax.swing.LookAndFeel; import javax.swing.UIManager; +import javax.swing.border.Border; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.UIResource; import javax.swing.plaf.basic.BasicTextFieldUI; @@ -59,6 +60,7 @@ * * * + * @uiDefault TextComponent.arc int * @uiDefault Component.focusWidth int * @uiDefault Component.minimumWidth int * @uiDefault Component.isIntelliJTheme boolean @@ -70,6 +72,7 @@ public class FlatTextFieldUI extends BasicTextFieldUI { + protected int arc; protected int focusWidth; protected int minimumWidth; protected boolean isIntelliJTheme; @@ -86,6 +89,7 @@ protected void installDefaults() { super.installDefaults(); String prefix = getPropertyPrefix(); + arc = UIManager.getInt( "TextComponent.arc" ); focusWidth = UIManager.getInt( "Component.focusWidth" ); minimumWidth = UIManager.getInt( "Component.minimumWidth" ); isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" ); @@ -136,7 +140,7 @@ protected void propertyChange( PropertyChangeEvent e ) { @Override protected void paintSafely( Graphics g ) { - paintBackground( g, getComponent(), focusWidth, isIntelliJTheme ); + paintBackground( g, getComponent(), focusWidth, arc, isIntelliJTheme ); paintPlaceholder( g, getComponent(), placeholderForeground ); super.paintSafely( g ); } @@ -146,13 +150,15 @@ protected void paintBackground( Graphics g ) { // background is painted elsewhere } - static void paintBackground( Graphics g, JTextComponent c, int focusWidth, boolean isIntelliJTheme ) { + static void paintBackground( Graphics g, JTextComponent c, int focusWidth, int arc, boolean isIntelliJTheme ) { + Border border = c.getBorder(); + // do not paint background if: // - not opaque and // - border is not a flat border and // - opaque was explicitly set (to false) // (same behaviour as in AquaTextFieldUI) - if( !c.isOpaque() && !(c.getBorder() instanceof FlatBorder) && FlatUIUtils.hasOpaqueBeenExplicitlySet( c ) ) + if( !c.isOpaque() && !(border instanceof FlatBorder) && FlatUIUtils.hasOpaqueBeenExplicitlySet( c ) ) return; // fill background if opaque to avoid garbage if user sets opaque to true @@ -164,7 +170,8 @@ static void paintBackground( Graphics g, JTextComponent c, int focusWidth, boole try { FlatUIUtils.setRenderingHints( g2 ); - float fFocusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) focusWidth ) : 0; + float fFocusWidth = (border instanceof FlatBorder) ? scale( (float) focusWidth ) : 0; + float fArc = (border instanceof FlatTextBorder) ? scale( (float) arc ) : 0; Color background = c.getBackground(); g2.setColor( !(background instanceof UIResource) @@ -172,7 +179,7 @@ static void paintBackground( Graphics g, JTextComponent c, int focusWidth, boole : (isIntelliJTheme && (!c.isEnabled() || !c.isEditable()) ? FlatUIUtils.getParentBackground( c ) : background) ); - FlatUIUtils.paintComponentBackground( g2, 0, 0, c.getWidth(), c.getHeight(), fFocusWidth, 0 ); + FlatUIUtils.paintComponentBackground( g2, 0, 0, c.getWidth(), c.getHeight(), fFocusWidth, fArc ); } finally { g2.dispose(); } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 622e39f0e..343d93167 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -189,7 +189,7 @@ FileView.floppyDriveIcon=com.formdev.flatlaf.icons.FlatFileViewFloppyDriveIcon #---- FormattedTextField ---- -FormattedTextField.border=com.formdev.flatlaf.ui.FlatBorder +FormattedTextField.border=com.formdev.flatlaf.ui.FlatTextBorder FormattedTextField.margin=@textComponentMargin FormattedTextField.background=@textComponentBackground FormattedTextField.placeholderForeground=@disabledText @@ -296,7 +296,7 @@ OptionPane.warningIcon=com.formdev.flatlaf.icons.FlatOptionPaneWarningIcon #---- PasswordField ---- -PasswordField.border=com.formdev.flatlaf.ui.FlatBorder +PasswordField.border=com.formdev.flatlaf.ui.FlatTextBorder PasswordField.margin=@textComponentMargin PasswordField.background=@textComponentBackground PasswordField.placeholderForeground=@disabledText @@ -457,11 +457,12 @@ TextArea.background=@textComponentBackground # allowed values: "never", "once" (default) or "always" TextComponent.selectAllOnFocusPolicy=once +TextComponent.arc=0 #---- TextField ---- -TextField.border=com.formdev.flatlaf.ui.FlatBorder +TextField.border=com.formdev.flatlaf.ui.FlatTextBorder TextField.margin=@textComponentMargin TextField.background=@textComponentBackground TextField.placeholderForeground=@disabledText