Skip to content

Commit

Permalink
TextField, FormattedTextField and PasswordField: support round border…
Browse files Browse the repository at this point in the history
… (issue #65)
  • Loading branch information
DevCharly committed Feb 17, 2020
1 parent f736ed4 commit b522500
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
* <!-- FlatTextFieldUI -->
*
* @uiDefault TextComponent.arc int
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
*
* <!-- FlatPasswordFieldUI -->
*
* @uiDefault TextComponent.arc int
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
Expand All @@ -62,6 +63,7 @@
public class FlatPasswordFieldUI
extends BasicPasswordFieldUI
{
protected int arc;
protected int focusWidth;
protected int minimumWidth;
protected boolean isIntelliJTheme;
Expand All @@ -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" );
Expand Down Expand Up @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +60,7 @@
*
* <!-- FlatTextFieldUI -->
*
* @uiDefault TextComponent.arc int
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
Expand All @@ -70,6 +72,7 @@
public class FlatTextFieldUI
extends BasicTextFieldUI
{
protected int arc;
protected int focusWidth;
protected int minimumWidth;
protected boolean isIntelliJTheme;
Expand All @@ -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" );
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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
Expand All @@ -164,15 +170,16 @@ 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)
? background
: (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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b522500

Please sign in to comment.